diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index f9a79e42d..9b3d2d7f4 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -313,6 +313,18 @@ class input_stream_adapter { #if JSON_PRECISE_STREAM_POSITION commit_lookahead(); +#endif +#if !JSON_DISABLE_STREAM_BULK_SCAN + // the elements are in the get area: copy them, as sgetn() does, but + // without the virtual call + const char* const first = streambuf_access::get_ptr(sb); + const auto available = static_cast(streambuf_access::get_end(sb) - first); + if (JSON_HEDLEY_LIKELY(count * sizeof(T) <= available && count * sizeof(T) <= static_cast((std::numeric_limits::max)()))) + { + std::memcpy(dest, first, count * sizeof(T)); + streambuf_access::bump(sb, static_cast(count * sizeof(T))); + return count * sizeof(T); + } #endif auto res = static_cast(sb->sgetn(reinterpret_cast(dest), static_cast(count * sizeof(T)))); if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T))) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4c466ee98..ff1c3a674 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -8778,6 +8778,18 @@ class input_stream_adapter { #if JSON_PRECISE_STREAM_POSITION commit_lookahead(); +#endif +#if !JSON_DISABLE_STREAM_BULK_SCAN + // the elements are in the get area: copy them, as sgetn() does, but + // without the virtual call + const char* const first = streambuf_access::get_ptr(sb); + const auto available = static_cast(streambuf_access::get_end(sb) - first); + if (JSON_HEDLEY_LIKELY(count * sizeof(T) <= available && count * sizeof(T) <= static_cast((std::numeric_limits::max)()))) + { + std::memcpy(dest, first, count * sizeof(T)); + streambuf_access::bump(sb, static_cast(count * sizeof(T))); + return count * sizeof(T); + } #endif auto res = static_cast(sb->sgetn(reinterpret_cast(dest), static_cast(count * sizeof(T)))); if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T)))