Skip to content

simplify the reduced slotmap even further #925

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
71 changes: 14 additions & 57 deletions src/texture/slotmap.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,25 @@
// Heavily reduced version of the `slotmap` crate: https://github.com/orlp/slotmap
use miniquad::TextureId;
use std::fmt;
use std::num::NonZeroU32;

#[derive(Copy, Clone, PartialEq)]
pub(crate) struct TextureSlotId {
idx: u32,
version: NonZeroU32,
}

impl fmt::Debug for TextureSlotId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}v{}", self.idx, self.version.get())
}
}

impl TextureSlotId {
fn new(idx: u32, version: u32) -> Self {
debug_assert!(version > 0);

Self {
idx,
version: unsafe { NonZeroU32::new_unchecked(version | 1) },
}
}
}
pub(crate) type TextureSlotId = u32;

// Storage inside a slot or metadata for the freelist when vacant.
union SlotUnion {
value: TextureId,
next_free: u32,
}

// A slot, which represents storage for a value and a current version.
// Can be occupied or vacant.
struct Slot {
u: SlotUnion,
version: u32, // Even = vacant, odd = occupied.
}

/// Slot map, storage with stable unique keys.
pub(crate) struct TextureIdSlotMap {
slots: Vec<Slot>,
slots: Vec<SlotUnion>,
free_head: u32,
num_elems: u32,
}

impl TextureIdSlotMap {
/// Constructs a new, empty [`TextureIdSlotMap`].
pub fn new() -> Self {
let slots = vec![Slot {
u: SlotUnion { next_free: 0 },
version: 0,
}];
let slots = vec![SlotUnion { next_free: 0 }];

Self {
slots,
Expand All @@ -69,9 +36,7 @@ impl TextureIdSlotMap {
/// Returns [`true`] if the slot map contains `key`.
#[inline(always)]
fn contains_key(&self, key: TextureSlotId) -> bool {
self.slots
.get(key.idx as usize)
.map_or(false, |slot| slot.version == key.version.get())
self.slots.get(key as usize).is_some()
}

/// Inserts a value into the slot map. Returns a unique key that can be used
Expand All @@ -88,54 +53,46 @@ impl TextureIdSlotMap {
}

if let Some(slot) = self.slots.get_mut(self.free_head as usize) {
let occupied_version = slot.version | 1;
let kd = TextureSlotId::new(self.free_head, occupied_version);
let kd = self.free_head;

// Update.
unsafe {
self.free_head = slot.u.next_free;
slot.u.value = value;
slot.version = occupied_version;
self.free_head = slot.next_free;
slot.value = value;
}
self.num_elems = new_num_elems;
return kd;
}

let version = 1;
let kd = TextureSlotId::new(self.slots.len() as u32, version);
let kd = self.slots.len() as u32;

// Create new slot before adjusting freelist in case f or the allocation panics or errors.
self.slots.push(Slot {
u: SlotUnion { value },
version,
});
self.slots.push(SlotUnion { value });

self.free_head = kd.idx + 1;
self.free_head = kd + 1;
self.num_elems = new_num_elems;
kd
}

/// Removes a key from the slot map if it is present.
pub fn remove(&mut self, key: TextureSlotId) {
if self.contains_key(key) {
let idx = key.idx as usize;
let idx = key as usize;

// This is safe because we know that the slot is occupied.
let slot = unsafe { self.slots.get_unchecked_mut(idx) };

// Maintain freelist.
slot.u.next_free = self.free_head;
slot.next_free = self.free_head;
self.free_head = idx as u32;
self.num_elems -= 1;
slot.version = slot.version.wrapping_add(1);
}
}

/// Returns a reference to the value corresponding to the key.
pub fn get(&self, key: TextureSlotId) -> Option<TextureId> {
self.slots
.get(key.idx as usize)
.filter(|slot| slot.version == key.version.get())
.map(|slot| unsafe { slot.u.value })
.get(key as usize)
.map(|slot| unsafe { slot.value })
}
}
Loading