diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp index 1cad57b19..f2a230320 100644 --- a/include/nlohmann/detail/output/output_adapters.hpp +++ b/include/nlohmann/detail/output/output_adapters.hpp @@ -53,7 +53,7 @@ class output_vector_adapter : public output_adapter_protocol JSON_HEDLEY_NON_NULL(2) void write_characters(const CharType* s, std::size_t length) override { - std::copy(s, s + length, std::back_inserter(v)); + v.insert(v.end(), s, s + length); } private: diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 65670d92c..cffbd7dce 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -14250,7 +14250,7 @@ class output_vector_adapter : public output_adapter_protocol JSON_HEDLEY_NON_NULL(2) void write_characters(const CharType* s, std::size_t length) override { - std::copy(s, s + length, std::back_inserter(v)); + v.insert(v.end(), s, s + length); } private: diff --git a/tests/benchmarks/src/benchmarks.cpp b/tests/benchmarks/src/benchmarks.cpp index 4f32a61a2..aa57e4347 100644 --- a/tests/benchmarks/src/benchmarks.cpp +++ b/tests/benchmarks/src/benchmarks.cpp @@ -1,6 +1,8 @@ #include "benchmark/benchmark.h" #include #include +#include +#include #include using json = nlohmann::json; @@ -107,4 +109,63 @@ BENCHMARK_CAPTURE(Dump, small_signed_ints / -, TEST_DATA_DIRECTORY "/regression/ BENCHMARK_CAPTURE(Dump, small_signed_ints / 4, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json", 4); +////////////////////////////////////////////////////////////////////////////// +// serialize CBOR +////////////////////////////////////////////////////////////////////////////// +static void ToCbor(benchmark::State& state, const char* filename) +{ + std::ifstream f(filename); + std::string str((std::istreambuf_iterator(f)), std::istreambuf_iterator()); + json j = json::parse(str); + + while (state.KeepRunning()) + { + json::to_cbor(j); + } + + state.SetBytesProcessed(state.iterations() * json::to_cbor(j).size()); +} +BENCHMARK_CAPTURE(ToCbor, jeopardy, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json"); +BENCHMARK_CAPTURE(ToCbor, canada, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json"); +BENCHMARK_CAPTURE(ToCbor, citm_catalog, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json"); +BENCHMARK_CAPTURE(ToCbor, twitter, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json"); +BENCHMARK_CAPTURE(ToCbor, floats, TEST_DATA_DIRECTORY "/regression/floats.json"); +BENCHMARK_CAPTURE(ToCbor, signed_ints, TEST_DATA_DIRECTORY "/regression/signed_ints.json"); +BENCHMARK_CAPTURE(ToCbor, unsigned_ints, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json"); +BENCHMARK_CAPTURE(ToCbor, small_signed_ints, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json"); + +////////////////////////////////////////////////////////////////////////////// +// serialize binary CBOR +////////////////////////////////////////////////////////////////////////////// +static void BinaryToCbor(benchmark::State& state) +{ + std::vector data(256); + std::iota(data.begin(), data.end(), 0); + + auto it = data.begin(); + std::vector in; + in.reserve(state.range(0)); + for (int i = 0; i < state.range(0); ++i) + { + if (it == data.end()) + { + it = data.begin(); + } + + in.push_back(*it); + ++it; + } + + json::binary_t bin{in}; + json j{{"type", "binary"}, {"data", bin}}; + + while (state.KeepRunning()) + { + json::to_cbor(j); + } + + state.SetBytesProcessed(state.iterations() * json::to_cbor(j).size()); +} +BENCHMARK(BinaryToCbor)->RangeMultiplier(2)->Range(8, 8 << 12); + BENCHMARK_MAIN();