Skip to content

perf: lazily size RegionTracker bitmaps to reduce memory overhead#1111

Merged
cberner merged 3 commits intocberner:masterfrom
jlucaso1:reduce-region-tracker-memory
Mar 18, 2026
Merged

perf: lazily size RegionTracker bitmaps to reduce memory overhead#1111
cberner merged 3 commits intocberner:masterfrom
jlucaso1:reduce-region-tracker-memory

Conversation

@jlucaso1
Copy link
Contributor

@jlucaso1 jlucaso1 commented Mar 18, 2026

Summary

  • RegionTracker::new() allocates bitmap capacity matching the actual region count instead of MAX_REGIONS (1,048,576)
  • Tree height is pre-padded via a new BtreeBitmap::new_padded() method so resize() stays simple — just grows each level's Vec, no structural changes needed
  • Only RegionTracker uses new_padded(); buddy allocators continue using new() and are unaffected
  • Removes now-unused MAX_REGIONS constant

Commit 592c2f8 changed the bitmap capacity from (regions, regions) to (regions, MAX_REGIONS) to avoid tree height changes during resize(). This trades ~2.7 MB of RAM for simpler resize logic — 21 bitmaps × 128 KB leaf allocation even for empty databases.

This PR keeps resize() simple by pre-padding only the RegionTracker bitmaps to full height at construction. Extra levels cost ~36 bytes each (single-entry U64GroupedBitmap), so the total overhead for padding is ~1.5 KB across all 21 bitmaps.

Measured impact

Minimal create → insert → query, set_cache_size(0), release optimizations:

Variant RSS after query DB overhead
redb v3 (stock) 9,488 KB ~7,268 KB
redb v3 (this PR, in-memory) 3,908 KB ~1,684 KB
redb v3 (this PR, file backend) 2,872 KB ~760 KB

@jlucaso1 jlucaso1 force-pushed the reduce-region-tracker-memory branch from df28a77 to aabb047 Compare March 18, 2026 02:04
RegionTracker pre-allocated 21 BtreeBitmaps at MAX_REGIONS (1,048,576)
capacity. Start at the actual region count instead with the tree
pre-padded to full height so resize() never needs to insert new levels.
@jlucaso1 jlucaso1 force-pushed the reduce-region-tracker-memory branch from aabb047 to 2c5be19 Compare March 18, 2026 02:15
@jlucaso1 jlucaso1 changed the title perf: reduce RegionTracker memory overhead from ~2.7 MB to ~3 KB perf: lazily size RegionTracker bitmaps to reduce memory overhead Mar 18, 2026
Move height padding from BtreeBitmap::new() into a new_padded() method
so buddy allocators are not affected. Only RegionTracker calls
new_padded() with MAX_PAGE_INDEX+1 as the max capacity.
@jlucaso1 jlucaso1 requested a review from cberner March 18, 2026 03:28
@cberner cberner merged commit 6340178 into cberner:master Mar 18, 2026
4 checks passed
@cberner
Copy link
Owner

cberner commented Mar 18, 2026

Merged. Thanks!

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