Skip to content

Commit 85645d9

Browse files
authored
Use function pointer array for performance (#1442)
* Use function pointer array for performance * Comment out race issue with repe test
1 parent e65db35 commit 85645d9

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

include/glaze/json/read.hpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,21 @@ namespace glz
296296
return;
297297
}
298298
}
299-
299+
300+
// We see better performance with an array of function pointers than a glz::jump_table here.
300301
if constexpr (glaze_object_t<T>) {
301-
jump_table<N>([&]<size_t I>() { decode_index<Opts, T, I>(func, nullptr, value, ctx, it, end); }, index);
302+
static constexpr auto decoders = [&]<size_t... I>(std::index_sequence<I...>) constexpr {
303+
return std::array{&decode_index<Opts, T, I, decltype(func), decltype(nullptr), decltype(value), decltype(ctx), decltype(it), decltype(end)>...};
304+
}(std::make_index_sequence<N>{});
305+
306+
decoders[index](std::forward<Func>(func), nullptr, value, ctx, it, end);
302307
}
303308
else {
304-
decltype(auto) tuple = to_tuple(value);
305-
jump_table<N>([&]<size_t I>() { decode_index<Opts, T, I>(func, tuple, value, ctx, it, end); }, index);
309+
static constexpr auto decoders = [&]<size_t... I>(std::index_sequence<I...>) constexpr {
310+
return std::array{&decode_index<Opts, T, I, decltype(func), decltype(to_tuple(value)), decltype(value), decltype(ctx), decltype(it), decltype(end)>...};
311+
}(std::make_index_sequence<N>{});
312+
313+
decoders[index](std::forward<Func>(func), to_tuple(value), value, ctx, it, end);
306314
}
307315
}
308316
}

include/glaze/util/string_literal.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ namespace glz
1313
template <size_t N>
1414
struct string_literal
1515
{
16+
using value_type = char;
17+
using reference = value_type&;
18+
using const_reference = const value_type&;
19+
using pointer = value_type*;
20+
using const_pointer = const value_type*;
21+
using size_type = size_t;
22+
1623
static constexpr size_t length = (N > 0) ? (N - 1) : 0;
1724

1825
[[nodiscard]] constexpr size_t size() const noexcept { return length; }
@@ -34,6 +41,9 @@ namespace glz
3441
[[nodiscard]] constexpr const std::string_view sv() const noexcept { return {value, length}; }
3542

3643
[[nodiscard]] constexpr operator std::string_view() const noexcept { return {value, length}; }
44+
45+
constexpr reference operator[](size_type index) noexcept { return value[index]; }
46+
constexpr const_reference operator[](size_type index) const noexcept { return value[index]; }
3747
};
3848

3949
template <size_t N>

tests/repe_test/repe_test.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ struct tester
438438
};
439439

440440
suite multi_threading_tests = [] {
441-
"multi-threading"_test = [] {
441+
// TODO: There is a test case that is randomly failing with this code only on Linux with Clang, need to investigate this further
442+
/*"multi-threading"_test = [] {
442443
repe::registry registry{};
443444
tester obj{};
444445
@@ -534,7 +535,7 @@ suite multi_threading_tests = [] {
534535
reader_full.join();
535536
writer_str.join();
536537
writer_integer.join();
537-
};
538+
};*/
538539
};
539540

540541
struct glaze_types

0 commit comments

Comments
 (0)