Skip to content

Commit

Permalink
Expand comment about why we round up in the shrink check
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzgen authored Feb 14, 2024
1 parent 25c7d1f commit 1be6a22
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,9 +1724,31 @@ impl Bump {
// Only reclaim the excess space (which requires a copy) if it
// is worth it: we are actually going to recover "enough" space
// and we can do a non-overlapping copy.
// `(old_size + 1) / 2` so division rounds up rather than down,
// so this test fails for e.g. `old_size` = 1, `new_size` = 1
// or `old_size` = 5, `new_size` = 3 (which would overlap).
//
// We do `(old_size + 1) / 2` so division rounds up rather than
// down. Consider when:
//
// old_size = 5
// new_size = 3
//
// If we do not take care to round up, this will result in:
//
// delta = 2
// (old_size / 2) = (5 / 2) = 2
//
// And the the check will succeed even though we are have
// overlapping ranges:
//
// |--------old-allocation-------|
// |------from-------|
// |-------to--------|
// +-----+-----+-----+-----+-----+
// | a | b | c | . | . |
// +-----+-----+-----+-----+-----+
//
// But we MUST NOT have overlapping ranges because we use
// `copy_nonoverlapping` below! Therefore, we round the
// division up to avoid this issue.
&& delta >= (old_size + 1) / 2
{
let footer = self.current_chunk_footer.get();
Expand Down

0 comments on commit 1be6a22

Please sign in to comment.