json_document: read files and streams in one block
parse(FILE*) and parse(std::istream&) read their input into a string that grew by doubling, with a reallocation and copy at each step (about 16 reads for a 0.6 MB file). On Linux, glibc then grows and trims the heap around these blocks, so the memory faults in again on every parse. Reserve the rest of the input first when its size is known: for a FILE* that is a regular file, from fstat (_fstat64 on Windows) and its position; for a stream, from in_avail() (which libstdc++ answers for files with the rest of the file). The read loop is unchanged; the size is only a hint. File to document on Linux (GCC 16): twitter.json -72%, canada.json -72%, semanticscholar-corpus.json -73%, jeopardy.json -25..-29%, small files -0..-17%. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
3 files changed
+149
-6
No files matched your search
@@ -11,7 +11,15 @@
|
||||
#include <algorithm> // max
|
||||
#include <cstddef> // size_t
|
||||
#ifndef JSON_NO_IO
|
||||
#include <cstdio> // FILE, fread
|
||||
#include <cstdio> // FILE, fread, ftell
|
||||
#if defined(_WIN32)
|
||||
#include <stdio.h> // _fileno, _ftelli64 // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers)
|
||||
#include <sys/types.h> // (for sys/stat.h)
|
||||
#include <sys/stat.h> // _fstat64, _S_IFMT, _S_IFREG
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
#include <stdio.h> // fileno // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers)
|
||||
#include <sys/stat.h> // fstat, S_ISREG
|
||||
#endif
|
||||
#endif
|
||||
#include <istream> // istream, streambuf
|
||||
#include <string> // basic_string, char_traits, string
|
||||
@@ -142,6 +150,16 @@ inline void append_stream(std::string& buf, std::istream& is)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// room for the rest when the stream buffer knows how much is left (for a
|
||||
// file with libstdc++, the rest of the file), and for the read that finds
|
||||
// the end: the input is then read in one block, without a reallocation and
|
||||
// copy at each doubling of the string (only a hint: the loop below reads
|
||||
// what is there)
|
||||
const std::streamsize rest = sb->in_avail();
|
||||
if (rest > 0 && static_cast<std::size_t>(rest) < buf.max_size() - buf.size() - 4096)
|
||||
{
|
||||
buf.reserve(buf.size() + static_cast<std::size_t>(rest) + 4096);
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
const std::size_t used = buf.size();
|
||||
@@ -168,11 +186,51 @@ struct is_c_stream
|
||||
{};
|
||||
|
||||
#ifndef JSON_NO_IO
|
||||
/// the number of bytes left in a C stream that is a regular file (from its
|
||||
/// size and position), 0 for anything else and on other systems: a hint for
|
||||
/// the room to reserve; f is not null
|
||||
inline unsigned long long file_rest(std::FILE* f) noexcept
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
struct _stat64 st {};
|
||||
if (::_fstat64(_fileno(f), &st) == 0 && (st.st_mode & _S_IFMT) == _S_IFREG)
|
||||
{
|
||||
const long long here = ::_ftelli64(f);
|
||||
if (here >= 0 && st.st_size > here)
|
||||
{
|
||||
return static_cast<unsigned long long>(st.st_size - here);
|
||||
}
|
||||
}
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
struct stat st {};
|
||||
if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode))
|
||||
{
|
||||
const long here = std::ftell(f);
|
||||
if (here >= 0 && st.st_size > here)
|
||||
{
|
||||
return static_cast<unsigned long long>(st.st_size - here);
|
||||
}
|
||||
}
|
||||
#else
|
||||
static_cast<void>(f);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// append the rest of a C stream: in blocks with fread, not character by
|
||||
/// character (the library's file adapter reads with fgetc);
|
||||
/// f is not null
|
||||
/// character (the library's file adapter reads with fgetc), and in one block
|
||||
/// when it is a regular file of known size; f is not null
|
||||
inline void append_file(std::string& buf, std::FILE* f)
|
||||
{
|
||||
// room for the rest of a regular file and the byte that shows its end:
|
||||
// the file is then read in one block, without a reallocation and copy at
|
||||
// each doubling of the string (only a hint: the loop below reads what is
|
||||
// there)
|
||||
const unsigned long long rest = file_rest(f);
|
||||
if (rest > 0 && rest < buf.max_size() - buf.size() - 1)
|
||||
{
|
||||
buf.reserve(buf.size() + static_cast<std::size_t>(rest) + 1);
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
const std::size_t used = buf.size();
|
||||
|
||||
Reference in new issue
Block a user