From fe6df2c6a555ff8b5ed2a5aa6726315fc838645a Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 9 Oct 2024 10:58:38 -0400 Subject: [PATCH] Optimize real integers on `ANY_PACKED_TYPE_TAG_BYTE_PREFIX` Signed-off-by: Juan Cruz Viotti --- src/runtime/decoder_any.cc | 3 ++ src/runtime/encoder_any.cc | 10 +++++++ .../sourcemeta/jsonbinpack/runtime_encoding.h | 2 ++ test/e2e/CMakeLists.txt | 6 +--- test/e2e/circleciblank/schema-less/output.bin | Bin 12 -> 11 bytes test/e2e/circleciblank/schema-less/size.txt | 2 +- test/e2e/geojson/schema-less/output.bin | Bin 157 -> 127 bytes test/e2e/geojson/schema-less/size.txt | 2 +- test/runtime/decode_any_test.cc | 18 ++++++++++++ test/runtime/encode_any_test.cc | 26 ++++++++++++++++++ 10 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/runtime/decoder_any.cc b/src/runtime/decoder_any.cc index f79b684a..44445b75 100644 --- a/src/runtime/decoder_any.cc +++ b/src/runtime/decoder_any.cc @@ -64,6 +64,9 @@ auto Decoder::ANY_PACKED_TYPE_TAG_BYTE_PREFIX( return sourcemeta::jsontoolkit::JSON{true}; case SUBTYPE_NUMBER: return this->DOUBLE_VARINT_TUPLE({}); + case SUBTYPE_POSITIVE_REAL_INTEGER_BYTE: + return sourcemeta::jsontoolkit::JSON{ + static_cast(this->get_byte())}; case SUBTYPE_POSITIVE_INTEGER: return sourcemeta::jsontoolkit::JSON{ static_cast(this->get_varint())}; diff --git a/src/runtime/encoder_any.cc b/src/runtime/encoder_any.cc index 909c9214..8ce4a4b9 100644 --- a/src/runtime/encoder_any.cc +++ b/src/runtime/encoder_any.cc @@ -81,6 +81,16 @@ auto Encoder::ANY_PACKED_TYPE_TAG_BYTE_PREFIX( : SUBTYPE_FALSE}; this->put_byte(TYPE_OTHER | static_cast(subtype << type_size)); + } else if (document.is_integer_real()) { + const auto value{document.as_integer()}; + if (value >= 0 && is_byte(value)) { + this->put_byte(TYPE_OTHER | SUBTYPE_POSITIVE_REAL_INTEGER_BYTE + << type_size); + this->put_byte(static_cast(value)); + } else { + this->put_byte(TYPE_OTHER | SUBTYPE_NUMBER << type_size); + this->DOUBLE_VARINT_TUPLE(document, {}); + } } else if (document.is_real()) { this->put_byte(TYPE_OTHER | SUBTYPE_NUMBER << type_size); this->DOUBLE_VARINT_TUPLE(document, {}); diff --git a/src/runtime/include/sourcemeta/jsonbinpack/runtime_encoding.h b/src/runtime/include/sourcemeta/jsonbinpack/runtime_encoding.h index 3c020051..2e225e81 100644 --- a/src/runtime/include/sourcemeta/jsonbinpack/runtime_encoding.h +++ b/src/runtime/include/sourcemeta/jsonbinpack/runtime_encoding.h @@ -429,6 +429,7 @@ constexpr std::uint8_t SUBTYPE_NULL = 0b00000010; constexpr std::uint8_t SUBTYPE_POSITIVE_INTEGER = 0b00000011; constexpr std::uint8_t SUBTYPE_NEGATIVE_INTEGER = 0b00000100; constexpr std::uint8_t SUBTYPE_NUMBER = 0b00000101; +constexpr std::uint8_t SUBTYPE_POSITIVE_REAL_INTEGER_BYTE = 0b00000110; constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_7 = 0b00000111; constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_8 = 0b00001000; constexpr std::uint8_t SUBTYPE_LONG_STRING_BASE_EXPONENT_9 = 0b00001001; @@ -440,6 +441,7 @@ static_assert(SUBTYPE_NULL <= uint_max); static_assert(SUBTYPE_POSITIVE_INTEGER <= uint_max); static_assert(SUBTYPE_NEGATIVE_INTEGER <= uint_max); static_assert(SUBTYPE_NUMBER <= uint_max); +static_assert(SUBTYPE_POSITIVE_REAL_INTEGER_BYTE <= uint_max); static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_7 <= uint_max); static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_8 <= uint_max); static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_9 <= uint_max); diff --git a/test/e2e/CMakeLists.txt b/test/e2e/CMakeLists.txt index a2d12b40..26e361ad 100644 --- a/test/e2e/CMakeLists.txt +++ b/test/e2e/CMakeLists.txt @@ -18,7 +18,6 @@ macro(add_jsonbinpack_e2e_test name) # TODO: Test schema-driven too endmacro() -# TODO: This case regresses. Should be 10 add_jsonbinpack_e2e_test(circleciblank) add_jsonbinpack_e2e_test(circlecimatrix) add_jsonbinpack_e2e_test(commitlint) @@ -26,7 +25,6 @@ add_jsonbinpack_e2e_test(commitlintbasic) add_jsonbinpack_e2e_test(epr) add_jsonbinpack_e2e_test(eslintrc) add_jsonbinpack_e2e_test(esmrc) -# TODO: This case regresses. Should be 117 add_jsonbinpack_e2e_test(geojson) add_jsonbinpack_e2e_test(githubfundingblank) add_jsonbinpack_e2e_test(githubworkflow) @@ -34,9 +32,8 @@ add_jsonbinpack_e2e_test(gruntcontribclean) add_jsonbinpack_e2e_test(imageoptimizerwebjob) add_jsonbinpack_e2e_test(jsonereversesort) add_jsonbinpack_e2e_test(jsonesort) -# TODO: This case regresses. Should be 512 add_jsonbinpack_e2e_test(jsonfeed) -# TODO: This case regresses. Should be 2612 +# TODO: This case regresses and we are beaten by Smile. Should be 2612 add_jsonbinpack_e2e_test(jsonresume) add_jsonbinpack_e2e_test(mixed-bounded-object) add_jsonbinpack_e2e_test(netcoreproject) @@ -45,7 +42,6 @@ add_jsonbinpack_e2e_test(openweathermap) add_jsonbinpack_e2e_test(openweatherroadrisk) add_jsonbinpack_e2e_test(ox-test) add_jsonbinpack_e2e_test(packagejson) -# TODO: This case regresses. Should be 791 add_jsonbinpack_e2e_test(packagejsonlintrc) add_jsonbinpack_e2e_test(sapcloudsdkpipeline) add_jsonbinpack_e2e_test(travisnotifications) diff --git a/test/e2e/circleciblank/schema-less/output.bin b/test/e2e/circleciblank/schema-less/output.bin index d408f2f31fd515d6bc32f03bb7ac69064daf5796..9cc7f975510bc97bbc806c41f191349208f6fd7e 100644 GIT binary patch literal 11 ScmWgUC`&CW&dkp!Ei literal 12 TcmWgUC`&CW&dkr#XJG&U7Zd|k diff --git a/test/e2e/circleciblank/schema-less/size.txt b/test/e2e/circleciblank/schema-less/size.txt index 48082f72..b4de3947 100644 --- a/test/e2e/circleciblank/schema-less/size.txt +++ b/test/e2e/circleciblank/schema-less/size.txt @@ -1 +1 @@ -12 +11 diff --git a/test/e2e/geojson/schema-less/output.bin b/test/e2e/geojson/schema-less/output.bin index 78b0fd87708a49482ef2dcb684b433309413879c..d367f4b57c2918ab3215a765646871d4c81aabc1 100644 GIT binary patch delta 59 vcmbQsSU*A8$UM!QNya=KLNLn!xnP1x#ze+E#hgLLJQYGPg1BH}VzCwghj&GaqJDaj1Tq$&*MqX&o4^J%u6guEtU~6k