From 6b86cbcb2547105a1c598f2f1da44e640c69b9ec Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 19 Aug 2020 11:39:56 +1000 Subject: [PATCH] Optimize example/jsonptr's write_dst On a mid-range x86_64 laptop, processing the 181 MiB citylots.json file from github.com/zemirco/sf-city-lots-json: $ time gen/bin/example-jsonptr < citylots.json > /dev/null Before this commit: real 0m1.949s After: real 0m1.863s Ratio: 1.05x --- example/cbor-to-json/cbor-to-json.cc | 22 ++++++++++++++++++---- example/json-to-cbor/json-to-cbor.cc | 21 +++++++++++++++++---- example/jsonptr/jsonptr.cc | 18 ++++++++++++++---- 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/example/cbor-to-json/cbor-to-json.cc b/example/cbor-to-json/cbor-to-json.cc index 2a9a76da..da9df533 100644 --- a/example/cbor-to-json/cbor-to-json.cc +++ b/example/cbor-to-json/cbor-to-json.cc @@ -137,7 +137,11 @@ static const char* g_usage = const char* g_new_line_then_256_indent_bytes; uint32_t g_bytes_per_indent_depth; -uint8_t g_dst_array[32768]; +#ifndef DST_BUFFER_ARRAY_SIZE +#define DST_BUFFER_ARRAY_SIZE (32 * 1024) +#endif + +uint8_t g_dst_array[DST_BUFFER_ARRAY_SIZE]; wuffs_base__io_buffer g_dst; uint32_t g_depth; @@ -252,7 +256,7 @@ flush_dst() { } std::string // -write_dst(const void* s, size_t n) { +write_dst_slow(const void* s, size_t n) { const uint8_t* p = static_cast(s); while (n > 0) { size_t i = g_dst.writer_length(); @@ -276,6 +280,17 @@ write_dst(const void* s, size_t n) { return ""; } +inline std::string // +write_dst(const void* s, size_t n) { + if (n <= (DST_BUFFER_ARRAY_SIZE - g_dst.meta.wi)) { + memcpy(g_dst.data.ptr + g_dst.meta.wi, s, n); + g_dst.meta.wi += n; + g_wrote_to_dst = true; + return ""; + } + return write_dst_slow(s, n); +} + // ---- class Callbacks : public wuffs_aux::DecodeCborCallbacks { @@ -612,8 +627,7 @@ class Callbacks : public wuffs_aux::DecodeCborCallbacks { std::string // main1(int argc, char** argv) { - g_dst = wuffs_base__ptr_u8__writer( - &g_dst_array[0], (sizeof(g_dst_array) / sizeof(g_dst_array[0]))); + g_dst = wuffs_base__ptr_u8__writer(&g_dst_array[0], DST_BUFFER_ARRAY_SIZE); g_depth = 0; g_ctx = context::none; g_wrote_to_dst = false; diff --git a/example/json-to-cbor/json-to-cbor.cc b/example/json-to-cbor/json-to-cbor.cc index 2c6bdfb4..b1e4195b 100644 --- a/example/json-to-cbor/json-to-cbor.cc +++ b/example/json-to-cbor/json-to-cbor.cc @@ -114,7 +114,11 @@ static const char* g_usage = // ---- -uint8_t g_dst_array[32768]; +#ifndef DST_BUFFER_ARRAY_SIZE +#define DST_BUFFER_ARRAY_SIZE (32 * 1024) +#endif + +uint8_t g_dst_array[DST_BUFFER_ARRAY_SIZE]; wuffs_base__io_buffer g_dst; std::vector g_quirks; @@ -190,7 +194,7 @@ flush_dst() { } std::string // -write_dst(const void* s, size_t n) { +write_dst_slow(const void* s, size_t n) { const uint8_t* p = static_cast(s); while (n > 0) { size_t i = g_dst.writer_length(); @@ -213,6 +217,16 @@ write_dst(const void* s, size_t n) { return ""; } +inline std::string // +write_dst(const void* s, size_t n) { + if (n <= (DST_BUFFER_ARRAY_SIZE - g_dst.meta.wi)) { + memcpy(g_dst.data.ptr + g_dst.meta.wi, s, n); + g_dst.meta.wi += n; + return ""; + } + return write_dst_slow(s, n); +} + // ---- class Callbacks : public wuffs_aux::DecodeJsonCallbacks { @@ -298,8 +312,7 @@ class Callbacks : public wuffs_aux::DecodeJsonCallbacks { std::string // main1(int argc, char** argv) { - g_dst = wuffs_base__ptr_u8__writer( - &g_dst_array[0], (sizeof(g_dst_array) / sizeof(g_dst_array[0]))); + g_dst = wuffs_base__ptr_u8__writer(&g_dst_array[0], DST_BUFFER_ARRAY_SIZE); TRY(parse_flags(argc, argv)); diff --git a/example/jsonptr/jsonptr.cc b/example/jsonptr/jsonptr.cc index 1a71628b..3064ef0f 100644 --- a/example/jsonptr/jsonptr.cc +++ b/example/jsonptr/jsonptr.cc @@ -818,10 +818,7 @@ flush_dst() { } const char* // -write_dst(const void* s, size_t n) { - if (g_suppress_write_dst > 0) { - return nullptr; - } +write_dst_slow(const void* s, size_t n) { const uint8_t* p = static_cast(s); while (n > 0) { size_t i = g_dst.writer_length(); @@ -848,6 +845,19 @@ write_dst(const void* s, size_t n) { return nullptr; } +inline const char* // +write_dst(const void* s, size_t n) { + if (g_suppress_write_dst > 0) { + return nullptr; + } else if (n <= (DST_BUFFER_ARRAY_SIZE - g_dst.meta.wi)) { + memcpy(g_dst.data.ptr + g_dst.meta.wi, s, n); + g_dst.meta.wi += n; + g_wrote_to_dst = true; + return nullptr; + } + return write_dst_slow(s, n); +} + // ---- uint8_t //