Skip to content

perf(runtime): reuse rounded digits in native F32 formatting - #1591

Merged
Lorenzobattistela merged 1 commit into
bendlang:mainfrom
sebastian-griego:perf/f32-fixed-format
Oct 16, 2026
Merged

Lorenzobattistela merged 1 commit into
bendlang:mainfrom
sebastian-griego:perf/f32-fixed-format

Conversation

@sebastian-griego

Copy link
Copy Markdown
Contributor

Native F32.show finds the shortest round-trip precision with scientific notation. For many values in fixed decimal notation, it then calls snprintf again. That second call repeats the decimal conversion and rounding.

Reuse the rounded digits and move the decimal point instead. For example, turn 1.25e-01 into 0.125 without a second conversion. Keep the precision search, notation thresholds, special values, and scientific-notation path unchanged.

The change adds 11 net lines to comp.ts. The regression test covers decimal-point positions, signs, negative zero, notation thresholds, large values, infinity, and NaN.

Measured complete CSV exports (milliseconds):

Workload macOS before macOS after Linux before Linux after
Waveform 444.3 406.6 510.0 465.6
Short decimals 166.7 135.9 157.0 135.6
Integer-valued floats 282.7 260.5 397.8 360.8
Scientific notation 435.3 439.5 698.1 699.2

Fixed decimal workloads used 7.9–18.5% less total time on macOS and 8.7–13.6% less on Linux. The scientific path is unchanged. Its small timing increase does not establish a regression.

Each program exports 200,000 generated rows with an integer index and three F32 columns. Each version ran 15 times in alternating order. The table gives the median after excluding the first two runs. Timing includes the native process and output to a pipe read by Python; it excludes compilation. Runs used --threads 1 --gpu off. All 120 outputs per platform had matching before/after hashes within each workload.

Both environments used an Apple M5 Max and Bun 1.3.14. macOS used Apple clang 21.0.0. Linux ran in an OrbStack ARM64 VM with Debian 13, glibc 2.41, and Clang 19.1.7. The baseline was d2aeefc1902d617340e26f7fab5c1ef50fe28efa.

Validation:

  • Old/new formatter comparisons produced identical text and return lengths for 10,393,728 inputs on macOS, Linux glibc, and Linux musl 1.2.5. The inputs include every positive F32 in [1, 2), two million pseudorandom bit patterns, and 5,120 exponent/mantissa boundary cases.
  • The same comparison passed AddressSanitizer and UndefinedBehaviorSanitizer on macOS and Linux glibc.
  • All 24 focused native tests passed on macOS and Linux. The Linux compiler and native tests ran inside Linux.
  • The new test also passed through the source CLI and emitted JavaScript under Bun on macOS.
  • bun gates/repo.ts: 54/54. git diff --check: passed.

Linux x86-64, CUDA, and the full test/performance cluster were not tested.

Waveform CSV workload

Save this as /tmp/f32-csv.bend:

import Base

def row(+i: U32) -> String:
  +t = F32.div(U32.to_f32(i), 48000.0)
  +a = F32.mul(6.2831855, F32.mul(440.0, t))
  U32.show(i) ++ "," ++ F32.show(t) ++ "," ++ F32.show(F32.sin(a))
    ++ "," ++ F32.show(F32.mul(0.75, F32.cos(a)))

def export(n: Nat, +i: U32) -> IO(Unit):
  match n:
    case 0n:
      IO.pure(Unit, Unit{})
    case 1n+p:
      do IO<Unit>:
        Unit <- IO.print(row(i))
        export(p, U32.inc(i))

def main() -> IO(Unit):
  export(200000n, 0)

Build it from each checkout. Use a separate output path for each version:

bun bend2/main.ts /tmp/f32-csv.bend -o /tmp/f32-csv-before
bun bend2/main.ts /tmp/f32-csv.bend -o /tmp/f32-csv-after

Run this Python script after both builds. It uses the same timing method as the table:

import hashlib
import statistics
import subprocess
import time

bins = ["/tmp/f32-csv-before", "/tmp/f32-csv-after"]
times = {name: [] for name in bins}
hashes = set()
for repeat in range(15):
    for name in bins if repeat % 2 == 0 else bins[::-1]:
        start = time.perf_counter_ns()
        run = subprocess.run(
            [name, "--threads", "1", "--gpu", "off"],
            capture_output=True, check=True,
        )
        times[name].append((time.perf_counter_ns() - start) / 1e6)
        hashes.add(hashlib.sha256(run.stdout).hexdigest())
assert len(hashes) == 1
for name in bins:
    print(name, statistics.median(times[name][2:]), "ms")

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 16, 2026
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