Skip to content

Commit

Permalink
Put Context member in Encoder class (#838)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Oct 8, 2024
1 parent 5a2eae1 commit 2eaffb6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/runtime/encoder_any.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,18 @@ auto Encoder::ANY_PACKED_TYPE_TAG_BYTE_PREFIX(
} else if (document.is_string()) {
const sourcemeta::jsontoolkit::JSON::String value{document.to_string()};
const auto size{document.byte_size()};
const bool is_shared{this->context().has(value, Context::Type::Standalone)};
const bool is_shared{this->context_.has(value, Context::Type::Standalone)};
if (size < uint_max<5>) {
const std::uint8_t type{is_shared ? TYPE_SHARED_STRING : TYPE_STRING};
this->put_byte(
static_cast<std::uint8_t>(type | ((size + 1) << type_size)));
if (is_shared) {
this->put_varint(
this->position() -
this->context().offset(value, Context::Type::Standalone));
this->context_.offset(value, Context::Type::Standalone));
} else {
this->context().record(value, this->position(),
Context::Type::Standalone);
this->context_.record(value, this->position(),
Context::Type::Standalone);
this->put_string_utf8(value, size);
}
} else if (size >= uint_max<5> && size < uint_max<5> * 2 && !is_shared) {
Expand Down
36 changes: 18 additions & 18 deletions src/runtime/encoder_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ auto Encoder::FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED(
const sourcemeta::jsontoolkit::JSON::String value{document.to_string()};
const auto size{value.size()};
assert(document.size() == size);
const bool is_shared{this->context().has(value, Context::Type::Standalone)};
const bool is_shared{this->context_.has(value, Context::Type::Standalone)};

// (1) Write 0x00 if shared, else do nothing
if (is_shared) {
Expand All @@ -34,9 +34,9 @@ auto Encoder::FLOOR_VARINT_PREFIX_UTF8_STRING_SHARED(
// (3) Write relative offset if shared, else write plain string
if (is_shared) {
this->put_varint(this->position() -
this->context().offset(value, Context::Type::Standalone));
this->context_.offset(value, Context::Type::Standalone));
} else {
this->context().record(value, this->position(), Context::Type::Standalone);
this->context_.record(value, this->position(), Context::Type::Standalone);
this->put_string_utf8(value, size);
}
}
Expand All @@ -49,7 +49,7 @@ auto Encoder::ROOF_VARINT_PREFIX_UTF8_STRING_SHARED(
const auto size{value.size()};
assert(document.size() == size);
assert(size <= options.maximum);
const bool is_shared{this->context().has(value, Context::Type::Standalone)};
const bool is_shared{this->context_.has(value, Context::Type::Standalone)};

// (1) Write 0x00 if shared, else do nothing
if (is_shared) {
Expand All @@ -62,9 +62,9 @@ auto Encoder::ROOF_VARINT_PREFIX_UTF8_STRING_SHARED(
// (3) Write relative offset if shared, else write plain string
if (is_shared) {
this->put_varint(this->position() -
this->context().offset(value, Context::Type::Standalone));
this->context_.offset(value, Context::Type::Standalone));
} else {
this->context().record(value, this->position(), Context::Type::Standalone);
this->context_.record(value, this->position(), Context::Type::Standalone);
this->put_string_utf8(value, size);
}
}
Expand All @@ -79,7 +79,7 @@ auto Encoder::BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED(
assert(options.minimum <= options.maximum);
assert(is_byte(options.maximum - options.minimum + 1));
assert(is_within(size, options.minimum, options.maximum));
const bool is_shared{this->context().has(value, Context::Type::Standalone)};
const bool is_shared{this->context_.has(value, Context::Type::Standalone)};

// (1) Write 0x00 if shared, else do nothing
if (is_shared) {
Expand All @@ -92,9 +92,9 @@ auto Encoder::BOUNDED_8BIT_PREFIX_UTF8_STRING_SHARED(
// (3) Write relative offset if shared, else write plain string
if (is_shared) {
this->put_varint(this->position() -
this->context().offset(value, Context::Type::Standalone));
this->context_.offset(value, Context::Type::Standalone));
} else {
this->context().record(value, this->position(), Context::Type::Standalone);
this->context_.record(value, this->position(), Context::Type::Standalone);
this->put_string_utf8(value, size);
}
}
Expand Down Expand Up @@ -129,23 +129,23 @@ auto Encoder::PREFIX_VARINT_LENGTH_STRING_SHARED(
assert(document.is_string());
const sourcemeta::jsontoolkit::JSON::String value{document.to_string()};

if (this->context().has(value, Context::Type::PrefixLengthVarintPlusOne)) {
if (this->context_.has(value, Context::Type::PrefixLengthVarintPlusOne)) {
const auto new_offset{this->position()};
this->put_byte(0);
this->put_varint(this->position() -
this->context().offset(
value, Context::Type::PrefixLengthVarintPlusOne));
this->put_varint(
this->position() -
this->context_.offset(value, Context::Type::PrefixLengthVarintPlusOne));
// Bump the context cache for locality purposes
this->context().record(value, new_offset,
Context::Type::PrefixLengthVarintPlusOne);
this->context_.record(value, new_offset,
Context::Type::PrefixLengthVarintPlusOne);
} else {
const auto size{value.size()};
assert(document.size() == size);
this->context().record(value, this->position(),
Context::Type::PrefixLengthVarintPlusOne);
this->context_.record(value, this->position(),
Context::Type::PrefixLengthVarintPlusOne);
this->put_varint(size + 1);
// Also record a standalone variant of it
this->context().record(value, this->position(), Context::Type::Standalone);
this->context_.record(value, this->position(), Context::Type::Standalone);
this->put_string_utf8(value, size);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/include/sourcemeta/jsonbinpack/runtime_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "runtime_export.h"

#include <sourcemeta/jsonbinpack/runtime_encoder_context.h>
#include <sourcemeta/jsonbinpack/runtime_output_stream.h>
#include <sourcemeta/jsonbinpack/runtime_plan.h>

Expand Down Expand Up @@ -63,6 +64,9 @@ class SOURCEMETA_JSONBINPACK_RUNTIME_EXPORT Encoder : private OutputStream {

#undef DECLARE_ENCODING
#endif

private:
Context context_;
};

} // namespace sourcemeta::jsonbinpack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class SOURCEMETA_JSONBINPACK_RUNTIME_EXPORT Context {
#if defined(_MSC_VER)
#pragma warning(disable : 4251 4275)
#endif
// TODO: Keep a reference to the string instead of copying it
std::map<std::pair<sourcemeta::jsontoolkit::JSON::String, Type>,
std::uint64_t>
strings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#include <sourcemeta/jsontoolkit/json.h>

#include <sourcemeta/jsonbinpack/runtime_encoder_context.h>

#include <cstdint> // std::uint8_t, std::uint16_t, std::uint64_t
#include <ostream> // std::basic_ostream

Expand All @@ -30,11 +28,9 @@ class SOURCEMETA_JSONBINPACK_RUNTIME_EXPORT OutputStream {
auto put_varint_zigzag(const std::int64_t value) -> void;
auto put_string_utf8(const sourcemeta::jsontoolkit::JSON::String &string,
const std::uint64_t length) -> void;
auto context() -> Context &;

private:
Stream &stream;
Context context_;
};

} // namespace sourcemeta::jsonbinpack
Expand Down
2 changes: 0 additions & 2 deletions src/runtime/output_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,4 @@ auto OutputStream::put_string_utf8(
}
}

auto OutputStream::context() -> Context & { return this->context_; }

} // namespace sourcemeta::jsonbinpack

0 comments on commit 2eaffb6

Please sign in to comment.