Skip to content

Add BITRANGEINTARRAY for native bitmap enumeration - #15986

Merged
sundb merged 44 commits into
redis:unstablefrom
aviggiano:feature/bitmap-intarray
Oct 28, 2026
Merged

sundb merged 44 commits into
redis:unstablefrom
aviggiano:feature/bitmap-intarray

Conversation

@aviggiano

@aviggiano aviggiano commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Bitmap enumeration

Adds BITRANGEINTARRAY key start end [BYTE|BIT] [LIMIT count] [ZEROS], which returns the offsets of the set bits in a range, or of the clear bits with ZEROS, as an ascending integer array. Offsets are numbered as in GETBIT/SETBIT: bit 0 is the most significant bit of the first byte.

> SETBIT k 1 1 / SETBIT k 7 1 / SETBIT k 9 1 / SETBIT k 30 1
> BITRANGEINTARRAY k 0 -1                -> 1 7 9 30
> BITRANGEINTARRAY k 1 -1                -> 9 30      (bytes 1..end)
> BITRANGEINTARRAY k 5 10 BIT            -> 7 9
> BITRANGEINTARRAY k 24 31 BIT ZEROS     -> 24 25 26 27 28 29 31
> BITRANGEINTARRAY k 30 100 BIT ZEROS    -> 31        (clamped to the value)
> BITRANGEINTARRAY k 0 -1 BIT LIMIT 1    -> 1
> BITRANGEINTARRAY k 2 -1 BIT LIMIT 2    -> 7 9       (next page)
> BITRANGEINTARRAY k 0 -1 LIMIT 1        -> 1 7       (BYTE: completes byte 0)

Command behavior

  • Read-only. Works on string values and native bitmaps under either bitmap-default-roaring setting. Strings are read in place, so their bytes, type and expiration are unchanged.
  • start and end are required and follow BITCOUNT range semantics: inclusive, BYTE by default or BIT, negative indexes count from the end, out-of-range ends are clamped, and a reversed range is empty. Without LIMIT, the reply length equals BITCOUNT key start end [unit] for set bits; with ZEROS it is the number of bits in the range minus that count. 0 -1 covers the whole value.
  • ZEROS returns the clear bits instead of the set bits. The range never extends past the end of the value: unlike BITPOS key 0, which reports the first bit past the end of an all-ones value, ZEROS returns only clear bits inside the value.
  • LIMIT count returns the lowest count offsets in the range and stops the scan once they are found. LIMIT 0 returns an empty array and a negative count is an error. To page:
    • BIT: the reply stops at count; pass the last returned offset + 1 as the next start. A reply with fewer than count offsets ends the scan.
    • BYTE: the reply also completes the byte holding the last offset, so it can hold up to 7 more; pass that byte + 1 (last offset / 8 + 1) as the next start. For an unchanged value, pages never repeat or skip offsets. Separate calls do not share a snapshot.
  • BYTE/BIT, LIMIT and ZEROS follow the range in any order. A repeated option takes its last value.
  • Missing keys and empty values return an empty array, and other types return WRONGTYPE. There is no fixed result-count cap; client output-buffer limits still apply.

Implementation

  • Native bitmaps: bitroarVisitBits seeks to the start of the range, without a cardinality scan.
    • Set bits are streamed with the roaring iterator, 256 at a time.
    • With ZEROS, the runs of set bits are read and the gaps between them returned, so long runs are skipped at once.
  • Strings: bitrangeintarrayString scans 64-bit words in place, skips words without a matching bit, and visits only matching bits with clz, inverting each word for ZEROS. No temporary bitmap is allocated.
  • Offsets are streamed into a deferred-length array, so no second array proportional to the result is allocated. Standard reply suppression applies, and enumeration stops once an output-buffer limit schedules the client to close.
  • normalizeBitRange accepts NULL byte masks for callers that only need the bit interval.

Documentation

redis/docs#4304 documents the command.


Note

Medium Risk
New public read path can emit very large replies and is bounded only by client output-buffer limits; core bitmap range semantics were extended via nullable masks in shared helpers.

Overview
Adds the read-only BITRANGEINTARRAY command (8.12.0), which returns ascending bit offsets for set bits in a range—or clear bits with ZEROS—using BITCOUNT-style start/end, optional BYTE/BIT, LIMIT, and BYTE-unit paging that finishes the last byte so clients can page without gaps.

Native bitmaps enumerate via new bitroarVisitBits (Roaring iterator batches of 256; ZEROS walks gaps between set runs). String values use an in-place 64-bit word scan with clz, without building a temporary bitmap. Replies stream into a deferred-length array (no result-sized allocation) and stop when client output limits schedule disconnect. normalizeBitRange now allows NULL byte masks when only the bit interval is needed.

Command metadata lands in commands.def / bitrangeintarray.json, with broad bitroar.tcl coverage and a tools/bitroar-intarray-bench.py harness comparing native vs module int-array reads.

Reviewed by Cursor Bugbot for commit 88147bd. Bugbot is set up for automated code reviews on this repo. Configure here.

@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.08%. Comparing base (33c2a2a) to head (88147bd).

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable   #15986      +/-   ##
============================================
+ Coverage     77.73%   78.08%   +0.35%     
============================================
  Files           157      157              
  Lines         93128    93257     +129     
============================================
+ Hits          72393    72820     +427     
+ Misses        20735    20437     -298     
Files with missing lines Coverage Δ
src/bitops.c 82.14% <100.00%> (+6.59%) ⬆️
src/bitroar.c 96.16% <100.00%> (+0.49%) ⬆️
src/commands.def 100.00% <ø> (ø)
src/server.h 100.00% <ø> (ø)

... and 23 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@aviggiano aviggiano changed the title Add R.GETINTARRAY and R.RANGEINTARRAY for native bitmaps Add BITGETINTARRAY and BITRANGEINTARRAY for native bitmaps Oct 15, 2026
@aviggiano
aviggiano marked this pull request as ready for review October 16, 2026 00:53
Comment thread tests/unit/bitroar-intarray.tcl Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
@sundb sundb added this to Redis 8.12 Oct 16, 2026
@sundb sundb added release-notes indication that this issue needs to be mentioned in the release notes state:needs-doc-pr requires a PR to redis-doc repository labels Oct 16, 2026
Comment thread src/commands/bitgetintarray.json Resolved
Comment thread src/bitops.c Resolved
Comment thread tests/unit/bitroar-intarray.tcl Resolved
@aviggiano aviggiano changed the title Add BITGETINTARRAY and BITRANGEINTARRAY for native bitmaps Add BITRANGEINTARRAY for native bitmap enumeration Oct 17, 2026
@aviggiano

aviggiano commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

32-bit runtime validation now passes: 35 feature tests and 508 RESP2/RESP3 checks, including the new string-read path. Tested 378b067 with the test cleanup from 0d6e7283e; runtime code is unchanged.

Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
Comment thread src/bitops.c Resolved
@aviggiano

Copy link
Copy Markdown
Contributor Author

@sundb @oranagra feedback addressed. Please take another look.

@aviggiano
aviggiano requested review from oranagra and sundb October 19, 2026 17:47
sundb and others added 7 commits October 27, 2026 11:47
BITRANGEINTARRAY key bit start end [BYTE|BIT] [LIMIT count]

As in BITPOS, the bit argument (0 or 1) selects which bits are returned.
Unlike BITPOS, the range never extends past the end of the value, so no
clear bits are returned beyond it.

Strings invert each word when looking for clear bits. The scan is always
inlined with a constant bit, so looking for set bits keeps the plain loop.
Native bitmaps return clear bits as the gaps between runs of set bits, so
long runs are skipped at once. Both native walks read and pass offsets in
batches that start at 16 and double up to 256, so short ranges and small
LIMITs read little past what they return.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The string/native parity and BYTE LIMIT tests derive NOT keys with BITOP,
whose source and destination must be in the same slot in cluster mode.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 2b8ab5f. Configure here.

Comment thread src/bitops.c Resolved
sundb and others added 3 commits October 27, 2026 17:41
The name no longer reads like the BIT unit keyword that can follow
the range, and matches the 0 or 1 argument of SETBIT.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aviggiano

Copy link
Copy Markdown
Contributor Author

Could a maintainer add action:run-benchmark to run the CE Performance suite? I do not have permission to add the label.

BITRANGEINTARRAY key start end [BIT|BYTE] [LIMIT count] [ZEROS]
returns the set bits by default, and the clear bits with ZEROS. The
common case no longer needs a 0 or 1 argument that reads like the BIT
unit keyword.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sundb sundb added the action:run-benchmark Triggers the benchmark suite for this Pull Request label Oct 27, 2026
@sundb

sundb commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Could a maintainer add action:run-benchmark to run the CE Performance suite? I do not have permission to add the label.

added

@sundb

sundb commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

@sundb sundb removed the action:run-benchmark Triggers the benchmark suite for this Pull Request label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-notes indication that this issue needs to be mentioned in the release notes state:needs-doc-pr requires a PR to redis-doc repository

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants