Skip to content

Commit

Permalink
Using new hash approach in mustache (#1306)
Browse files Browse the repository at this point in the history
* Using new hash approach in mustache

* renamed stencil_test to mustache_test
  • Loading branch information
stephenberry committed Sep 17, 2024
1 parent eecdf5c commit 23a76d4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
77 changes: 38 additions & 39 deletions include/glaze/mustache/mustache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ namespace glz
return unexpected(error_ctx{ctx.error, ctx.custom_error_message, 0, ctx.includer_error});
}

auto [it, end] = read_iterators<Opts, false>(ctx, tmp);
auto p = read_iterators<Opts, false>(ctx, tmp);
auto it = p.first;
auto end = p.second;
auto start = it;
if (not bool(ctx.error)) [[likely]] {
auto skip_whitespace = [&] {
Expand All @@ -44,48 +46,45 @@ namespace glz
}

sv key{s, size_t(it - s)};

static constexpr auto num_members = refl<T>.N;

decltype(auto) frozen_map = [&]() -> decltype(auto) {
using V = decay_keep_volatile_t<decltype(value)>;
if constexpr (detail::reflectable<T> && num_members > 0) {
#if ((defined _MSC_VER) && (!defined __clang__))
static thread_local auto cmap = detail::make_map<V, Opts.use_hash_comparison>();
#else
static thread_local constinit auto cmap = detail::make_map<V, Opts.use_hash_comparison>();
#endif
// We want to run this populate outside of the while loop
populate_map(value, cmap); // Function required for MSVC to build
return cmap;
}
else if constexpr (detail::glaze_object_t<T> && num_members > 0) {
static constexpr auto cmap = detail::make_map<T, Opts.use_hash_comparison>();
return cmap;
}
else {
return nullptr;
}
}();

if (const auto& member_it = frozen_map.find(key); member_it != frozen_map.end()) [[likely]] {
std::visit(
[&](auto&& member_ptr) {
static thread_local std::string temp{};
std::ignore =
write<opt_true<Opts, &opts::raw>>(detail::get_member(value, member_ptr), temp, ctx);
result.append(temp);
},
member_it->second);
if (bool(ctx.error)) [[unlikely]]
return unexpected(
error_ctx{ctx.error, ctx.custom_error_message, size_t(it - start), ctx.includer_error});
}
else {

static constexpr auto N = refl<T>.N;
static constexpr auto HashInfo = detail::hash_info<T>;

const auto index =
detail::decode_hash<T, HashInfo, HashInfo.type>::op(key.data(), key.data() + key.size());

if (index >= N) [[unlikely]] {
ctx.error = error_code::unknown_key;
return unexpected(
error_ctx{ctx.error, ctx.custom_error_message, size_t(it - start), ctx.includer_error});
}
else {
static thread_local std::string temp{};
detail::jump_table<N>([&]<size_t I>() {
static constexpr auto TargetKey = get<I>(refl<T>.keys);
static constexpr auto Length = TargetKey.size();
if (((s + Length) < end) && compare<Length>(TargetKey.data(), s)) [[likely]] {
if constexpr (detail::reflectable<T> && N > 0) {
std::ignore =
write<opt_true<Opts, &opts::raw>>(detail::get_member(value, get<I>(detail::to_tuple(value))), temp, ctx);
}
else if constexpr (detail::glaze_object_t<T> && N > 0) {
std::ignore =
write<opt_true<Opts, &opts::raw>>(detail::get_member(value, get<I>(refl<T>.values)), temp, ctx);
}
}
else {
ctx.error = error_code::unknown_key;
}
}, index);

if (bool(ctx.error)) [[unlikely]] {
return unexpected(
error_ctx{ctx.error, ctx.custom_error_message, size_t(it - start), ctx.includer_error});
}

result.append(temp);
}

skip_whitespace();

Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ add_subdirectory(json_test)
add_subdirectory(jsonrpc_test)
add_subdirectory(lib_test)
add_subdirectory(mock_json_test)
add_subdirectory(stencil_test)
add_subdirectory(mustache_test)
add_subdirectory(reflection_test)
add_subdirectory(repe_test)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project(stencil_test)
project(mustache_test)

add_compile_definitions(CURRENT_DIRECTORY="${CMAKE_CURRENT_SOURCE_DIR}")

Expand Down
File renamed without changes.

0 comments on commit 23a76d4

Please sign in to comment.