Skip to content

Add a single-byte fast path for String#capitalize and fix :ascii case mapping - #9961

Merged
headius merged 2 commits into
jruby:masterfrom
sampokuokkanen:string-casemap-single-byte
Oct 30, 2026
Merged

headius merged 2 commits into
jruby:masterfrom
sampokuokkanen:string-casemap-single-byte

Conversation

@sampokuokkanen

Copy link
Copy Markdown
Contributor

This ports ruby/ruby#17403, which added capitalize_single to CRuby, and fixes some :ascii case mapping bugs I found along the way.

Fast path

upcase, downcase and capitalize now get the same single-byte helpers CRuby has (upcaseSingle, downcaseSingle, capitalizeSingle), behind a port of case_option_single_p. That check also covers ASCII-8BIT strings now, as in ruby/ruby#17401. Before this, capitalize always went through full Unicode case mapping.

:ascii fixes

StringSupport.asciiOnlyCaseMap walked bytes as if every string were ASCII. Now it walks characters in the string's own encoding, like CRuby's onigenc_ascii_only_case_map. So:

  • invalid strings raise ArgumentError: input string invalid, as in CRuby
  • capitalize(:ascii) on UTF-16BE and UTF-32BE capitalizes the first character instead of the first byte
  • Shift_JIS characters whose second byte looks like an ASCII letter are left alone; before, they were changed into a different character

Benchmark

bench/bench_string_casemap.rb (benchmark-ips, single run, arm64 macOS, JDK 21). CRuby ran without YJIT/ZJIT. Iterations per second:

JRuby master This PR Change CRuby 4.0.7 CRuby master
upcase, 1 byte 8.9M 9.1M 1.02x 8.0M 8.6M
upcase!, 1 byte 9.1M 9.0M 0.99x 8.6M 9.6M
upcase, 100 bytes 5.3M 5.8M 1.10x 5.9M 6.2M
upcase!, 100 bytes 5.3M 6.1M 1.14x 5.1M 5.3M
upcase, 1000 bytes 1.4M 1.5M 1.11x 1.7M 1.6M
upcase!, 1000 bytes 1.4M 1.5M 1.10x 1.0M 1.0M
upcase, 100 bytes binary 4.3M 5.4M 1.26x 2.5M 6.3M
upcase!, 100 bytes binary 4.2M 5.6M 1.33x 2.3M 5.0M
upcase, 100 bytes UTF-8 1.9M 2.0M 1.07x 1.5M 1.5M
upcase!, 100 bytes UTF-8 1.8M 2.0M 1.09x 1.4M 1.4M
downcase, 1 byte 8.0M 9.2M 1.14x 8.4M 8.8M
downcase!, 1 byte 7.6M 9.2M 1.21x 9.0M 9.6M
downcase, 100 bytes 4.8M 5.8M 1.22x 6.6M 6.8M
downcase!, 100 bytes 5.4M 6.1M 1.12x 6.4M 6.7M
downcase, 1000 bytes 1.0M 1.6M 1.58x 2.1M 2.1M
downcase!, 1000 bytes 1.2M 1.6M 1.33x 1.5M 1.5M
downcase, 100 bytes binary 2.5M 5.6M 2.28x 2.6M 6.8M
downcase!, 100 bytes binary 2.5M 5.8M 2.34x 2.3M 6.7M
downcase, 100 bytes UTF-8 2.0M 2.0M 1.00x 1.5M 1.5M
downcase!, 100 bytes UTF-8 2.0M 2.0M 0.99x 1.4M 1.5M
capitalize, 1 byte 7.3M 9.1M 1.25x 5.5M 8.6M
capitalize!, 1 byte 6.6M 9.4M 1.44x 3.7M 9.6M
capitalize, 100 bytes 2.7M 6.0M 2.26x 1.2M 6.4M
capitalize!, 100 bytes 2.7M 6.3M 2.37x 1.3M 6.7M
capitalize, 1000 bytes 410k 1.8M 4.31x 170k 1.8M
capitalize!, 1000 bytes 412k 1.8M 4.31x 162k 1.7M
capitalize, 100 bytes binary 3.2M 5.7M 1.75x 2.4M 6.4M
capitalize!, 100 bytes binary 3.3M 5.8M 1.77x 2.3M 6.8M
capitalize, 100 bytes UTF-8 2.1M 2.0M 0.98x 1.4M 1.5M
capitalize!, 100 bytes UTF-8 2.0M 2.0M 0.99x 1.4M 1.5M

swapcase is unchanged, since CRuby has no single-byte path for it (it's used really rarely). Strings with non-ASCII characters don't take the fast path, so they don't change either. Non-bang downcase on long ASCII strings is still behind CRuby; I'll look at that separately.

I didn't add scanning of unknown code ranges (as #9955 does for casecmp?): CRuby doesn't scan here either, and it made fresh non-ASCII strings about 20% slower.

Port ruby/ruby#17403 (capitalize_single). The fast-path check follows
case_option_single_p, including ASCII-8BIT strings, and upcase, downcase
and capitalize each get a CRuby-shaped helper: upcaseSingle,
downcaseSingle and capitalizeSingle.
StringSupport.asciiOnlyCaseMap always walked bytes with ASCIIEncoding, so
invalid strings did not raise, capitalize titlecased the first byte of
UTF-16BE and UTF-32BE strings, and Shift_JIS trail bytes in the ASCII
range were case mapped. Port onigenc_ascii_only_case_map so it walks
characters in the string's encoding, as CRuby does.

The single-byte check now matches only UTF-8 itself, as
case_option_single_p does, so UTF8-MAC strings are checked too.

The deprecated asciiOnlyCaseMap(Ruby, ByteList, IntHolder, Encoding)
overload now honors its encoding argument instead of ignoring it, so
callers can see ArgumentError for invalid input.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the port!

@headius
headius merged commit 958fea1 into jruby:master Oct 30, 2026
146 checks passed
@headius headius added this to the JRuby 10.1.3.0 milestone Oct 30, 2026
@sampokuokkanen
sampokuokkanen deleted the string-casemap-single-byte branch November 15, 2026 00:39
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