Skip to content

Keep chars_format::javascript out of the common hot path - #410

Merged
lemire merged 1 commit into
fastfloat:mainfrom
Algunenano:javascript-fmt-hot-path
Sep 21, 2026
Merged

lemire merged 1 commit into
fastfloat:mainfrom
Algunenano:javascript-fmt-hot-path

Conversation

@Algunenano

Copy link
Copy Markdown
Contributor

chars_format::javascript, new in 8.3.0, adds its ECMAScript leading-zero rule as a runtime test on fmt inside parse_number_string. Unlike basic_json_fmt, which is a template parameter and compiles away, this one cannot: its two error returns and the Annex B octal scan land in the parser body that every from_chars instantiation force-inlines, and fmt has to stay live across the integer scan.

This turned up while reviewing the v8.3.0 bump in ClickHouse (ClickHouse/ClickHouse#121383), which only ever passes chars_format::general. The bump alone cost about 3% on text float parsing.

The fix makes the rule a compile-time parameter of the parser body. parse_number_string keeps its signature and behaviour: it tests the format once and routes to a cold, never-inlined javascript instantiation, so callers that use it directly are unaffected. from_chars_float_advanced leaves for a cold from_chars_float_javascript as its first statement, before anything else is live, so it compiles to a tail call. Placement matters here: any later, and options stays live across the parse and the hot frame picks up two extra spill pairs, which costs as much as the original problem.

Measurements

Intel Xeon 6975P-C (Granite Rapids), clang 22.1.8.

ClickHouse, instructions in from_chars_caller<double>::call: 935 on v8.2.10, 1031 on v8.3.0, 944 on this branch.

ClickHouse, parsing a 20M row text column of Float64, perf stat for the whole run:

instructions cycles
v8.2.10 16.295G 3.179G
v8.3.0 16.437G 3.274G
this branch 16.354G 3.182G

benchmarks/benchmark.cpp, cycles per float, best of 7:

canada.txt mesh.txt
v8.2.10 47.03 24.78
v8.3.0 47.34 26.08
this branch 46.28 24.86

ctest is green with clang 22 and with gcc in C++20 mode. clang-format-17 --style=file reports no changes.

8.3.0 added the ECMAScript leading-zero rule as a runtime test on fmt inside
parse_number_string, so unlike basic_json_fmt its error returns and Annex B
octal scan land in the parser body that every from_chars instantiation
force-inlines. from_chars_caller<double>::call grew from 935 to 1031
instructions and other formats lost about 3% on short inputs.

Make the rule a compile-time parameter of the parser body, selected once at the
entry point, and take chars_format::javascript out to a cold conversion before
anything else is live so the branch is a tail call. parse_number_string keeps
its signature and behaviour.

benchmarks/benchmark.cpp, Intel Xeon 6975P-C, clang 22.1.8, cycles per float,
best of 7:

              canada.txt    mesh.txt
  8.2.10         47.03        24.78
  8.3.0          47.34        26.08
  this           46.28        24.86
@lemire

lemire commented Sep 21, 2026

Copy link
Copy Markdown
Member

@Algunenano This looks good, running tests, and then I will merge.

@lemire
lemire merged commit 6373592 into fastfloat:main Sep 21, 2026
29 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants