-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rethink and simplify the encoder context class
Signed-off-by: Juan Cruz Viotti <[email protected]>
- Loading branch information
Showing
10 changed files
with
311 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <sourcemeta/jsonbinpack/runtime_encoder_cache.h> | ||
|
||
namespace sourcemeta::jsonbinpack { | ||
|
||
auto Cache::record(const sourcemeta::jsontoolkit::JSON::String &value, | ||
const std::uint64_t offset, const Type type) -> void { | ||
// Encoding a shared string has some overhead, such as the | ||
// shared string marker + the offset, so its not worth | ||
// doing for strings that are too small. | ||
constexpr auto MINIMUM_STRING_LENGTH{3}; | ||
|
||
// We don't want to allow the context to grow | ||
// forever, otherwise an attacker could force the | ||
// program to exhaust memory given an input | ||
// document that contains a high number of large strings. | ||
constexpr auto MAXIMUM_BYTE_SIZE{20971520}; | ||
|
||
const auto value_size{value.size()}; | ||
if (value_size < MINIMUM_STRING_LENGTH || value_size >= MAXIMUM_BYTE_SIZE) { | ||
return; | ||
} | ||
|
||
// Remove the oldest entries to make space if needed | ||
while (!this->data.empty() && | ||
this->byte_size + value_size >= MAXIMUM_BYTE_SIZE) { | ||
this->remove_oldest(); | ||
} | ||
|
||
auto result{this->data.insert({std::make_pair(value, type), offset})}; | ||
if (result.second) { | ||
this->byte_size += value_size; | ||
this->order.emplace(offset, result.first->first); | ||
} else if (offset > result.first->second) { | ||
this->order.erase(result.first->second); | ||
// If the string already exists, we want to | ||
// bump the offset for locality purposes. | ||
result.first->second = offset; | ||
this->order.emplace(offset, result.first->first); | ||
} | ||
|
||
// Otherwise we are doing something wrong | ||
assert(this->order.size() == this->data.size()); | ||
} | ||
|
||
auto Cache::remove_oldest() -> void { | ||
assert(!this->data.empty()); | ||
// std::map are by definition ordered by key, | ||
// so the begin iterator points to the entry | ||
// with the lowest offset, a.k.a. the oldest. | ||
const auto iterator{this->order.cbegin()}; | ||
this->byte_size -= iterator->second.get().first.size(); | ||
this->data.erase(iterator->second.get()); | ||
this->order.erase(iterator); | ||
} | ||
|
||
auto Cache::find(const sourcemeta::jsontoolkit::JSON::String &value, | ||
const Type type) const -> std::optional<std::uint64_t> { | ||
const auto result{this->data.find(std::make_pair(value, type))}; | ||
if (result == this->data.cend()) { | ||
return std::nullopt; | ||
} | ||
|
||
return result->second; | ||
} | ||
|
||
} // namespace sourcemeta::jsonbinpack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/runtime/include/sourcemeta/jsonbinpack/runtime_encoder_cache.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#ifndef SOURCEMETA_JSONBINPACK_RUNTIME_ENCODER_CACHE_H_ | ||
#define SOURCEMETA_JSONBINPACK_RUNTIME_ENCODER_CACHE_H_ | ||
#ifndef DOXYGEN | ||
|
||
#include "runtime_export.h" | ||
|
||
#include <sourcemeta/jsontoolkit/json.h> | ||
|
||
#include <functional> // std::reference_wrapper | ||
#include <map> // std::map | ||
#include <optional> // std::optional | ||
#include <utility> // std::pair | ||
|
||
namespace sourcemeta::jsonbinpack { | ||
|
||
class SOURCEMETA_JSONBINPACK_RUNTIME_EXPORT Cache { | ||
public: | ||
enum class Type { Standalone, PrefixLengthVarintPlusOne }; | ||
auto record(const sourcemeta::jsontoolkit::JSON::String &value, | ||
const std::uint64_t offset, const Type type) -> void; | ||
auto remove_oldest() -> void; | ||
auto find(const sourcemeta::jsontoolkit::JSON::String &value, | ||
const Type type) const -> std::optional<std::uint64_t>; | ||
|
||
private: | ||
// Exporting symbols that depends on the standard C++ library is considered | ||
// safe. | ||
// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN | ||
#if defined(_MSC_VER) | ||
#pragma warning(disable : 4251 4275) | ||
#endif | ||
std::uint64_t byte_size{0}; | ||
using Entry = std::pair<sourcemeta::jsontoolkit::JSON::String, Type>; | ||
std::map<Entry, std::uint64_t> data; | ||
std::map<std::uint64_t, std::reference_wrapper<const Entry>> order; | ||
#if defined(_MSC_VER) | ||
#pragma warning(default : 4251 4275) | ||
#endif | ||
}; | ||
|
||
} // namespace sourcemeta::jsonbinpack | ||
|
||
#endif | ||
#endif |
120 changes: 0 additions & 120 deletions
120
src/runtime/include/sourcemeta/jsonbinpack/runtime_encoder_context.h
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.