fix: reset pointer to the end of chunk not start - #315
Open
my4ng wants to merge 3 commits into
Open
Conversation
fitzgen
reviewed
Feb 25, 2026
fitzgen
left a comment
Owner
There was a problem hiding this comment.
Thanks! Can you add a regression test to tests/all/try_alloc_try_with.rs?
fitzgen
reviewed
Feb 26, 2026
Comment on lines
+240
to
+263
| let bump = Bump::new(); | ||
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | ||
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | ||
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | ||
|
|
||
| for _ in 0..36 { | ||
| bump.alloc_try_with(|| Err::<[u8; 128], _>(())).unwrap_err(); | ||
| } | ||
|
|
||
| // only ONE new chunk should have been allocated | ||
| assert_eq!(bump.allocated_bytes(), 974656); | ||
|
|
||
| let bump = Bump::new(); | ||
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | ||
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | ||
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | ||
|
|
||
| for _ in 0..36 { | ||
| bump.try_alloc_try_with(|| Err::<[u8; 128], _>(())) | ||
| .unwrap_err(); | ||
| } | ||
|
|
||
| // only ONE new chunk should have been allocated | ||
| assert_eq!(bump.allocated_bytes(), 974656); |
Owner
There was a problem hiding this comment.
Can this test be simplified into the following?
Suggested change
| let bump = Bump::new(); | |
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | |
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | |
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | |
| for _ in 0..36 { | |
| bump.alloc_try_with(|| Err::<[u8; 128], _>(())).unwrap_err(); | |
| } | |
| // only ONE new chunk should have been allocated | |
| assert_eq!(bump.allocated_bytes(), 974656); | |
| let bump = Bump::new(); | |
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | |
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | |
| bump.alloc_layout(Layout::from_size_align(139040, 128).unwrap()); | |
| for _ in 0..36 { | |
| bump.try_alloc_try_with(|| Err::<[u8; 128], _>(())) | |
| .unwrap_err(); | |
| } | |
| // only ONE new chunk should have been allocated | |
| assert_eq!(bump.allocated_bytes(), 974656); | |
| let mut bump = Bump::with_capacity(100); | |
| assert_eq!(bump.iter_allocated_chunks().count(), 1); | |
| type R = Result<[u8; 99], ()>; | |
| assert_eq!(mem::size_of::<R>(), 100); | |
| for _ in 0..10 { | |
| bump.alloc_try_with(|| R::Err(())).unwrap_err(); | |
| } | |
| assert_eq!(bump.iter_allocated_chunks().count(), 1); | |
| bump.alloc_try_with(|| R::Ok(Default::default())).unwrap(); | |
| assert_eq!(bump.iter_allocated_chunks().count(), 1); |
Contributor
Author
There was a problem hiding this comment.
Unfortunately the suggested change doesn't fail when the fix is reverted, however I have minimised the test case and taken some of the ideas here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #314.
In the case where
(try_)alloc_try_withhas anErrresult and it was the last allocation with a different chunk than the rewind one, thecurrent_ptrwas erroneously set to the start of the chunk rather than the end, which implies that the chunk has no more capacity left for future allocation, since we are bumping upwards. This causes the allocation doubling since it keeps getting new chunks.I suspect this might be from when the bump direction was changed.