Skip to content

# (feat): BitVector Pooling Support#14

Merged
mgyoo86 merged 15 commits intomasterfrom
feat/BitVector_support
Jan 25, 2026
Merged

# (feat): BitVector Pooling Support#14
mgyoo86 merged 15 commits intomasterfrom
feat/BitVector_support

Conversation

@mgyoo86
Copy link
Collaborator

@mgyoo86 mgyoo86 commented Jan 24, 2026

Summary

This PR introduces comprehensive support for pooling BitArrays (specifically BitVector and its multidimensional views), enabling significant memory savings for boolean arrays compared to standard Vector{Bool}.

Key Changes

1. New Bit Sentinel Type

Introduced Bit as a sentinel type to distinguish between bit-packed arrays and standard boolean arrays:

  • acquire!(pool, Bit, n): Returns a pooled BitVector (1 bit per element).
  • acquire!(pool, Bool, n): Returns a pooled Vector{Bool} (1 byte per element).

2. BitTypedPool Implementation

  • Added specialized BitTypedPool to AdaptiveArrayPool (accessible via .bits).
  • Allocates and manages a simplified pool of BitVectors.
  • Returns SubArray{Bool, 1, BitVector} for 1D requests.
  • Returns ReshapedArray{Bool, N, ...} wrapper for N-D requests, maintaining zero-allocation usage where possible.

3. API Integrations

  • falses! (or zeros!) / trues! (or ones!) Support:
    # Allocates BitVector filled with false (reused if available)
    mask = falses!(pool, 1000) # or zeros!(pool, Bit, 1000)
    
    # Allocates BitVector filled with true
    flags = trues!(pool, 1000) # or ones!(pool, Bit, 1000)
  • Sentinel Methods: Defined zero(Bit) === false and one(Bit) === true to support generic fill operations.

4. Memory Efficiency

  • ~8x reduction in memory usage for boolean arrays.
  • Ideal for large masks, flags, and binary datasets where Vector{Bool} overhead is significant.

Usage Example

using AdaptiveArrayPools

pool = AdaptiveArrayPool()

# 1. Standard Bool Vector (1000 bytes)
v_bool = acquire!(pool, Bool, 1000) 

# 2. BitVector (1000 bits ≈ 125 bytes)
v_bit = acquire!(pool, Bit, 1000)

# 3. Multidimensional BitArray
# Returns a ReshapedArray viewing the pooled BitVector
matrix_bit = trues!(pool, 100, 100)

Testing

  • Added test/test_bitarray.jl covering:
    • Sentinel type behavior.
    • Pool structure verification.
    • 1D and N-D acquisition and reuse.
    • zeros! and ones! convenience functions.
    • Resizing behaviors (via pool release/re-acquire).

- Add BitTypedPool struct extending AbstractTypedPool{Bool, BitVector}
- Add `bits` field to AdaptiveArrayPool for BitVector storage
- Update FIXED_SLOT_FIELDS to include :bits slot
- Include empty N-D cache fields for empty!() compatibility

BitTypedPool stores 1 bit per element (~8x memory savings vs Vector{Bool}).
Note: unsafe_acquire! not supported - BitArray chunks are immutable.
- Add allocate_vector dispatch for BitTypedPool
- Implement acquire_bits!(pool, n) for 1D SubArray{Bool,1,BitVector}
- Implement acquire_bits!(pool, dims...) for N-D ReshapedArray views
- Add _acquire_bits_impl! for macro transformation support
- Add DisabledPool fallbacks returning native BitArray types
- Add trues!(pool, dims...) for BitArray filled with true
- Add falses!(pool, dims...) for BitArray filled with false
- Include tuple form and DisabledPool fallbacks
- Add _trues_impl!/_falses_impl! for macro transformation
- Add GlobalRef constants for _acquire_bits_impl!, _trues_impl!, _falses_impl!
- Update _transform_acquire_calls for acquire_bits!/trues!/falses!
- Add :_bits_slot marker in _extract_acquire_types for typed checkpoint
- Handle :_bits_slot in _filter_static_types to trigger full checkpoint
- Export acquire_bits!, trues!, falses! in main module
- Add test_bitarray.jl with 85 tests covering:
  - BitTypedPool structure and state management
  - acquire_bits! 1D and N-D operations
  - trues!/falses! convenience functions
  - DisabledPool fallbacks and macro integration
  - Memory efficiency vs Vector{Bool}
- Update test_fixed_slots.jl to use AbstractTypedPool
- Add acquire_bits! to empty! integration test
BREAKING CHANGE: Removed acquire_bits!, trues!, falses! functions.

Use the unified API with Bit sentinel type instead:
- acquire_bits!(pool, n)     -> acquire!(pool, Bit, n)
- trues!(pool, dims...)      -> ones!(pool, Bit, dims...)
- falses!(pool, dims...)     -> zeros!(pool, Bit, dims...)

Changes:
- Add Bit sentinel type for bit-packed boolean storage
- Add get_typed_pool!(pool, ::Type{Bit}) dispatch
- Add zero(Bit)/one(Bit) for fill operations
- Add DisabledPool fallbacks for Bit type
- Remove acquire_bits!, trues!, falses! and their _impl! variants
- Remove macro transformation for removed functions
- Update tests to use new unified API

This provides a consistent API pattern across all element types:
  acquire!(pool, Float64, n)  # Vector{Float64}
  acquire!(pool, Bool, n)     # Vector{Bool}
  acquire!(pool, Bit, n)      # BitVector
- Add _throw_bit_unsafe_error() with helpful message explaining why
  BitArray cannot use unsafe_wrap (immutable chunks)
- Add explicit _unsafe_acquire_impl! dispatches for Bit type that
  fail-fast before any work is done
- Add test verifying error is thrown with helpful message
- Fix unused argument warnings in DisabledPool fallbacks
@codecov
Copy link

codecov bot commented Jan 24, 2026

Codecov Report

❌ Patch coverage is 97.14286% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.45%. Comparing base (e7553b4) to head (020317d).
⚠️ Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
src/macros.jl 80.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #14      +/-   ##
==========================================
+ Coverage   96.41%   96.45%   +0.04%     
==========================================
  Files           8        8              
  Lines        1004     1072      +68     
==========================================
+ Hits          968     1034      +66     
- Misses         36       38       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces comprehensive BitVector pooling support to AdaptiveArrayPools, enabling significant memory savings (approximately 8x) for boolean arrays by using bit-packed storage instead of byte-per-element storage.

Changes:

  • Introduced Bit sentinel type to distinguish bit-packed arrays from standard boolean arrays
  • Added BitTypedPool specialized pool for managing BitVector instances with 1D caching
  • Integrated Bit type support into existing APIs (acquire!, zeros!, ones!) with proper error handling for unsupported unsafe_acquire!

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/types.jl Defines Bit sentinel type, BitTypedPool structure, and adds bits field to AdaptiveArrayPool; includes dispatch for get_typed_pool!
src/acquire.jl Implements allocation dispatch for BitTypedPool, defines zero/one for Bit, adds clear error handling for unsupported unsafe_acquire!, and provides DisabledPool fallbacks
src/convenience.jl Adds zeros!/ones! support for DisabledPool with Bit type using falses/trues
src/AdaptiveArrayPools.jl Exports the Bit type for public use
test/test_bitarray.jl Comprehensive test suite covering all aspects of BitArray pooling including acquisition, state management, error handling, and memory efficiency
test/test_fixed_slots.jl Updates fixed slot infrastructure tests to include BitTypedPool and use AbstractTypedPool type checks
test/test_coverage.jl Adds tests for pooling_enabled dispatch covering both AbstractArrayPool and DisabledPool
test/runtests.jl Includes new BitArray test file in the test suite

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Add back trues! and falses! as convenience wrappers that mirror Julia's
built-in trues() and falses() functions for BitArray creation.

Changes:
- Add trues!(pool, dims...) as wrapper for ones!(pool, Bit, dims...)
- Add falses!(pool, dims...) as wrapper for zeros!(pool, Bit, dims...)
- Add _trues_impl!/_falses_impl! for macro transformation
- Add DisabledPool fallbacks (trues!/falses! -> trues/falses)
- Add macro transformation support in _transform_acquire_calls
- Add type extraction for trues!/falses! (always Bit type)
- Add comprehensive tests including macro integration

This provides both API styles:
- Original Julia-style: trues!(pool, dims...) / falses!(pool, dims...)
- Unified Bit sentinel: ones!(pool, Bit, dims...) / zeros!(pool, Bit, dims...)
Invert the delegation relationship so that trues!/falses! are the
canonical BitArray functions (matching Julia's native API), and
ones!/zeros! with Bit type delegate to them.

Changes:
- trues!/falses! now have their own implementations (acquire + fill)
- ones!(pool, Bit, ...) delegates to trues!(pool, ...)
- zeros!(pool, Bit, ...) delegates to falses!(pool, ...)
- Update docstrings to reflect new primary status

This follows Julia's semantics where trues/falses are the native
BitArray creation functions, not ones/zeros.
…aths

Add tests to achieve 100% coverage for convenience.jl BitArray functions:
- NTuple form tests for trues!/falses! and zeros!/ones! with Bit type
- Generic DisabledPool fallback tests for unknown backends
- _trues_impl!/_falses_impl! delegator tests for DisabledPool
- _zeros_impl!/_ones_impl! with Bit type NTuple for AbstractArrayPool

Coverage: src/convenience.jl 96% → 100%
@mgyoo86 mgyoo86 merged commit 7d4fd9a into master Jan 25, 2026
9 checks passed
@mgyoo86 mgyoo86 deleted the feat/BitVector_support branch January 25, 2026 01:29
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.

1 participant