Repository navigation
Add String#bit_get, bit_set?, bit_set, bit_clear, bit_flip and bit_count (Feature #22118) - #10118
Conversation
…unt (Feature #22118) These read, change and count single bits of a string's bytes. Bit 0 is the least significant bit of the first byte; lsb_first: false numbers the bits in each byte from the most significant bit instead. bit_set, bit_clear, bit_flip and bit_count also take a region, as an offset and a length or as a Range (Feature #22279). Writes raise IndexError when the region runs past the end; bit_count clips it. The frozen check comes before the bounds check, as in CRuby (Bug #22316). The logic lives in the new org.jruby.util.StringBits. Bit positions from 2**63 to 2**64-1 are kept as Long.MAX_VALUE: they are past the end of every JRuby string, so the results are the same as CRuby's. Updates the bit_set, bit_clear, bit_flip and bit_count specs from CRuby's spec/ruby, which add the region and frozen cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@headius this is ready for review |
headius
left a comment
There was a problem hiding this comment.
Straight port looks pretty good. Future work could do some benchmarking to see if this optimizes like we want it to; having all mutations pass through the same indirection with multiple mutate functions will damage inlining, and there may be faster ways to update multiple bits in a byte[] on JVM.
|
@headius I benchmarked your two ideas from the review: dropping the shared I used JMH, calling
Best of 5 forks, ops/ms, Java 21, M4 Pro:
An earlier benchmark-ips run from Ruby gave the same answer: single-bit 0.98–1.20×, word-wise flip 0.79× at 1 MB. The benchmark class, the two variant diffs, full results and run instructions are in https://gist.github.com/zev/242e75871bc6d12fe04216a149d46605. 🤖 Generated with Claude Code |
Adds the String bit methods from Ruby 4.1:
bit_get,bit_set?,bit_set,bit_clear,bit_flipandbit_count(Feature #22118). It includes the region arguments (Feature #22279) and the frozen check before the bounds check (Bug #22316). These are on the #9746 checklist. Thebitwise_*methods from the same feature come in a separate PR, based on this one.CRuby changes: ruby/ruby@2461caebba, ruby/ruby@03a426ec8d, ruby/ruby@d8d75cee41. Current code: string.c#L7424-L8209.
"\xAA".b.bit_get(1)1NoMethodError: undefined method 'bit_get' ...1"\xAA".b.bit_get(0, lsb_first: false)1NoMethodError1"\xAA".b.bit_set?(8)nilNoMethodErrornil(+"\x00\x00").b.bit_set(4, 8)"\xF0\x0F"NoMethodError"\xF0\x0F"(+"\xFF").b.bit_clear(0..3)"\xF0"NoMethodError"\xF0"(+"\x00").b.bit_flip(0, 9)IndexError: bit range out of rangeNoMethodError"\x00".b.freeze.bit_set(9, 0)FrozenError: can't modify frozen String: "\x00"NoMethodError"\xFF\x0F".b.bit_count(4, 100)8NoMethodError8"\x00".b.bit_get(-1)IndexError: bit index out of rangeNoMethodError"\x00".b.bit_count(0)ArgumentError: no bit length givenNoMethodErrorChanges
org.jruby.util.StringBits. It holds the logic, split like CRuby's helpers: offset and length conversion, Range conversion, the byte mask for a region, the region write, and the bit count. Each method has a@CRubyannotation, with anotewhere it ports only part of a function.2**63. CRuby keeps positions asuint64_t. Positions from2**63to2**64-1are stored asLong.MAX_VALUE. They are past the end of every JRuby string, so every comparison gives the same result. Above2**64-1, it raisesArgumentError, as CRuby does.RubyString. It adds thin@JRubyMethodoverloads withkeywords = true, so a positional Hash is not read aslsb_first:(s.bit_get(0, {lsb_first: false})raisesArgumentError, as in CRuby).frozen?after converting the arguments and before the bounds check. The string size is read after the frozen check, because a chilled-string warning handler can change the string. Writes clear the code range.VarHandleandLong.bitCount.Specs
I copied CRuby's newer
bit_set_spec.rb,bit_clear_spec.rb,bit_flip_spec.rbandbit_count_spec.rbfromspec/ruby/core/string(CRubyd8d75cee41). They add the region and frozen cases.bit_get_spec.rbandbit_set_p_spec.rbalready matched CRuby.Testing
bit_*spec files: 47 examples, 0 failures, 0 errors on JRuby (interpreter and forced JIT) and CRuby 4.1.0-dev. CRuby 4.0.7 runs 0 examples, because of the version guard. On the base branch, all 29 old examples errored.spec/ruby/core/string(mspec ci): no new failures. The 29bit_*errors are gone. The remaining errors are thebitwise_*specs, which the next PR fixes.test/mri/ruby/test_string.rb: 0 failures, as before. JRuby's copy has no bit tests yet.test_string.rb(master, copied outside the tree): alltest_bit_*tests pass, except the 9*_chilled_string_warningtests. Those fail because of an existing JRuby issue (see below). The same tests pass by hand without--debug.lsb_first:, frozen strings, encodings, shared strings, andto_intthat changes the receiver) gives the same output as CRuby 4.1.0-dev, except for the messages listed below.Not in this PR
s.bit_get(0, 1, lsb_first: true)says(given 3, expected 1..2), while CRuby says(given 2, expected 1).s.bit_getwith no arguments saysexpected 1..2, while CRuby saysexpected 1. Other methods that take keywords, likeString#unpack1, already behave the same way.unknown keyword: :foo). CRuby lists both (unknown keywords: :foo, :bar). The message comes from the sharedArgsUtil.extractKeywordArg.--debug, JRuby's chilled-string warning (RubyStringDebugChilledString) is sent withoutcategory: :deprecated, so aWarning.warnhandler that checks the category doesn't see it. JRuby'sassert_separatelyadds--debug, so CRuby's*_chilled_string_warningtests (including the existingsetbyteand<<ones) fail on JRuby.String#setbyteraises a JavaArrayIndexOutOfBoundsException, because it checks the index before the frozen check. CRuby fixed the same case in0b60b0e4c5.🤖 Generated with Claude Code