Skip to content

Speed up precise float text parsing (bump fast_float to v8.2.10) - #108205

Merged
Algunenano merged 10 commits into
ClickHouse:masterfrom
Algunenano:bump-fast_float-v8.2.10
Jul 1, 2026
Merged

Algunenano merged 10 commits into
ClickHouse:masterfrom
Algunenano:bump-fast_float-v8.2.10

Conversation

@Algunenano

@Algunenano Algunenano commented Jun 22, 2026 •

Copy link
Copy Markdown
Member

Bumps fast_float from v8.2.3 to v8.2.10 and reworks the precise float text
parser (precise_float_parsing = 1) so it is as fast as, or faster than, the
default (fast) parser on essentially every input.

Main changes:

  • Move readFloatTextPreciseImpl/readFloatTextFastImpl and the heavy
    <fast_float/fast_float.h> include out of readFloatText.h into the .cpp,
    so touching the parsers no longer rebuilds the world.
  • Replace the per-value dynamic_cast used to detect a memory buffer with a
    virtual isMemoryBuffer, taking the no-copy path without RTTI.
  • Parse integers without fast_float's re-scan: short numbers via from_chars;
    up to 38 digits via an unsigned __int128 SWAR accumulator with a
    correctly-rounded conversion; longer integers by building the parsed-number
    struct directly with wide reads instead of fast_float's per-digit
    too_many_digits re-parse.
  • Dispatch by numeric-token length, not the whole remaining buffer; a ., e
    or E sends the token straight to from_chars (the integer fast paths cannot
    apply to a fraction/exponent).
  • fast_float source is left pristine (no fork); the long-integer path is built
    on top of its existing primitives.

Benchmarks

ThinLTO build, 16M strings per shape, Float64, parse cost isolated by
subtracting a sum(length(s)) baseline; median ms, lower is better.

Precise parser, master → this branch:

shape master branch
rand64 (20-digit integers) 82 62 −24%
longdigits (60-digit integers) 171 108 −37%
int_big 42 36 −14%
int_small 29 26 −10%
rand32 44 40 −9%
exp 60 55 −8%
ratio 58 54 −7%
frac (long fractions) 77 87 +13%

Large gains on long integers; the lone regression is long fractions (see below).

With this, the precise parser is faster than the default fast parser on every
shape except frac (local non-LTO run, fast vs precise ms):

shape fast precise winner
int_small 31 27 precise
int_big 43 35 precise
rand32 49 42 precise
rand64 71 61 precise
exp 61 55 precise
ratio 84 52 precise
longdigits 117 108 precise
frac 83 88 fast 1.06×

frac is ~19-20 significant-digit fractions, exactly fast_float's
too_many_digits boundary. The fast path wins there (~1.06× on x86, ~1.32× on
Darwin/ARM) only because it truncates the mantissa (~1 ULP off) while the precise
path rounds correctly; that cost is inherent at this digit count, and no dispatch
change closes it without giving up correct rounding.

The fast (default) path is kept at parity with master: an experiment that shared
the precise SWAR digit loop into it regressed short integers and ThinLTO did not
inline it back, so it was reverted. CI performance tests confirm that x86 results
remain stable, while ARM results see a slight improvement (around 10% in
random numbers).

Follow-up: with precise ahead on every shape but the inherent frac case, a
later PR may enable precise_float_parsing by default.

04402_float_parsing_corner_cases is muted on Darwin: the fast parser's accuracy
tracks long double width (64-bit on Darwin/ARM, where it equals double, vs
80/128-bit on Linux), so a Linux-generated reference cannot match there.

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Speed up parsing of floating-point numbers from text with precise_float_parsing = 1, making it as fast as or faster than the default parser on almost all inputs. Also bumps the bundled fast_float library to v8.2.10.

Version info

  • Merged into: 26.7.1.359 (included in 26.7 and later)

Algunenano and others added 5 commits June 22, 2026 18:04
Header-only library; updates the float parser used by the precise
`toFloat*`/`precise_float_parsing` path. v8.2.10 adds integer-parsing
and digit-scan improvements over v8.2.3.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The parsing implementations (precise/fast/simple) and the heavy
<fast_float/fast_float.h> include lived in readFloatText.h, so every
translation unit that only calls these functions recompiled all of it
(and re-parsed fast_float) on any change. Move the implementations and
the fast_float include into readFloatText.cpp, leaving declarations plus
explicit instantiations for BFloat16/Float32/Float64. No behavior change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Precise path (precise_float_parsing=1, the fast_float-based parser):
- Replace the per-value dynamic_cast<ReadBufferFromMemory*> with a cheap
  virtual ReadBuffer::isMemoryBuffer(), avoiding RTTI in the hot loop.
- Dispatch by the input length (known for free), shared by the no-copy
  and copy paths:
    * <= 19 chars        -> fast_float::from_chars (cannot be too_many_digits,
                            so it never takes fast_float's re-parse path);
    * long pure integer  -> accumulate into unsigned __int128 (SWAR, 8 digits
      (<= 38 digits)        at a time) and convert, correctly rounded and far
                            cheaper than fast_float's big-integer path;
    * otherwise          -> fromCharsLong, a single-parse helper built on
                            fast_float's own primitives (no library fork) that
                            parses once with spans materialized, avoiding the
                            >19-significant-digit double parse.

Fast path (precise_float_parsing=0): accumulate 8 digits at a time via
SWAR in readUIntTextUpToNSignificantDigits instead of digit-by-digit.

The precise parser now matches or beats the fast (truncating) parser across
the practical range, including long integers, while staying correctly rounded.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ength

For an integer with more than 38 significant digits (does not fit in unsigned __int128), build
the parsed_number_string_t directly with a lean SWAR scan -- accumulate the first 19 significant
digits, count the rest eight at a time -- and hand it to fast_float::from_chars_advanced. This
avoids fast_float::parse_number_string accumulating every digit and then recomputing the mantissa
from the first 19. Its result is returned as-is, including result_out_of_range (e.g. a >38-digit
value that overflows Float32), instead of re-parsing through the slow path.

Also SWAR the unsigned __int128 accumulation in the <= 38-digit integer path.

Classify short vs long by the numeric token length, not the remaining buffer size: callers such
as parseReadableSize pass a buffer whose end is past the number (e.g. "1.5 GiB"), so a short
token in a larger buffer must still take the fast from_chars path. The selected parser still
receives the real buffer end and consumes the whole token.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Around 120 boundary and corner inputs (zero/sign forms, dots, scientific notation, round-to-even
ties near 2^52/2^53, full double range incl. subnormals/overflow/underflow, Float32 range and 2^24
ties, long integers crossing the 19- and 38-significant-digit dispatch boundaries, long fractions,
leading zeros, whitespace, malformed input, and inf/nan spellings) parsed as Float32 and Float64
under both precise_float_parsing settings.

Covers far more than the existing 02813_float_parsing.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d801c4d]

Summary: ❌

job_name test_name status info comment
Stress test (arm_tsan) FAIL
Hung check failed, possible deadlock found FAIL cidb, issue

AI Review

Summary

This PR bumps fast_float and moves/reworks the precise float text parser so precise_float_parsing = 1 avoids the previous long-integer re-scan while keeping the default fast parser path at parity. I did not find unresolved correctness, safety, or performance issues in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements submodule changed At least one submodule changed in this PR. labels Jun 22, 2026
Comment thread src/IO/readFloatText.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

This PR rewrites the text-to-float parsing hot path (readFloatText) and moves it out of the header — it only affects converting text into floats during ingestion of text formats. ClickBench queries run over a table already loaded in native binary format, so Q4, Q15, and Q28 never exercise float-text parsing at SELECT time. The ~19% deltas on Q4/Q15 and the smaller borderline Q28 shift therefore can't be attributed to this change, and all three are downgraded to not-sure as off-path measurement variance rather than real query-path improvements.

clickbench

⚠️ 3 inconclusive

Flagged queries (3 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 4 not_sure 262 212 -19.1% <0.0001 PR only speeds text->float parsing (ingest path); ClickBench Q4 reads pre-loaded native data, off-path
⚠️ 15 not_sure 245 198 -19.2% <0.0001 Float-text parsing change can't touch this SELECT over native-format columns; -19% is off-path variance
⚠️ 28 not_sure 6671 6286 -5.8% 0.0042 Heavy string/group-by query, no text->float parsing at SELECT time; borderline -5.8% is off-path

q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 9b69789f-68fc-4088-9ad6-d4eea75d52a9
  • MIRAI run: c2617261-b303-46f8-8129-e6fcb329b47a
  • PR check IDs:
    • clickbench_501075_1782829821
    • clickbench_501089_1782829821
    • clickbench_501095_1782829821
    • tpch_adapted_1_official_501102_1782829821
    • tpch_adapted_1_official_501125_1782829821

Moving the parsers into the .cpp exposed clang-tidy findings the header
instantiations did not surface:
- Extend the existing `clang-analyzer-*` NOLINT region to the whole file;
  the analyzer reaches `fast_float`'s `chars_format` flag-enum operators
  through the parse call sites, not just the include (same suppression as
  `IO/examples/read_float_perf.cpp`).
- Drop the redundant `static` on two anonymous-namespace constexprs.
- Initialize the SWAR `chunk`/`val` reads and value-initialize the three
  `from_chars_result_t` locals.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@Algunenano Algunenano self-assigned this Jun 23, 2026
@Algunenano

Copy link
Copy Markdown
Member Author

Fast test (arm_darwin)

This is exactly what lead me into this improvement. The fast algorithm is less less precise in Darwin + ARM than in Linux versions because long double is equal to double both (64b). In Linux x86 it's 80bit, and IIRC Linux ARM is 120 bit. I'll mute this test for Darwin. Hopefully if precise becomes faster than fast, then we can just ignore this and use precise by default

@Algunenano

Copy link
Copy Markdown
Member Author
  • For fast parsing:
    No diff in performance in AMD:
image

Slight improvement in ARM:
image

  • Precise parsing. Does not run in CI, but numbers provided in the PR description

@Algunenano Algunenano removed their assignment Jun 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.40% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.60% 77.70% +0.10%

Changed lines: Changed C/C++ lines covered: 245/403 (60.79%) · Uncovered code

Full report · Diff report

@Algunenano
Algunenano added this pull request to the merge queue Jul 1, 2026
Merged via the queue into ClickHouse:master with commit 1a36e54 Jul 1, 2026
172 of 174 checks passed
@Algunenano
Algunenano deleted the bump-fast_float-v8.2.10 branch July 1, 2026 17:23
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 1, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…v8.2.10

Speed up precise float text parsing (bump `fast_float` to v8.2.10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants