Skip to content

Add String#bit_get, bit_set?, bit_set, bit_clear, bit_flip and bit_count (Feature #22118) - #10118

Merged
headius merged 1 commit into
jruby:ruby-4.1from
zev:string-bit-ops
Dec 1, 2026
Merged

headius merged 1 commit into
jruby:ruby-4.1from
zev:string-bit-ops

Conversation

@zev

@zev zev commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Adds the String bit methods from Ruby 4.1: bit_get, bit_set?, bit_set, bit_clear, bit_flip and bit_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. The bitwise_* 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.

Call CRuby 4.1 JRuby before JRuby after
"\xAA".b.bit_get(1) 1 NoMethodError: undefined method 'bit_get' ... 1
"\xAA".b.bit_get(0, lsb_first: false) 1 NoMethodError 1
"\xAA".b.bit_set?(8) nil NoMethodError nil
(+"\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 range NoMethodError same as CRuby
"\x00".b.freeze.bit_set(9, 0) FrozenError: can't modify frozen String: "\x00" NoMethodError same as CRuby
"\xFF\x0F".b.bit_count(4, 100) 8 NoMethodError 8
"\x00".b.bit_get(-1) IndexError: bit index out of range NoMethodError same as CRuby
"\x00".b.bit_count(0) ArgumentError: no bit length given NoMethodError same as CRuby

Changes

  • New 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 @CRuby annotation, with a note where it ports only part of a function.
  • Bit positions above 2**63. CRuby keeps positions as uint64_t. Positions from 2**63 to 2**64-1 are stored as Long.MAX_VALUE. They are past the end of every JRuby string, so every comparison gives the same result. Above 2**64-1, it raises ArgumentError, as CRuby does.
  • RubyString. It adds thin @JRubyMethod overloads with keywords = true, so a positional Hash is not read as lsb_first: (s.bit_get(0, {lsb_first: false}) raises ArgumentError, as in CRuby).
  • Order of checks, as in CRuby. Writes check 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.
  • Bit counting. It reads eight bytes at a time with a VarHandle and Long.bitCount.

Specs

I copied CRuby's newer bit_set_spec.rb, bit_clear_spec.rb, bit_flip_spec.rb and bit_count_spec.rb from spec/ruby/core/string (CRuby d8d75cee41). They add the region and frozen cases. bit_get_spec.rb and bit_set_p_spec.rb already matched CRuby.

Testing

  • The six 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 29 bit_* errors are gone. The remaining errors are the bitwise_* specs, which the next PR fixes.
  • test/mri/ruby/test_string.rb: 0 failures, as before. JRuby's copy has no bit tests yet.
  • CRuby's own test_string.rb (master, copied outside the tree): all test_bit_* tests pass, except the 9 *_chilled_string_warning tests. Those fail because of an existing JRuby issue (see below). The same tests pass by hand without --debug.
  • A script with about 300 calls (offsets and lengths of every kind, Ranges, lsb_first:, frozen strings, encodings, shared strings, and to_int that changes the receiver) gives the same output as CRuby 4.1.0-dev, except for the messages listed below.

Not in this PR

  • For a wrong argument count together with keywords, JRuby's arity check counts the keyword Hash as an argument: s.bit_get(0, 1, lsb_first: true) says (given 3, expected 1..2), while CRuby says (given 2, expected 1). s.bit_get with no arguments says expected 1..2, while CRuby says expected 1. Other methods that take keywords, like String#unpack1, already behave the same way.
  • With two unknown keywords, JRuby names only the first (unknown keyword: :foo). CRuby lists both (unknown keywords: :foo, :bar). The message comes from the shared ArgsUtil.extractKeywordArg.
  • With --debug, JRuby's chilled-string warning (RubyString DebugChilledString) is sent without category: :deprecated, so a Warning.warn handler that checks the category doesn't see it. JRuby's assert_separately adds --debug, so CRuby's *_chilled_string_warning tests (including the existing setbyte and << ones) fail on JRuby.
  • If a warning handler shrinks a chilled string, String#setbyte raises a Java ArrayIndexOutOfBoundsException, because it checks the index before the frozen check. CRuby fixed the same case in 0b60b0e4c5.

🤖 Generated with Claude Code

…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>
@zev
zev marked this pull request as ready for review November 24, 2026 03:36
@zev

zev commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@headius this is ready for review

@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.

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 headius added this to the JRuby 10.2.0.0 milestone Dec 1, 2026
@headius
headius merged commit 24832ea into jruby:ruby-4.1 Dec 1, 2026
127 of 149 checks passed
@zev

zev commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@headius I benchmarked your two ideas from the review: dropping the shared mutate indirection for the single-bit writers, and faster multi-bit byte[] updates. Neither helps, so I'd keep the code as merged.

I used JMH, calling RubyString's bit methods directly from Java so Ruby dispatch stays out of the measurement. I compared three builds:

  • before: ruby-4.1 at 6f4e39b, the merged code.
  • after1: separate setBit/clearBit/flipBit for the one-argument forms, with no Mutation dispatch.
  • after2: after1 plus flipping whole bytes 8 at a time through a long VarHandle view.

Best of 5 forks, ops/ms, Java 21, M4 Pro:

Benchmark before after1 after2
64 × bit_set(i) 4,712 4,705 (1.00×) 4,700 (1.00×)
64 × bit_clear(i) 4,711 5,302 (1.13×) 4,708 (1.00×)
64 × bit_flip(i) 4,751 4,696 (0.99×) 4,587 (0.97×)
set/clear/flip mixed 5,101 5,126 (1.00×) 5,148 (1.01×)
bit_flip(0, len), 4 KB 22,849 18,821 (0.82×) 12,942 (0.57×)
bit_flip(0, len), 1 MB 68.9 56.1 (0.81×) 40.8 (0.59×)
control: 64 × bit_get(i) 11,175 11,101 (0.99×) 11,179 (1.00×)
control: bit_count, 4 KB 7,667 6,963 (0.91×) 7,149 (0.93×)
  • Single-bit specialization: within noise. The bit_count control, whose code is the same in all three builds, moved about 10% between runs, so anything under about 15% is noise here.
  • Flipping 8 bytes at a time: about 40% slower at 4 KB and 1 MB, in every fork. C2 already vectorizes the plain bytes[i] = (byte) ~bytes[i] loop, and the VarHandle loop is slower than that. after1's 0.82× on region flips is the same run-to-run drift the control shows.

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. bench/pom.xml is still on parent 9.2.1.0-SNAPSHOT with JMH 1.19, so I compiled it by hand against lib/jruby.jar; the README has the commands. I can turn it into a PR for bench/ if you want it in the repo.

🤖 Generated with Claude Code

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