Repository navigation
Add a single-byte fast path for String#capitalize and fix :ascii case mapping - #9961
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This ports ruby/ruby#17403, which added
capitalize_singleto CRuby, and fixes some:asciicase mapping bugs I found along the way.Fast path
upcase,downcaseandcapitalizenow get the same single-byte helpers CRuby has (upcaseSingle,downcaseSingle,capitalizeSingle), behind a port ofcase_option_single_p. That check also covers ASCII-8BIT strings now, as in ruby/ruby#17401. Before this,capitalizealways went through full Unicode case mapping.:asciifixesStringSupport.asciiOnlyCaseMapwalked bytes as if every string were ASCII. Now it walks characters in the string's own encoding, like CRuby'sonigenc_ascii_only_case_map. So:ArgumentError: input string invalid, as in CRubycapitalize(:ascii)on UTF-16BE and UTF-32BE capitalizes the first character instead of the first byteBenchmark
bench/bench_string_casemap.rb(benchmark-ips, single run, arm64 macOS, JDK 21). CRuby ran without YJIT/ZJIT. Iterations per second:upcase, 1 byteupcase!, 1 byteupcase, 100 bytesupcase!, 100 bytesupcase, 1000 bytesupcase!, 1000 bytesupcase, 100 bytes binaryupcase!, 100 bytes binaryupcase, 100 bytes UTF-8upcase!, 100 bytes UTF-8downcase, 1 bytedowncase!, 1 bytedowncase, 100 bytesdowncase!, 100 bytesdowncase, 1000 bytesdowncase!, 1000 bytesdowncase, 100 bytes binarydowncase!, 100 bytes binarydowncase, 100 bytes UTF-8downcase!, 100 bytes UTF-8capitalize, 1 bytecapitalize!, 1 bytecapitalize, 100 bytescapitalize!, 100 bytescapitalize, 1000 bytescapitalize!, 1000 bytescapitalize, 100 bytes binarycapitalize!, 100 bytes binarycapitalize, 100 bytes UTF-8capitalize!, 100 bytes UTF-8swapcaseis 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-bangdowncaseon 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.