Skip to content

Commit

Permalink
Fix unsoundness in BucketBackend::shrink_to_fit (#66)
Browse files Browse the repository at this point in the history
* fix unsoundness in BucketBackend::shrink_to_fit

* adjust numbers for memory consumption of BucketBackend
  • Loading branch information
Robbepop authored May 1, 2024
1 parent 549db6c commit c906590
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
5 changes: 0 additions & 5 deletions src/backend/bucket/fixed_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,4 @@ impl FixedString {
},
))
}

/// Shrink capacity to fit the contents exactly.
pub fn shrink_to_fit(&mut self) {
self.contents.shrink_to_fit();
}
}
3 changes: 2 additions & 1 deletion src/backend/bucket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ where

fn shrink_to_fit(&mut self) {
self.spans.shrink_to_fit();
self.head.shrink_to_fit();
// Commenting out the below line fixes: https://github.com/Robbepop/string-interner/issues/46
// self.head.shrink_to_fit();
self.full.shrink_to_fit();
}

Expand Down
36 changes: 32 additions & 4 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ pub trait BackendStats {
}

impl BackendStats for backend::BucketBackend<DefaultSymbol> {
const MIN_OVERHEAD: f64 = 2.1;
const MAX_OVERHEAD: f64 = 2.33;
const MAX_ALLOCATIONS: usize = 66;
const MAX_DEALLOCATIONS: usize = 43;
const MIN_OVERHEAD: f64 = 2.2;
const MAX_OVERHEAD: f64 = 3.1;
const MAX_ALLOCATIONS: usize = 65;
const MAX_DEALLOCATIONS: usize = 42;
const NAME: &'static str = "BucketBackend";
}

Expand Down Expand Up @@ -360,6 +360,34 @@ macro_rules! gen_tests_for_backend {
let expected_iter = symbols.into_iter().zip(strings);
assert!(Iterator::eq(expected_iter, &interner));
}

#[test]
fn shrink_to_fit_works() {
let mut interner = StringInterner::new();
// Insert 3 unique strings:
let aa = interner.get_or_intern("aa").to_usize();
let bb = interner.get_or_intern("bb").to_usize();
let cc = interner.get_or_intern("cc").to_usize();

interner.shrink_to_fit();

assert_eq!(
interner.get_or_intern("aa").to_usize(),
aa,
"'aa' did not produce the same symbol",
);
assert_eq!(
interner.get_or_intern("bb").to_usize(),
bb,
"'bb' did not produce the same symbol",
);
assert_eq!(
interner.get_or_intern("cc").to_usize(),
cc,
"'cc' did not produce the same symbol",
);
assert_eq!(interner.len(), 3);
}
};
}

Expand Down

0 comments on commit c906590

Please sign in to comment.