Skip to content

Research: can fast_float be made faster for V8's use? #435

Open

Description

@lemire

@LeszekSwirski suggested in #434 (comment) a not_short_numbers performance hint for callers, like V8, that have already handled short numbers in an inline path that writes to an int32.

This issue is to research whether, and how, fast_float can be made faster for V8's use.

What V8 hands to fast_float

If I understand correctly, V8 first parses the strings that are integers fitting in an int32 (e.g., 42, -1024). fast_float gets the rest: decimals (-65.61, 0.1), numbers with an exponent (1e3), and integers that do not fit in an int32 (1731000000000).

Note that excluding short integers does not mean that the integer parts are long: -65.61 has a two-digit integer part.

A first experiment: scanning the integer part eight digits at a time

I tried a hint (not_short_integers) that makes the parser read the integer part eight digits at a time (SWAR) from the start, then fall back to the usual digit-by-digit scan. Results are unchanged for all inputs, short integers included.

Apple M4, fastfloat (64), change in MB/s with the hint, best of three. The canada and mesh rows have their int32 integers removed, as V8 would.

Data ASCII UTF-16
13-digit timestamps +27% +14%
integers beyond int32 +26% +26%
mix of timestamps, decimals and long integers +9% +5%
canada without int32 integers −9% −4%
mesh without int32 integers −10% −5%
17-digit doubles −1% −5%

It helps when the integer parts are long and costs up to about 10% when they are short, which is presumably the common case in V8.

Two variants were worse:

  • Eight digits at a time, then a plain digit loop: −7 to −13% on decimals.
  • Branch-free counting of the leading digits from one 8-byte load: −20% on canada. The pointer advance then depends on a chain of multiplications, while the branches of the usual scan are well predicted on regular data.

So, as such, the hint does not look like a win for V8.

Open questions

  • What does the distribution of the strings that reach fast_float in V8 look like (length, length of the integer part, decimals versus long integers, Latin-1 versus UTF-16)? A sample, or even rough statistics, would let us benchmark the right thing.
  • Is there something else that V8 knows about its inputs and that fast_float could exploit? For example: the string is already trimmed, the whole range must be consumed, the format is fixed (no inf/nan)...
  • Would it help if fast_float could take over V8's int32 path (one call returning either an int32 or a double), so that short integers are not scanned twice when the inline path fails?
  • Is the cost of the call itself (e.g., the parser not being inlined) significant on V8's side?

Activity

  1. LeszekSwirski commented on Nov 18, 2026

    @LeszekSwirski
    Collaborator

    Thanks for testing! V8's current benchmarking focuses on Speedometer 3.1 and JetStream 3.0, the numbers there match your suspicion that integer parts are mostly short.

    For context, fast_float is used in three paths in V8:

    • JSON.parse (chars_format::json). V8 first scans "0", "-0", and 1-9 digit signed/unsigned integers inline into a 31-bit Smi (using two 4-digit SWAR steps on Latin-1), leaving decimals, exponents, and >= 10-digit integers to fast_float.
    • InternalStringToDouble (chars_format::general | no_infnan | allow_leading_plus) which mirrors the spec StringNumericValue operation (https://tc39.es/ecma262/#sec-runtime-semantics-stringnumericvalue) and is itself used in three paths:
      • JS lexing, which needs to additionally pre-scan for non-decimal prefixes and strip _ separators -- our current implementation already pre-scans for short integers in the tokenizer and doesn't do the fast_float parse of doubles until the parsing, but this could be changed. Since it's the tokenizer, we know that there's no leading whitespace.
      • String::ToNumber, for things like Number(str) or +str, which also has to support non-decimal prefixes but not _ separators, and does need to trim whitespace. This one always returns a double, but then we test to see if that double is in Smi range (int31 range) for the value we return to the user so maybe a call returning an int32 could be faster here.
      • parseFloat, which doesn't allow non-decimal prefixes, but does allow both leading whitespace and trailing characters.
    • parseInt (chars_format::fixed | no_infnan with decimal_point = '0'). This also has to strip leading whitespace and check for non-decimal prefixes, then it uses decimal_point = '0' as a trick so it stops at any non-digit without matching a fraction.

    Additionally, our internalized (i.e. canonicalized) strings check, as part of their internalization/hashing, whether they are "index-like" (i.e. an unsigned integer), and cache that as their hash. Anything that operates on an existing string (so anything that's not JSON.parse/JS lexing) checks for this bit first and fast path returns the cached unsigned integer value if it's there.

    To your questions (forgive the remnants of slightly LLM-y text, the prose is human written but the measurement data gets a bit dense):

    What does the distribution of the strings that reach fast_float in V8 look like (length, length of the integer part, decimals versus long integers, Latin-1 versus UTF-16)?

    Almost 100% Latin-1 (100% in Speedometer 3, 98.9% in JetStream 3; 0% UTF-16 in JSON.parse, the lexer, and parseInt). By call volume, ToNumber is the biggest caller (73% of 316k calls in SP3; 56% of 2.87M calls in JS3), followed by JSON.parse (23% SP3, 17% JS3), parseInt (2% SP3, 11% JS3), the JS lexer (1% SP3, 9% JS3), and parseFloat (0.1% SP3, 8% JS3). Because ToNumber calls lack an inline digit loop if they miss the array-index fast path, 1-9 digit integers are still 38% of all SP3 calls (mostly CSV fields in Charts-chartjs) and 21% of JS3 calls, while in JS3 ToNumber (pdfjs, proxy-mobx) 83% of calls (46% of all JS3 fast_float calls) are non-numeric strings rejected on char 0.

    For valid numbers reaching fast_float, integer parts are < 8 digits in 77% of SP3 calls and 93% of JS3 calls (mostly 1-4 digits). Looking only at the JSON.parse numbers that miss the inline Smi scan and reach fast_float, SP3 (Perf-Dashboard) is 90% 13-digit timestamps and 10% decimals, whereas JS3 (478k calls, 95% of them in json-parse-inspector and 5% in ML) is 100% decimals (477,534 / 477,537), ~98% of which are 16-20 char decimals like 19.418373880995205 (1-2 digit integer part, 14-16 digit fraction). About 80-97% of valid calls hit Clinger's fast path, the rest hit Eisel-Lemire, and < 0.01% exceed 19 digits. Probably this exact JS3 json-parse-inspector number format is overrepresented in these benchmarks.

    Is there something else that V8 knows about its inputs and that fast_float could exploit?

    Currently, leading whitespace is stripped outside of fast_float since we have to do it anyway for the non-decimal prefix detection. I think we also therefore know we don't have a leading 0 for an integer, and all three V8 call sites already pass no_infnan because we have a different syntax of Infinity/NaN that we detect on the fast_float failure path. Only the JS lexer consumes the full range (because it's consuming a token that we've already tokenized), and maybe it could be better to already have fast_float run in the tokenizer and scan up to the end of the number. Otherwise, JSON.parse passes the end of the whole JSON buffer, parseInt/parseFloat allow trailing junk ("16px"), and ToNumber allows trailing whitespace.

    For JSON and lexing, we could also know whether we have a leading digit, since we're dispatching on that character anyway.

    Would it help if fast_float could take over V8's int32 path (one call returning either an int32 or a double), so that short integers are not scanned twice when the inline path fails?

    Preliminary measurements on my machine say "yes", if the 1-9 digit int32 fast path is always_inline (with SWAR) and hands the already-scanned prefix (cursor, prefix_val, digit_count, negative) to an out-of-line float/long-int continuation without rewinding:

    • In InternalStringToDouble (ToNumber / parseFloat) and parseInt, where V8 currently has no inline digit loop and round-trips short integers through double and DoubleToSmiInteger, an inline int32 | double fast path is ~11% faster on Speedometer 3's ToNumber corpus and ~22% faster on JetStream 3's ToNumber corpus.
    • In JSON.parse, it is ~11% faster on Speedometer 3's non-Smi JSON numbers (~5% across an 80%-Smi / 20%-timestamp mix, since 13-digit timestamps don't re-scan their first 9 digits) and roughly neutral (~2% slower) on JetStream 3's 84%-Smi / 16%-decimal mix (where 19.418373880995205 only had 2 integer digits to re-scan anyway; V8's JSON object loop dispatches directly on switch (*cursor) for '0', '1'..'9', '-', etc., so a standalone helper wouldn't fold into that switch unless V8 calls a "resume after prefix" entry point on fallback).
    • In the JS lexer, V8 still couldn't use it directly because the needs to strip _ separators and check for a BigInt n suffix. We could still use it on the post-processed string though, I haven't measured this yet.

    Is the cost of the call itself (e.g., the parser not being inlined) significant on V8's side?

    Because V8 calls from_chars_advanced (which is fastfloat_really_inline), fast_float's hot path is already inlined into V8. Keeping from_chars_advanced always_inline on Clang definitely matters (marking it noinline regresses our corpora by 7% on JSON.parse and 23-64% on ToNumber where fast 1st-char rejections dominate), while V8 keeps its JSON fallback wrapper out-of-line so it doesn't bloat the inline Smi loop. Overall fast_float is ~1.4% of cycles in json-parse-inspector and < 0.2% elsewhere in JS3/SP3, so wins mainly show up in JSON/CSV workloads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions