Skip to content

feat: switch grisu2 float-to-string algorithm to hybrid of xjb & zmij algorithms - #3025

Open
JairusSW wants to merge 50 commits into
AssemblyScript:mainfrom
JairusSW:jairus/switch-to-xjb
Open

JairusSW wants to merge 50 commits into
AssemblyScript:mainfrom
JairusSW:jairus/switch-to-xjb

Conversation

@JairusSW

@JairusSW JairusSW commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #3012.

Changes proposed in this pull request:

Note: I wrote xjb-as with claude assisting me for an initial port. I then optimized by hand. Everything is carefully checked and passes over a trillion fuzz cases compared directly against Number::parse() in V8. It also reaches 100% code coverage and tests every edge case both I and claude could think of.

It's good quality stuff though, and I'm quite confident in that.

Earlier main-vs-PR formatter measurements, before the exact-integer string path (10m iterations per cycle):

Randomish input bucket main PR Speedup
f64 buffered, ns/value 42.2 20.9 2.02×
f32 buffered, ns/value 36.0 17.8 2.02×
f64 string-returning, ns/value 79.4 59.6 1.33×
f32 string-returning, ns/value 72.0 55.7 1.29×

Earlier generated code size, before the exact-integer string path (a main function calling dtoa/ftoa):

Program uses main PR Δ
f64 only 4,120 B 6,357 B +2,237 B
f32 only 4,118 B 5,192 B +1,074 B
Both 4,342 B 9,431 B +5,089 B

Most of this is the formatter which is now ECMA262 compliant, so, it's more complicated.
The actual tables are 910 bytes (before) vs 872 bytes (this pr)

  • I've read the contributing guidelines
  • I've added my name and email to the NOTICE file

P.S. Sorry about the diff. Most of it is generated WAT fixtures.

JairusSW added 3 commits June 8, 2026 21:45
Signed-off-by: Jairus Tanaka <me@jairus.dev>
Signed-off-by: Jairus Tanaka <me@jairus.dev>
Signed-off-by: Jairus Tanaka <me@jairus.dev>
@JairusSW
JairusSW marked this pull request as draft June 9, 2026 06:04
@JairusSW
JairusSW marked this pull request as ready for review June 17, 2026 18:13
@PaperPrototype

Copy link
Copy Markdown

Just curious here, why the exception of a trailing .0?

@JairusSW

JairusSW commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

Just curious here, why the exception of a trailing .0?

Max and Dan made that decision so that floats are easily identifiable when converting them to a string. For example, you know that the string "1.0" is a result of f64.toString() while the string "1" is likely a result of i32.toString(). Slight deviation from ECMA262-spec, but it's not awful

@MaxGraey

MaxGraey commented Jun 17, 2026

Copy link
Copy Markdown
Member

Can you rename xjb.ts to dtoa.ts and in comments mentioned about implementation based on xjb and zmij? xjb filename is very exotic and doesn't explains exactly what this file does

Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/xjb.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
JairusSW and others added 2 commits June 17, 2026 16:23
Co-authored-by: Max Graey <maxgraey@gmail.com>
Co-authored-by: Max Graey <maxgraey@gmail.com>
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Co-authored-by: Max Graey <maxgraey@gmail.com>
Comment thread tests/compiler/bindings/raw.release.wat Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Co-authored-by: Max Graey <maxgraey@gmail.com>
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts Outdated
Comment thread std/assembly/util/dtoa.ts
Comment thread std/assembly/util/dtoa.ts
Comment thread std/assembly/util/dtoa.ts Outdated
@MaxGraey

Copy link
Copy Markdown
Member

Also plz add extra tests which cover new implementation-specific edge cases (I anready found a bug) and uncomment existing test cases which failed with grisu2

This branch has not been deployed

No deployments
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.

[Task] Replace Grisu2 with Zmij for float-to-string conversion

4 participants