Skip to content

[alloc+atomic] Optimize GhostTreeBStackAllocator - #39

Merged
williamwutq merged 6 commits into
masterfrom
alloc
Aug 8, 2026
Merged

[alloc+atomic] Optimize GhostTreeBStackAllocator#39
williamwutq merged 6 commits into
masterfrom
alloc

Conversation

@williamwutq

Copy link
Copy Markdown
Owner

Description: Bring several optimization to GhostTreeBStackAllocator and its corresponding C implementation by child pointer and height caching, rotation threading, reducing dynamic heap allocation on hot path, and other techniques. In addition, allow C fuzzing to skip fsync, mirroring similar Rust optimization.

Important Feature: No
Type: Allocator - Optimization
Magic Number: ALGT
Bulk: No
Tests: Included
Feature Flags: alloc + set
Breaking change: No
New Types: None
Rust Only: No
Fuzz: Yes
Safety Review: Needed: Crash Safety, Invariants, Thread Safety

@williamwutq williamwutq self-assigned this Aug 8, 2026
@williamwutq

williamwutq commented Aug 8, 2026

Copy link
Copy Markdown
Owner Author

Perf data on my machine (mixed/uniform)

threads deep-tree (iters=90) master→alloc ratio median ratio
1t 37.9 → 26.3 ms 0.69 0.73
2t 44.0 → 31.1 ms 0.71 0.72
4t 38.6 → 25.9 ms (iters=72)* 0.67 0.67
16t 45.2 → 31.0 ms 0.68 0.75

@williamwutq

Copy link
Copy Markdown
Owner Author

Per-op latency (ms), 10 samples/case, iters 9→90 on APFS SSD (my machine)

threads branch min P20 P40 P60 P80 max deep-ratio a/m
1t master 22.56 28.64 33.99 43.10 53.79 64.10
1t alloc 18.55 20.77 24.42 30.33 36.89 42.75 0.69
2t master 24.39 31.80 44.76 47.50 51.64 68.66
2t alloc 21.00 26.44 28.03 33.19 38.18 46.98 0.68
4t master 16.41 18.62 27.70 31.44 35.50 45.74
4t alloc 17.55 20.25 21.65 29.01 33.61 38.79 0.78*
16t master 19.65 27.81 32.95 45.64 51.39 67.86
16t alloc 13.61 17.77 21.58 26.47 35.61 39.48 0.75

*4t deep-ratio is measured at iters=54 (criterion picked a different iter step, so the deepest common tree is shallower); its paired-median over common iters is 0.68, in line with the others.

This shows average ~27.5% lower per-op latency, a great improvement.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 optimizes the GhostTreeBstackAllocator (Rust + C) hot paths by avoiding per-operation heap allocations and reducing I/O during AVL updates via cached child heights, while also adding a C-only test/fuzz escape hatch to skip durable sync for faster fuzzing.

Changes:

  • Add denormalized cached child-height fields to AVL nodes and thread known heights through insert/remove/rotations to avoid extra reads.
  • Replace Vec-allocated path buffers with fixed-size stack arrays on AVL operations (insert, remove-min, best-fit remove).
  • Add BSTACK_TEST_NO_DURABLE_SYNC to make plat_durable_sync a no-op in C test/fuzz builds; remove the now-implemented optimization plan section from PLANNED.md.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/alloc/ghost_tree.rs Adds child-height caching + stack-based path tracking to reduce allocator critical-section work and I/O.
c/bstack_alloc.c Mirrors the Rust AVL optimizations (cached heights, threaded rotations, stack paths) and bumps ALGT magic.
c/bstack.c Adds compile-time test/fuzz option to skip durable sync for faster fuzzing.
PLANNED.md Removes the planned item that is now implemented by this PR.
Suppressed comments (1)

c/bstack_alloc.c:2376

  • nbf and nh are written by algt_read_node but never read afterwards in this branch either, which can trigger -Wunused-but-set-variable warnings. Consider explicitly marking them as used (or avoid reading them if not needed).
        uint64_t size, left, right, right_sz, right_l, right_r;
        int8_t nbf, right_bf; uint8_t nh, right_h;
        if (algt_read_node(bs, node, &size, &nbf, &nh, &left, &right) != 0) return -1;
        if (algt_read_node(bs, right, &right_sz, &right_bf, &right_h, &right_l, &right_r) != 0)

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread c/bstack_alloc.c
@williamwutq
williamwutq merged commit adf2dee into master Aug 8, 2026
19 checks passed
@williamwutq
williamwutq deleted the alloc branch August 8, 2026 08:25
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