Skip to content

feat: add manual bucket release to prevent memory waste #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

maksymar
Copy link
Contributor

@maksymar maksymar commented Aug 15, 2025

This PR adds release_virtual_memory_buckets() to enable manual bucket reclamation when stable structures (BTreeMap, Vec) are cleared.

Problem: Stable structures and MemoryManager operate independently - the memory manager allocates buckets but doesn't know when structures are cleared. When migrating data (A→B), clearing structure A doesn't automatically release its memory buckets, preventing structure B from reusing them and causing memory waste.

Solution: After clearing a stable structure, manually call release_virtual_memory_buckets(memory_id) to release its buckets for reuse by other structures.

Safety: Users must ensure the structure is fully cleared before releasing buckets. The memory manager provides no safety checks - releasing active buckets causes memory corruption.

Includes comprehensive tests demonstrating migration scenarios and verifying correct implementation.

Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/nns) 63579e8 2025-08-16 09:14:25 UTC

./benchmarks/nns/canbench_results.yml is up to date
📦 canbench_results_nns.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median -98 | p25 -439.05K | min -6.98M]
    change %: [max 0.00% | p75 0.00% | median -0.04% | p25 -0.16% | min -0.29%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
CSV results saved to canbench_results.csv

Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/btreemap) 63579e8 2025-08-16 09:15:56 UTC

./benchmarks/btreemap/canbench_results.yml is up to date
📦 canbench_results_btreemap.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 303 | regressed 0 | improved 0 | new 0 | unchanged 303]
    change:   [max +188.14K | p75 0 | median -3.78K | p25 -4.03K | min -1.55M]
    change %: [max +0.02% | p75 0.00% | median -0.00% | p25 -0.00% | min -0.03%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 303 | regressed 0 | improved 0 | new 0 | unchanged 303]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 303 | regressed 0 | improved 0 | new 0 | unchanged 303]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
CSV results saved to canbench_results.csv

Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/btreeset) 63579e8 2025-08-16 09:14:27 UTC

./benchmarks/btreeset/canbench_results.yml is up to date
📦 canbench_results_btreeset.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 100 | regressed 0 | improved 0 | new 0 | unchanged 100]
    change:   [max +1.32K | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 100 | regressed 0 | improved 0 | new 0 | unchanged 100]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 100 | regressed 0 | improved 0 | new 0 | unchanged 100]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
CSV results saved to canbench_results.csv

Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/memory_manager) 63579e8 2025-08-16 09:14:23 UTC

./benchmarks/memory_manager/canbench_results.yml is up to date
📦 canbench_results_memory-manager.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 3 | regressed 0 | improved 0 | new 0 | unchanged 3]
    change:   [max +768.00K | p75 +388.04K | median +8.09K | p25 +4.04K | min 0]
    change %: [max +0.22% | p75 +0.11% | median 0.00% | p25 0.00% | min 0.00%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 3 | regressed 0 | improved 0 | new 0 | unchanged 3]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 3 | regressed 0 | improved 0 | new 0 | unchanged 3]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
CSV results saved to canbench_results.csv

Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/vec) 63579e8 2025-08-16 09:14:15 UTC

./benchmarks/vec/canbench_results.yml is up to date
📦 canbench_results_vec.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 16 | regressed 0 | improved 0 | new 0 | unchanged 16]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
CSV results saved to canbench_results.csv

Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/io_chunks) 63579e8 2025-08-16 09:15:08 UTC

./benchmarks/io_chunks/canbench_results.yml is up to date
📦 canbench_results_io_chunks.csv available in artifacts

---------------------------------------------------

Summary:
  instructions:
    status:   Improvements detected 🟢
    counts:   [total 18 | regressed 0 | improved 1 | new 0 | unchanged 17]
    change:   [max +47.81K | p75 +168 | median 0 | p25 0 | min -40.98M]
    change %: [max 0.01% | p75 0.00% | median 0.00% | p25 0.00% | min -2.16%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 18 | regressed 0 | improved 0 | new 0 | unchanged 18]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 18 | regressed 0 | improved 0 | new 0 | unchanged 18]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------

Only significant changes:
| status | name                | calls |   ins |  ins Δ% | HI |  HI Δ% |   SMI |  SMI Δ% |
|--------|---------------------|-------|-------|---------|----|--------|-------|---------|
|   -    | write_chunks_vec_1m |       | 1.86B |  -2.16% |  0 |  0.00% | 1.54K |   0.00% |

ins = instructions, HI = heap_increase, SMI = stable_memory_increase, Δ% = percent change

---------------------------------------------------
CSV results saved to canbench_results.csv

@maksymar maksymar changed the title chore: empty feat: release_virtual_memory_buckets Aug 15, 2025
@maksymar maksymar force-pushed the maksym/free-buckets-on-clear branch 5 times, most recently from 74613ac to 641624a Compare August 15, 2025 13:32
@maksymar maksymar force-pushed the maksym/free-buckets-on-clear branch from 641624a to acb1eb6 Compare August 15, 2025 13:41
@maksymar maksymar changed the title feat: release_virtual_memory_buckets feat: bucket release and reuse in MemoryManager Aug 15, 2025
@maksymar maksymar requested a review from Copilot August 15, 2025 14:21
Copilot

This comment was marked as outdated.

@maksymar maksymar requested a review from Copilot August 15, 2025 14:29
Copilot

This comment was marked as outdated.

@maksymar maksymar requested a review from Copilot August 15, 2025 15:52
Copilot

This comment was marked as outdated.

@maksymar maksymar requested a review from Copilot August 15, 2025 16:00
Copilot

This comment was marked as outdated.

@maksymar maksymar requested a review from Copilot August 16, 2025 08:58
Copilot

This comment was marked as outdated.

@maksymar maksymar changed the title feat: bucket release and reuse in MemoryManager feat: add manual bucket release functionality to prevent memory waste Aug 16, 2025
@maksymar maksymar requested a review from Copilot August 16, 2025 10:52
Copy link
Contributor

@Copilot 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 manual bucket release functionality to the MemoryManager to prevent memory waste during data migration scenarios. When stable structures like BTreeMap or Vec are cleared, their allocated memory buckets remain tied to the memory ID and cannot be reused by other memories, leading to memory growth.

  • Adds release_virtual_memory_buckets() method to manually reclaim buckets after clearing structures
  • Implements bucket reuse pool to allow freed buckets to be allocated to other memories
  • Provides comprehensive test coverage demonstrating migration scenarios and bucket reuse efficiency

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/memory_manager/bucket_release_tests.rs Comprehensive test suite validating bucket release functionality and migration scenarios
src/memory_manager.rs Core implementation of bucket release mechanism and free bucket pool management
benchmarks/io_chunks/canbench_results.yml Performance benchmark results showing minimal instruction count changes
benchmarks/btreemap/canbench_results.yml BTreeMap benchmark results demonstrating performance impact

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@maksymar maksymar changed the title feat: add manual bucket release functionality to prevent memory waste feat: add manual bucket release to prevent memory waste Aug 16, 2025
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