Skip to content

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.

@github-actions
Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/nns) f80e250 2025-08-18 09:56:26 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 +64 | p75 +64 | median -46 | p25 -525.07K | min -8.27M]
    change %: [max +1.13% | p75 0.00% | median -0.04% | p25 -0.19% | min -0.23%]

  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

@github-actions
Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/btreemap) f80e250 2025-08-18 09:58:09 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 +190 | 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 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

@github-actions
Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/btreeset) f80e250 2025-08-18 09:56:39 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.03M | p75 +36 | median +36 | p25 0 | min 0]
    change %: [max +0.13% | 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

@github-actions
Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/memory_manager) f80e250 2025-08-18 09:56:15 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 +320.00K | p75 +160.76K | median +1.52K | p25 +760 | min 0]
    change %: [max +0.09% | p75 +0.05% | 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

@github-actions
Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/vec) f80e250 2025-08-18 09:56:19 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

@github-actions
Copy link

github-actions bot commented Aug 15, 2025

canbench 🏋 (dir: ./benchmarks/io_chunks) f80e250 2025-08-18 09:57:14 UTC

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

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

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 18 | regressed 0 | improved 0 | new 0 | unchanged 18]
    change:   [max +10.63M | p75 +30.76K | median +1.11K | p25 +65 | min +2]
    change %: [max +0.36% | p75 0.01% | median 0.00% | p25 0.00% | min 0.00%]

  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%]

---------------------------------------------------
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 and others added 4 commits August 15, 2025 16:23
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ble-structures into maksym/free-buckets-on-clear
@maksymar maksymar requested a review from Copilot August 15, 2025 14:29
Copilot

This comment was marked as outdated.

maksymar and others added 5 commits August 15, 2025 16:31
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@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
Copilot

This comment was marked as outdated.

@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
@maksymar maksymar marked this pull request as ready for review August 18, 2025 07:54
@maksymar maksymar requested a review from a team as a code owner August 18, 2025 07:54
@maksymar maksymar marked this pull request as draft August 18, 2025 08:10
@maksymar maksymar marked this pull request as ready for review August 18, 2025 09:38
@maksymar maksymar requested a review from Copilot August 18, 2025 11:09
Copy link
Contributor

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 adds manual bucket release functionality to the MemoryManager to prevent memory waste when stable structures are cleared. The feature enables bucket reuse between different memory IDs after data migration scenarios.

  • Implements release_virtual_memory_buckets() method for manual bucket reclamation
  • Adds free bucket pool with FIFO reuse strategy for optimal memory locality
  • Modifies bucket allocation logic to prioritize reusing free buckets over allocating new ones

Reviewed Changes

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

Show a summary per file
File Description
src/memory_manager.rs Core implementation of bucket release functionality and free bucket pool management
src/memory_manager/bucket_release_core_tests.rs Unit tests for basic bucket release operations and reuse verification
src/memory_manager/bucket_release_btreemap_tests.rs Integration tests demonstrating migration scenarios with BTreeMap
benchmarks/io_chunks/canbench_results.yml Minor benchmark instruction count increases due to additional bucket management logic
benchmarks/btreemap/canbench_results.yml Minor benchmark instruction count increases due to additional bucket management logic

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

@maksymar maksymar enabled auto-merge (squash) August 18, 2025 11:18
@maksymar maksymar merged commit 73e96e8 into main Aug 18, 2025
16 checks passed
@maksymar maksymar deleted the maksym/free-buckets-on-clear branch August 18, 2025 11:22
maksymar added a commit that referenced this pull request Aug 25, 2025
This PR reverts adding `reclaim_memory` method.

Reverts several recent commits in a single change:

- `6e397bd` fix: use conservative bucket reuse that survives reload #394
- `00468e1` docs: update memory reclamation examples in the docs  #392 
- `b911479` docs: cleanup documentation  #391 
- `d1fde89` docs: use reclaim_memory() name and update docs accordingly
#388
- `a18917b` docs: add safety documentation and tests for manual bucket
release #387
- `73e96e8` feat: add manual bucket release to prevent memory waste #386

This PR restores the codebase to the state before these commits.

Done with `git revert -n 6e397bd 00468e1 b911479 d1fde89 a18917b
73e96e8`

The reason for reverting this approach was that it can reclaim unused
memory in theory but provides little benefit in real-world migrations.
All due to the requirement to keep buckets in ascending order in each
VM.

Example 1: Reuse works
```
A allocates: [0, 4, 5]
B allocates: [1, 2, 3]
A frees: [0, 4, 5]
B grows: can reuse bucket 4 (since 4 > max(B) = 3)
B after grow: [1, 2, 3, 4]
```
Example 2: Reuse fails
```
A allocates: [0, 1, 2]
B allocates: [4, 5, 6]
A frees: [0, 1, 2]
B grows: cannot reuse any freed bucket (all < max(B) = 6), so allocates new bucket 7
B after grow: [4, 5, 6, 7]
```

In real life when migrating state A to state B, state B created after
state A grown, so it's first bucket ID is already higher than any free
bucket in state A virtual memory, therefore can not be reused.
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