Skip to content

Commit

Permalink
Optimize real integers on ANY_PACKED_TYPE_TAG_BYTE_PREFIX (#853)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
  • Loading branch information
jviotti authored Oct 9, 2024
1 parent 071f8d9 commit 63a3a1e
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/runtime/decoder_any.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(this->get_byte())};
case SUBTYPE_POSITIVE_INTEGER:
return sourcemeta::jsontoolkit::JSON{
static_cast<std::int64_t>(this->get_varint())};
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/encoder_any.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ auto Encoder::ANY_PACKED_TYPE_TAG_BYTE_PREFIX(
: SUBTYPE_FALSE};
this->put_byte(TYPE_OTHER |
static_cast<std::uint8_t>(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<std::uint8_t>(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, {});
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -440,6 +441,7 @@ static_assert(SUBTYPE_NULL <= uint_max<subtype_size>);
static_assert(SUBTYPE_POSITIVE_INTEGER <= uint_max<subtype_size>);
static_assert(SUBTYPE_NEGATIVE_INTEGER <= uint_max<subtype_size>);
static_assert(SUBTYPE_NUMBER <= uint_max<subtype_size>);
static_assert(SUBTYPE_POSITIVE_REAL_INTEGER_BYTE <= uint_max<subtype_size>);
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_7 <= uint_max<subtype_size>);
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_8 <= uint_max<subtype_size>);
static_assert(SUBTYPE_LONG_STRING_BASE_EXPONENT_9 <= uint_max<subtype_size>);
Expand Down
6 changes: 1 addition & 5 deletions test/e2e/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,22 @@ 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)
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)
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)
Expand All @@ -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)
Expand Down
Binary file modified test/e2e/circleciblank/schema-less/output.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion test/e2e/circleciblank/schema-less/size.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12
11
Binary file modified test/e2e/geojson/schema-less/output.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion test/e2e/geojson/schema-less/size.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
157
127
18 changes: 18 additions & 0 deletions test/runtime/decode_any_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,24 @@ TEST(JSONBinPack_Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__real_3_14) {
EXPECT_EQ(result, expected);
}

TEST(JSONBinPack_Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__real_3_0) {
using namespace sourcemeta::jsonbinpack;
InputByteStream stream{0x37, 0x03};
Decoder decoder{stream};
const auto result = decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({});
const sourcemeta::jsontoolkit::JSON expected{3.0};
EXPECT_EQ(result, expected);
}

TEST(JSONBinPack_Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__real_103_0) {
using namespace sourcemeta::jsonbinpack;
InputByteStream stream{0x37, 0x67};
Decoder decoder{stream};
const auto result = decoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX({});
const sourcemeta::jsontoolkit::JSON expected{103.0};
EXPECT_EQ(result, expected);
}

TEST(JSONBinPack_Decoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__256) {
using namespace sourcemeta::jsonbinpack;
InputByteStream stream{0x1f, 0x80, 0x02};
Expand Down
26 changes: 26 additions & 0 deletions test/runtime/encode_any_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,32 @@ TEST(JSONBinPack_Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__real_3_14) {
});
}

TEST(JSONBinPack_Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__real_3_0) {
using namespace sourcemeta::jsonbinpack;
sourcemeta::jsontoolkit::JSON document{3.0};
OutputByteStream stream{};

Encoder encoder{stream};
encoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX(document, {});
EXPECT_BYTES(stream, {
0x37, // tag: positive real integer byte
0x03 // 3
});
}

TEST(JSONBinPack_Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__real_103_0) {
using namespace sourcemeta::jsonbinpack;
sourcemeta::jsontoolkit::JSON document{103.0};
OutputByteStream stream{};

Encoder encoder{stream};
encoder.ANY_PACKED_TYPE_TAG_BYTE_PREFIX(document, {});
EXPECT_BYTES(stream, {
0x37, // tag: positive real integer byte
0x67 // 103
});
}

TEST(JSONBinPack_Encoder, ANY_PACKED_TYPE_TAG_BYTE_PREFIX__256) {
using namespace sourcemeta::jsonbinpack;
sourcemeta::jsontoolkit::JSON document{256};
Expand Down

0 comments on commit 63a3a1e

Please sign in to comment.