Skip to content

Optimize numeric conversion: 34% speedup for bitwise operations#282

Merged
fglock merged 4 commits intomasterfrom
optimize-numeric-conversion
Mar 7, 2026
Merged

Optimize numeric conversion: 34% speedup for bitwise operations#282
fglock merged 4 commits intomasterfrom
optimize-numeric-conversion

Conversation

@fglock
Copy link
Owner

@fglock fglock commented Mar 7, 2026

Summary

Performance improvements for numeric conversions in the runtime:

  • Replace INTEGER_PATTERN regex with simple mightBeInteger() char check in RuntimeScalar.getLong() and getInt()
  • Add fast-path in ScalarUtils.looksLikeNumber() for INTEGER/DOUBLE types
  • Split looksLikeNumber into inline fast-path + slow-path method for better JIT inlining

Benchmark

Using life_bitpacked.pl (200x200 grid, 10000 generations):

Version Generations/sec Improvement
Before ~506 gen/s -
+ looksLikeNumber fast-path ~535 gen/s +6%
+ mightBeInteger (no regex) ~680 gen/s +34% total

Root Cause

The regex pattern matching was creating new Matcher objects on every numeric conversion:

// Before: creates Matcher object every time
if (INTEGER_PATTERN.matcher(t).matches()) {
    yield Long.parseLong(t);
}

// After: simple char check
if (mightBeInteger(t)) {  // just checks first char
    yield Long.parseLong(t);
}

Test Plan

  • Build succeeds
  • Basic numeric conversions work: int("123"), int("-45"), int("1.5"), int("hello")
  • Full test suite

Additional Changes

Adds .cognition/skills/profile-perlonjava/ with JFR profiling workflow documentation for future optimization work.

Generated with Devin

fglock and others added 4 commits March 7, 2026 09:51
Performance improvements:
- Replace INTEGER_PATTERN regex with simple mightBeInteger() char check
  in RuntimeScalar.getLong() and getInt()
- Add fast-path in ScalarUtils.looksLikeNumber() for INTEGER/DOUBLE types
- Split looksLikeNumber into inline fast-path + slow-path method

Benchmark (life_bitpacked.pl 200x200 grid, 10000 generations):
- Before: ~506 gen/s
- After:  ~680 gen/s (+34%)

The regex pattern matching was creating new Matcher objects on every
numeric conversion. Replacing with a simple first-character check
avoids this overhead while still preventing exception costs for
obviously non-numeric strings.

Also adds .cognition/skills/profile-perlonjava/ with JFR profiling
workflow documentation.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <noreply@cognition.ai>
Static apply methods already check compilerSupplier before calling instance
apply. This adds applyCompiled() that skips the second check for compiled code.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <noreply@cognition.ai>
@fglock fglock merged commit ccd6304 into master Mar 7, 2026
2 checks passed
@fglock fglock deleted the optimize-numeric-conversion branch March 7, 2026 09:22
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