Compare commits

...

3 Commits

Author SHA1 Message Date
Francisco Geiman Thiesen 2887e7432c Fix wrong branch hint and add missing consteval guard in extract_from (#2609)
Two fixes to the ablation logic:

1. Fix incorrect branch hint in capacity_check() overflow detection
   (json_string_builder-inl.h:365): simdjson_likely was used on the
   overflow check `position + upcoming_bytes < position`, but overflow
   is rare so this should be simdjson_unlikely. The comment itself says
   "most of the time there is no overflow". This was causing the branch
   predictor to optimize for the wrong path.

2. Add missing ABLATION_NO_CONSTEVAL guard in extract_from()
   (json_builder.h:332): consteval_to_quoted_escaped was being used
   without an ablation guard, for consistency with all other call sites.

Updated ablation results with both fixes:
| Configuration    | CITM (MB/s) | Impact | Twitter (MB/s) | Impact |
|------------------|-------------|--------|----------------|--------|
| Baseline         | 3001.10     | -      | 5651.89        | -      |
| NO_BRANCH_HINTS  | 2443.67     | -19%   | 4651.78        | -18%   |
| NO_SIMD_ESCAPING | 2904.34     | -3%    | 1403.36        | -75%   |
| NO_CONSTEVAL     | 1796.05     | -40%   | 3542.77        | -37%   |
2026-02-20 14:45:02 -05:00
Francisco Geiman Thiesen 764a6a0bb6 Fix incomplete ABLATION_NO_CONSTEVAL guards in json_builder.h (#2607)
The ablation study was showing inconsistent results for the consteval
optimization (-9% CITM, -1% Twitter) because the ABLATION_NO_CONSTEVAL
guards were missing from two critical code paths:

1. atom() for structs (line 94-102): The consteval optimization for
   pre-computing escaped/quoted field names was not being disabled.

2. atom() for enums (line 136-150): The consteval optimization for
   pre-computing escaped/quoted enum string values was not being disabled.

With these guards added, the ablation study now correctly shows the full
impact of the consteval optimization:
- CITM: -47% (was -9%)
- Twitter: -38% (was -1%)

This confirms the consteval optimization provides ~2x performance
improvement by pre-computing escaped/quoted strings at compile time
via std::define_static_string and consteval_to_quoted_escaped.
2026-02-19 20:45:15 -05:00
Daniel Lemire 135eeecb40 simplified ablation 2026-02-18 21:12:28 -05:00
4 changed files with 98 additions and 2 deletions
Executable
+23
View File
@@ -0,0 +1,23 @@
CXX=clang++ cmake -B buildreflect -D SIMDJSON_STATIC_REFLECTION=ON -DSIMDJSON_DEVELOPER_MODE=ON -DSIMDJSON_ABLATION_NO_BRANCH_HINTS=ON
cmake --build buildreflect --target benchmark_serialization_citm_catalog benchmark_serialization_twitter
./buildreflect/benchmark/static_reflect/citm_catalog_benchmark/benchmark_serialization_citm_catalog
./buildreflect/benchmark/static_reflect/twitter_benchmark/benchmark_serialization_twitter
CXX=clang++ cmake -B buildreflecthints -D SIMDJSON_STATIC_REFLECTION=ON -DSIMDJSON_DEVELOPER_MODE=ON -DSIMDJSON_ABLATION_NO_BRANCH_HINTS=ON
cmake --build buildreflecthints --target benchmark_serialization_citm_catalog benchmark_serialization_twitter
./buildreflecthints/benchmark/static_reflect/citm_catalog_benchmark/benchmark_serialization_citm_catalog
./buildreflecthints/benchmark/static_reflect/twitter_benchmark/benchmark_serialization_twitter
CXX=clang++ cmake -B buildreflectescaping -D SIMDJSON_STATIC_REFLECTION=ON -DSIMDJSON_DEVELOPER_MODE=ON -DSIMDJSON_ABLATION_NO_SIMD_ESCAPING=ON
cmake --build buildreflectescaping --target benchmark_serialization_citm_catalog benchmark_serialization_twitter
./buildreflectescaping/benchmark/static_reflect/citm_catalog_benchmark/benchmark_serialization_citm_catalog
./buildreflectescaping/benchmark/static_reflect/twitter_benchmark/benchmark_serialization_twitter
CXX=clang++ cmake -B buildreflectconsteval -D SIMDJSON_STATIC_REFLECTION=ON -DSIMDJSON_DEVELOPER_MODE=ON -DSIMDJSON_ABLATION_NO_CONSTEVAL=ON
cmake --build buildreflectconsteval --target benchmark_serialization_citm_catalog benchmark_serialization_twitter
./buildreflectconsteval/benchmark/static_reflect/citm_catalog_benchmark/benchmark_serialization_citm_catalog
./buildreflectconsteval/benchmark/static_reflect/twitter_benchmark/benchmark_serialization_twitter
+30
View File
@@ -240,6 +240,36 @@ else()
add_compile_definitions(SIMDJSON_UTF8VALIDATION=1)
endif()
option(
SIMDJSON_ABLATION_NO_BRANCH_HINTS
"Disable branch hints for ablation testing."
OFF
)
if(SIMDJSON_ABLATION_NO_BRANCH_HINTS)
add_compile_definitions(ABLATION_NO_BRANCH_HINTS=1)
message(STATUS "ABLATION_NO_BRANCH_HINTS enabled")
endif()
option(
SIMDJSON_ABLATION_NO_SIMD_ESCAPING
"Disable SIMD escaping for ablation testing."
OFF
)
if(SIMDJSON_ABLATION_NO_SIMD_ESCAPING)
add_compile_definitions(ABLATION_NO_SIMD_ESCAPING=1)
message(STATUS "ABLATION_NO_SIMD_ESCAPING enabled")
endif()
option(
SIMDJSON_ABLATION_NO_CONSTEVAL
"Disable consteval for ablation testing."
OFF
)
if(SIMDJSON_ABLATION_NO_CONSTEVAL)
add_compile_definitions(ABLATION_NO_CONSTEVAL=1)
message(STATUS "ABLATION_NO_CONSTEVAL enabled")
endif()
include(CheckSymbolExists)
check_symbol_exists(fork unistd.h HAVE_POSIX_FORK)
check_symbol_exists(wait sys/wait.h HAVE_POSIX_WAIT)
@@ -94,8 +94,12 @@ constexpr void atom(string_builder &b, const T &t) {
template for (constexpr auto dm : std::define_static_array(std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::unchecked()))) {
if (i != 0)
b.append(',');
#if ABLATION_NO_CONSTEVAL
b.escape_and_append_with_quotes(std::meta::identifier_of(dm));
#else
constexpr auto key = std::define_static_string(constevalutil::consteval_to_quoted_escaped(std::meta::identifier_of(dm)));
b.append_raw(key);
#endif
b.append(':');
atom(b, t.[:dm:]);
i++;
@@ -132,9 +136,13 @@ void atom(string_builder &b, const T &e) {
#if SIMDJSON_STATIC_REFLECTION
constexpr auto enumerators = std::define_static_array(std::meta::enumerators_of(^^T));
template for (constexpr auto enum_val : enumerators) {
constexpr auto enum_str = std::define_static_string(constevalutil::consteval_to_quoted_escaped(std::meta::identifier_of(enum_val)));
if (e == [:enum_val:]) {
#if ABLATION_NO_CONSTEVAL
b.escape_and_append_with_quotes(std::meta::identifier_of(enum_val));
#else
constexpr auto enum_str = std::define_static_string(constevalutil::consteval_to_quoted_escaped(std::meta::identifier_of(enum_val)));
b.append_raw(enum_str);
#endif
return;
}
};
@@ -229,8 +237,12 @@ void append(string_builder &b, const Z &z) {
template for (constexpr auto dm : std::define_static_array(std::meta::nonstatic_data_members_of(^^Z, std::meta::access_context::unchecked()))) {
if (i != 0)
b.append(',');
#if ABLATION_NO_CONSTEVAL
b.escape_and_append_with_quotes(std::meta::identifier_of(dm));
#else
constexpr auto key = std::define_static_string(constevalutil::consteval_to_quoted_escaped(std::meta::identifier_of(dm)));
b.append_raw(key);
#endif
b.append(':');
atom(b, z.[:dm:]);
i++;
@@ -317,8 +329,12 @@ void extract_from(string_builder &b, const T &obj) {
first = false;
// Serialize the key
#if ABLATION_NO_CONSTEVAL
b.escape_and_append_with_quotes(std::meta::identifier_of(mem));
#else
constexpr auto quoted_key = std::define_static_string(constevalutil::consteval_to_quoted_escaped(std::meta::identifier_of(mem)));
b.append_raw(quoted_key);
#endif
b.append(':');
// Serialize the value
@@ -295,9 +295,22 @@ SIMDJSON_CONSTEXPR_LAMBDA simdjson_inline void escape_json_char(char c, char *&o
// written. Uses SIMD position finding to locate quotable characters efficiently.
inline size_t write_string_escaped(const std::string_view input, char *out) {
size_t mysize = input.size();
#if ABLATION_NO_SIMD_ESCAPING
auto naive_find_next_json_quotable_character = [](std::string_view input, size_t start) -> size_t {
for (size_t i = start; i < input.size(); ++i) {
auto c = static_cast<unsigned char>(input[i]);
if (c == '"' || c == '\\' || c < 32) return i;
}
return input.size();
};
#endif
// Use SIMD position finder directly - it returns mysize if no escape needed
#if ABLATION_NO_SIMD_ESCAPING
size_t location = naive_find_next_json_quotable_character(input, 0);
#else
size_t location = find_next_json_quotable_character(input, 0);
#endif
if (location == mysize) {
// Fast path: no escaping needed
memcpy(out, input.data(), input.size());
@@ -310,7 +323,11 @@ inline size_t write_string_escaped(const std::string_view input, char *out) {
escape_json_char(input[location], out);
location += 1;
while (location < mysize) {
#if ABLATION_NO_SIMD_ESCAPING
size_t newlocation = naive_find_next_json_quotable_character(input, location);
#else
size_t newlocation = find_next_json_quotable_character(input, location);
#endif
memcpy(out, input.data() + location, newlocation - location);
out += newlocation - location;
location = newlocation;
@@ -332,13 +349,23 @@ simdjson_inline bool string_builder::capacity_check(size_t upcoming_bytes) {
// We use the convention that when is_valid is false, then the capacity and
// the position are 0.
// Most of the time, this function will return true.
#if ABLATION_NO_BRANCH_HINTS
if (upcoming_bytes <= capacity - position) {
return true;
}
// check for overflow, most of the time there is no overflow
if (position + upcoming_bytes < position) {
return false;
}
#else
if (simdjson_likely(upcoming_bytes <= capacity - position)) {
return true;
}
// check for overflow, most of the time there is no overflow
if (simdjson_likely(position + upcoming_bytes < position)) {
if (simdjson_unlikely(position + upcoming_bytes < position)) {
return false;
}
#endif
// We will rarely get here.
grow_buffer((std::max)(capacity * 2, position + upcoming_bytes));
// If the buffer allocation failed, we set is_valid to false.