Skip to content

Conversation

proton-lisandro-pin
Copy link
Contributor

@proton-lisandro-pin proton-lisandro-pin commented May 28, 2025

What problem are we solving?

#6804

How are we solving the problem?

As of 2e1506c, CompactMap() uses variable-size slices as ID buckets.

An unexpected issue is that mostly-ordered needle writes causes the majority of buckets to end up filled, but with a lot of unused capacity - i.e. wasted memory. For the current bucket size of 10,000 elements, Golang allocates a capacity of 11,264, or ~12% waste.

This MR ensures that buckets which end up filled are re-sized so their capacity matches size; on average, it improves memory usage by ~10% for mostly ordered needle IDs. A couple CompactMap() memory pprofs yielded these averages:

v3.87, running weed server: 346.89MB/639.85MB heap, 347.16MB/3.48GB allocs
v3.87, running weed volume: 346.89MB/626.50MB heap
v3.88, running weed server: 468.81MB/751.42MB heap, 1.74GB/4.77GB allocs
v3.88, running weed volume: 468.81MB/764.41MB heap
new, running weed server:   426.21MB/711.50MB heap, 2.16GB/5.20GB allocs
new, running weed volume:   426.21MB/664.94MB heap

The underlying logic for CompactMap.Set() was also simplified in the process.

How is the PR tested?

Every single existing unit and integration test should pass without issues; the existing API and behavior for CompactMap() is unchanged.

Checks

  • I have added unit tests if possible.
  • I will add related wiki document changes and link to this PR after merging.

Since `CompactMap()` now uses variable-size slices as ID buckets,
when needles are mostly ordered their capacity these buckets end up mostly
filled, but with a lot of wasted cappacity.

For the current bucket size of 10,000 elements, Golang allocates a capacity
of 11,264, which adds up to ~12% waste.

This MR ensures that buckets which end up filled are re-sized so their capacity
matches size; on average, it improves memory usage by ~10% for higly ordered
needle IDs. A couple `CompactMap()` memory pprofs yielded these averages:

```
v3.87, running weed server: 346.89MB/639.85MB heap, 347.16MB/3.48GB allocs
v3.87, running weed volume: 346.89MB/626.50MB heap
v3.88, running weed server: 468.81MB/751.42MB heap, 1.74GB/4.77GB allocs
v3.88, running weed volume: 468.81MB/764.41MB heap
new, running weed server:   426.21MB/711.50MB heap, 2.16GB/5.20GB allocs
new, running weed volume:   426.21MB/664.94MB heap
```

Follow up to 2e1506c; see also seaweedfs#6804 .
@chrislusf chrislusf merged commit 9ffc8bc into seaweedfs:master May 28, 2025
7 checks passed
@proton-lisandro-pin proton-lisandro-pin deleted the weed_mem_fix branch June 6, 2025 10:10
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