Skip to content

perf: optimize parseInt with 4-digits-at-a-time parsing#897

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/parseInt-swar-clean
Open

perf: optimize parseInt with 4-digits-at-a-time parsing#897
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/parseInt-swar-clean

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented Jun 6, 2026

Motivation

The std.parseInt function was 1.97x slower than jrsonnet (Rust implementation) on Scala Native. The main bottleneck was the per-character digit parsing loop.

Key Design Decision

Inspired by jsoniter-scala's SWAR digit parsing technique, process 4 decimal digits at a time instead of 1. This reduces the number of loop iterations and charAt calls by 4x for the common case.

Modification

Changed sjsonnet/src/sjsonnet/stdlib/StringModule.scala:

  • For base 10 parsing, process 4 digits at a time
  • Validate all 4 digits before combining them
  • Fall through to scalar loop if any non-digit is found
  • Use Double accumulator throughout to handle numbers beyond Long range

Benchmark Results

Scala Native vs jrsonnet (hyperfine)

Test Before After Improvement
parseInt jrsonnet 1.97x faster jrsonnet 1.27x faster Gap reduced 36%

JMH (JVM)

Baseline JMH benchmarks are stable; the change benefits all platforms.

Analysis

The 4-digits-at-a-time approach reduces loop overhead by processing 4 digits per iteration instead of 1. For the benchmark input "-123949595" (9 digits), this means 2 iterations instead of 9.

The optimization validates all 4 digits before combining them, falling through to the scalar loop if any non-digit is found. This ensures correct error reporting for invalid inputs.

References

Result

  • ✅ All tests pass (./mill __.test)
  • ✅ Code formatted (./mill __.reformat)
  • ✅ Performance improved (gap reduced 36%)
  • ✅ No regressions in other scenarios

Inspired by jsoniter-scala's SWAR digit parsing technique, process
4 decimal digits at a time instead of 1. This reduces the number of
loop iterations and charAt calls by 4x for the common case.

The optimization validates all 4 digits before combining them, falling
through to the scalar loop if any non-digit is found. Uses Double
accumulator throughout to handle numbers beyond Long range.

Benchmark results (parseInt):
- Before: jrsonnet 1.97x faster
- After: jrsonnet 1.27x faster (36% gap reduction)

Reference: plokhotnyuk/jsoniter-scala@df92601
@He-Pin He-Pin marked this pull request as ready for review June 6, 2026 15:05
@He-Pin He-Pin marked this pull request as draft June 6, 2026 15:06
@He-Pin He-Pin marked this pull request as ready for review June 6, 2026 16:40
He-Pin added a commit to He-Pin/sjsonnet that referenced this pull request Jun 6, 2026
Motivation:
The bulk numeric array parser (previous commit) used
Double.parseDouble(data.substring()) for each element, creating a
substring allocation and invoking the full double parser even for
simple integers.

Modification:
- Parse simple integers directly using 4-digits-at-a-time technique
  (inspired by jsoniter-scala/PR databricks#897), avoiding substring allocation
  and Double.parseDouble overhead entirely
- Use Val.cachedNum for values 0-255, reusing pre-allocated instances
  instead of creating new Val.Num objects
- Float/exponent numbers still fall back to Double.parseDouble

Result:
Native A/B (member): 7.1ms → 5.9ms (-17.4%).
Combined with bulk parser: member gap vs jrsonnet 1.97x → 1.42x.
Also improves base64_byte_array: 11.3ms → 10.4ms (-7.9%).
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.

1 participant