Skip to content

Commit

Permalink
Support optimization for longer key length differences (#241)
Browse files Browse the repository at this point in the history
* Support longer key length differences

* zero out chunk
  • Loading branch information
stephenberry authored Mar 30, 2023
1 parent feb757a commit ce32f8e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/glaze/json/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ namespace glz
}
else {
static constexpr auto stats = key_stats<T, tag>();
if constexpr (stats.length_range < 8 && Opts.error_on_unknown_keys) {
if constexpr (stats.length_range < 16 && Opts.error_on_unknown_keys) {
if ((it + stats.max_length) < end) [[likely]] {
if constexpr (stats.length_range == 0) {
const sv key{it, stats.max_length};
Expand Down
34 changes: 30 additions & 4 deletions include/glaze/util/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,26 +280,52 @@ namespace glz::detail
static_assert(std::contiguous_iterator<std::decay_t<decltype(it)>>);

auto start = it;
it += MinLength;
it += MinLength; // immediately skip minimum length

static_assert(LengthRange < 8);
static_assert(LengthRange < 16);
if constexpr (LengthRange == 7) {
uint64_t chunk; // no need to default initialize
std::memcpy(&chunk, it, LengthRange + 1);
std::memcpy(&chunk, it, 8);
const uint64_t test_chunk = has_quote(chunk);
if (test_chunk != 0) [[likely]] {
it += (std::countr_zero(test_chunk) >> 3);

sv ret{start, static_cast<size_t>(it - start)};
++it;
return ret;
}
}
else if constexpr (LengthRange > 7) {
uint64_t chunk; // no need to default initialize
std::memcpy(&chunk, it, 8);
uint64_t test_chunk = has_quote(chunk);
if (test_chunk != 0) {
it += (std::countr_zero(test_chunk) >> 3);

sv ret{start, static_cast<size_t>(it - start)};
++it;
return ret;
}
else {
it += 8;
static constexpr auto rest = LengthRange + 1 - 8;
chunk = 0; // must zero out the chunk
std::memcpy(&chunk, it, rest);
test_chunk = has_quote(chunk);
if (test_chunk != 0) {
it += (std::countr_zero(test_chunk) >> 3);

sv ret{start, static_cast<size_t>(it - start)};
++it;
return ret;
}
}
}
else {
uint64_t chunk{};
std::memcpy(&chunk, it, LengthRange + 1);
const uint64_t test_chunk = has_quote(chunk);
if (test_chunk != 0) {
if (test_chunk != 0) [[likely]] {
it += (std::countr_zero(test_chunk) >> 3);

sv ret{start, static_cast<size_t>(it - start)};
Expand Down

0 comments on commit ce32f8e

Please sign in to comment.