Skip to content

Commit

Permalink
Review adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
terrarier2111 committed Jul 2, 2023
1 parent a1de834 commit 621002d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
14 changes: 4 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,11 @@ impl<T: Send> Default for ThreadLocal<T> {

impl<T: Send> Drop for ThreadLocal<T> {
fn drop(&mut self) {
let mut bucket_size = 1;

// Free each non-null bucket
for bucket in self.buckets.iter_mut() {
for (i, bucket) in self.buckets.iter_mut().enumerate() {
let bucket_ptr = *bucket.get_mut();

let this_bucket_size = bucket_size;
bucket_size <<= 1;
let this_bucket_size = 1 << i;

if bucket_ptr.is_null() {
break;
Expand All @@ -167,11 +164,8 @@ impl<T: Send> ThreadLocal<T> {
let allocated_buckets = usize::from(POINTER_WIDTH) - (capacity.leading_zeros() as usize);

let mut buckets = [ptr::null_mut(); BUCKETS];
let mut bucket_size = 1;
for bucket in buckets[..allocated_buckets].iter_mut() {
*bucket = allocate_bucket::<T>(bucket_size);

bucket_size <<= 1;
for (i, bucket) in buckets[..allocated_buckets].iter_mut().enumerate() {
*bucket = allocate_bucket::<T>(1 << i);
}

Self {
Expand Down
7 changes: 2 additions & 5 deletions src/thread_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ impl ThreadIdManager {
if let Some(id) = self.free_list.pop() {
id.0
} else {
// we don't allow 1 before MAX to be returned because our buckets can only contain
// up to usize::MAX - 1 elements, but this shouldn't have any impact in practice.
if self.free_from >= usize::MAX - 1 {
panic!("Ran out of thread IDs");
}
// `free_from` can't overflow as each thread takes up at least 2 bytes of memory and
// thus we can't even have `usize::MAX / 2 + 1` threads.

let id = self.free_from;
self.free_from += 1;
Expand Down

0 comments on commit 621002d

Please sign in to comment.