Skip to content
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

Support adding additional capacity to FreeLists #10516

Merged
merged 5 commits into from
Apr 7, 2025

Conversation

fitzgen
Copy link
Member

@fitzgen fitzgen commented Apr 2, 2025

Split out from #10503

@fitzgen fitzgen requested a review from a team as a code owner April 2, 2025 20:59
@fitzgen fitzgen requested review from dicej and removed request for a team April 2, 2025 20:59
@fitzgen fitzgen force-pushed the freelist-add-capacity branch from 1a8028b to 04e7e10 Compare April 2, 2025 21:23
free_list
}

/// Add additional capacity to this free list.
#[allow(dead_code)] // TODO: becomes used in https://github.com/bytecodealliance/wasmtime/pull/10503
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the tests below exercise this new method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines -54 to -63
if layout.size() > self.max_size() {
let trap = crate::Trap::AllocationTooLarge;
let err = anyhow::Error::from(trap);
let err = err.context(format!(
"requested allocation's size of {} is greater than the max supported size of {}",
layout.size(),
self.max_size(),
));
return Err(err);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To confirm, there's still a test below testing this behavior and that it fails?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, we just return an Ok(None) (meaning that the allocation might succeed after growth/gc) instead of Err(_) (meaning that the allocation will never succeed no matter what)

// If we are adding capacity beyond what a `u32` can address, then we
// can't actually use that capacity, so don't bother adding a new block
// to the free list.
let old_cap_rounded = round_usize_down_to_pow2(old_cap, ALIGN_USIZE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If allocations could previously go up to old_cap, shouldn't this round up instead of round down?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the old capacity was also rounded down the the alignment (all allocations are also a multiple of the alignment) so the first newly allocatable index for our additional capacity is that old capacity rounded down.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this assert that old_cap is already aligned in that case? It mostly just seems counter-inutitive to me that you'd round down the end to figure out the index of the next block, I'd expect that you'd either round up or assert rounding isn't necessary

Copy link
Member Author

@fitzgen fitzgen Apr 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep self.capacity aligned down to our alignment, then a sequence of 100 add_capacity(1) calls will never actually result in the FreeList thinking it has capacity for an allocation, even though it actually should. That is, self.capacity = round_down_to_align(self.capacity + additional) will always result in self.capacity == 0 when self.capacity is initially 0 and additional is 1, even when it is called in a loop.

We have to keep track of the actual capacity, and just do the rounding when adding blocks into the free list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok yeah that makes sense, I was missing that subtelty. Mind adding some comments to that effect to this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@github-actions github-actions bot added wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:ref-types Issues related to reference types and GC in Wasmtime labels Apr 2, 2025
Copy link

github-actions bot commented Apr 2, 2025

Subscribe to Label Action

cc @fitzgen

This issue or pull request has been labeled: "wasmtime:api", "wasmtime:ref-types"

Thus the following users have been cc'd because of the following labels:

  • fitzgen: wasmtime:ref-types

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

// If we are adding capacity beyond what a `u32` can address, then we
// can't actually use that capacity, so don't bother adding a new block
// to the free list.
let old_cap_rounded = round_usize_down_to_pow2(old_cap, ALIGN_USIZE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok yeah that makes sense, I was missing that subtelty. Mind adding some comments to that effect to this?

@fitzgen fitzgen enabled auto-merge April 7, 2025 17:16
@fitzgen fitzgen added this pull request to the merge queue Apr 7, 2025
Merged via the queue into bytecodealliance:main with commit 12195b8 Apr 7, 2025
41 checks passed
@fitzgen fitzgen deleted the freelist-add-capacity branch April 7, 2025 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:ref-types Issues related to reference types and GC in Wasmtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants