Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't recycle indices that reach EOL #2462

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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