Skip to content

Commit

Permalink
Don't recycle indices that reach EOL (#2462)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark authored Feb 8, 2022
1 parent f05e070 commit 6931e57
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ impl IdentityManager {
let (index, epoch, _backend) = id.unzip();
let pe = &mut self.epochs[index as usize];
assert_eq!(*pe, epoch);
*pe += 1;
self.free.push(index);
// If the epoch reaches EOL, the index doesn't go
// into the free list, will never be reused again.
if epoch < id::EPOCH_MASK {
*pe = epoch + 1;
self.free.push(index);
}
}
}

Expand Down Expand Up @@ -1081,3 +1085,17 @@ fn _test_send_sync(global: &Global<IdentityManagerFactory>) {
fn test_internal<T: Send + Sync>(_: T) {}
test_internal(global)
}

#[test]
fn test_epoch_end_of_life() {
use id::TypedId as _;
let mut man = IdentityManager::default();
man.epochs.push(id::EPOCH_MASK);
man.free.push(0);
let id1 = man.alloc::<id::BufferId>(Backend::Empty);
assert_eq!(id1.unzip().0, 0);
man.free(id1);
let id2 = man.alloc::<id::BufferId>(Backend::Empty);
// confirm that the index 0 is no longer re-used
assert_eq!(id2.unzip().0, 1);
}
2 changes: 1 addition & 1 deletion wgpu-core/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{cmp::Ordering, fmt, marker::PhantomData, num::NonZeroU64};
use wgt::Backend;

const BACKEND_BITS: usize = 3;
const EPOCH_MASK: u32 = (1 << (32 - BACKEND_BITS)) - 1;
pub const EPOCH_MASK: u32 = (1 << (32 - BACKEND_BITS)) - 1;
type Dummy = hal::api::Empty;

#[repr(transparent)]
Expand Down

0 comments on commit 6931e57

Please sign in to comment.