Repository navigation
Add an ASCII fast path for String#casecmp? - #9955
Conversation
|
@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? |
5461d55 to
9974ffd
Compare
|
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 ns/op, M1 Pro, JDK 24:
Other cases were within noise. |
|
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. |
9974ffd to
6cdcee3
Compare
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. |
Port the ASCII
String#casecmp?fast path from ruby/ruby#17444 by @sampokuokkanen, usingByteList.caseInsensitiveCmpinstead 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:
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-ipscomparisons, runruby 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.