Skip to content

Add an ASCII fast path for String#casecmp? - #9955

Merged
headius merged 1 commit into
jruby:masterfrom
headius:work/c01-known-casecmp
Oct 30, 2026
Merged

headius merged 1 commit into
jruby:masterfrom
headius:work/c01-known-casecmp

Conversation

@headius

@headius headius commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Port the ASCII String#casecmp? fast path from ruby/ruby#17444 by @sampokuokkanen, using ByteList.caseInsensitiveCmp instead of allocating folded copies. Unknown coderanges are scanned and cached, as in CRuby, so fresh ASCII strings can use the fast path.

JRuby median ns/op:

Input Baseline This PR
ASCII, 16 B 158.0 43.2
ASCII, first-byte mismatch, 256 B 461.4 25.8
Fresh ASCII, 32 B 1007.8 685.2
Forced-cold Unicode, short 322.4 341.5

Allocation falls from 244 to 36 bytes/op for 16-byte ASCII, and 1300 to 676 bytes/op for fresh ASCII, including the driver. The forced-cold row resets metadata before every call; normal calls reuse the cached scan.

Five JVMs/variant, Temurin 21.0.13+9, normal JIT, 512 MB heap, Linux i7-10850H, JRuby base 07e498eb6a. These historical measurements use the earlier driver; the implementation matches the measured direct port.

For benchmark-ips comparisons, run ruby bench/bench_string_casecmp_p.rb baseline /tmp/casecmp, then repeat with the other Ruby and a different label.

Validation: 57 String/Symbol examples/129 expectations pass interpreted and compiled; 2,266 encoding outcomes match baseline.

@headius

headius commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

@sampokuokkanen During the exploration of this port, Codex noticed a slowdown for unknown-coderange Unicode that is described above. The ASCII check triggers a full scan for unknown-coderange or new Unicode string content, and with CRuby's fallback Unicode casecmp logic that ends up being pure overhead.

The performance hit is not as bad on JRuby because we don't have the same optimized Unicode path. My agent oversold the slowdown to me, so I had it inject the unknown-coderange check, but I have no numbers on whether this would be a common enough case to worry about the perf hit in the original logic.

Thoughts?

Comment thread bench/bench_string_casecmp_p.rb Resolved
Comment thread test/jruby/test_string.rb Resolved
@headius
headius force-pushed the work/c01-known-casecmp branch from 5461d55 to 9974ffd Compare October 29, 2026 22:24
@headius
headius marked this pull request as ready for review October 29, 2026 22:42
@sampokuokkanen

Copy link
Copy Markdown
Contributor

Thanks for porting this! I'll also look into my other CRuby PRs if there's anything we could take from there.

I don't think we need the guard. Strings from IO, sockets or pack often have an unknown coderange. With the guard they never take the fast path, and downcase_bang sends even pure ASCII through the full Unicode caseMap. The scan result is cached, so each string pays for it once. The cold benchmarks reset it on every call, so they're the worst case. CRuby scans the same way.

ns/op, M1 Pro, JDK 24:

Case Guard Direct
Fresh ASCII, 32 B 915 623
Cold both, dense Unicode 62 B 1333 1528

Other cases were within noise.

@headius

headius commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

I agree. Unknown coderange strings don't stay that way for long enough for this to matter in real world cases. I'll revise this to match your version.

@headius
headius force-pushed the work/c01-known-casecmp branch from 9974ffd to 6cdcee3 Compare October 30, 2026 06:23
@headius headius changed the title Add a cached-ASCII fast path for String#casecmp? Add an ASCII fast path for String#casecmp? Oct 30, 2026
@headius headius added this to the JRuby 10.1.3.0 milestone Oct 30, 2026
@headius
headius merged commit 4963c83 into jruby:master Oct 30, 2026
143 checks passed
@headius
headius deleted the work/c01-known-casecmp branch October 30, 2026 06:43
@headius

headius commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

I'll also look into my other CRuby PRs if there's anything we could take from there.

That would be amazing! Chasing CRuby performance one-offs is a tough job, so I've been setting up an agent swarm to work on them. Unfortunately there's a lot of context required to make sure they do the right thing... context you already have.

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