Skip to content

Commit

Permalink
Simplify IndexedProperties::remove()
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Mar 24, 2024
1 parent 6b17728 commit d033160
Showing 1 changed file with 33 additions and 63 deletions.
96 changes: 33 additions & 63 deletions core/engine/src/object/property_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,77 +272,47 @@ impl IndexedProperties {
}
}

/// Removes a property descriptor with the specified key.
fn remove(&mut self, key: u32) -> bool {
fn convert_to_sparse_and_delete(&mut self, key: u32) -> bool {
let mut map = match self {
Self::DenseI32(vec) => {
// Fast Path: contiguous storage.

// If out of range, nothing to delete!
if key as usize >= vec.len() {
return false;
}

// If the key is pointing at the last element, then we pop it.
//
// It does not make the storage sparse.
if key as usize == vec.len().wrapping_sub(1) {
vec.pop().expect("Already checked if it is out of bounds");
return true;
}

// Slow Path: conversion to sparse storage.
Self::convert_dense_to_sparse(vec)
}
Self::DenseF64(vec) => {
// Fast Path: contiguous storage.

// If out of range, nothing to delete!
if key as usize >= vec.len() {
return false;
}

// If the key is pointing at the last element, then we pop it.
//
// It does not make the storage sparse.
if key as usize == vec.len().wrapping_sub(1) {
vec.pop().expect("Already checked if it is out of bounds");
return true;
}

// Slow Path: conversion to sparse storage.
Self::convert_dense_to_sparse(vec)
}
Self::DenseElement(vec) => {
// Fast Path: contiguous storage.

// If out of range, nothing to delete!
if key as usize >= vec.len() {
return false;
}

// If the key is pointing at the last element, then we pop it.
//
// It does not make the storage sparse.
if key as usize == vec.len().wrapping_sub(1) {
vec.pop().expect("Already checked if it is out of bounds");
return true;
}

// Slow Path: conversion to sparse storage.
Self::convert_dense_to_sparse(vec)
}
Self::Sparse(map) => {
return map.remove(&key).is_some();
}
IndexedProperties::DenseI32(vec) => Self::convert_dense_to_sparse(vec),
IndexedProperties::DenseF64(vec) => Self::convert_dense_to_sparse(vec),
IndexedProperties::DenseElement(vec) => Self::convert_dense_to_sparse(vec),
IndexedProperties::Sparse(map) => return map.remove(&key).is_some(),
};

let removed = map.remove(&key).is_some();
*self = Self::Sparse(Box::new(map));

removed
}

/// Removes a property descriptor with the specified key.
fn remove(&mut self, key: u32) -> bool {
match self {
// Fast Paths: contiguous storage.
//
// If the key is pointing at the last element, then we pop it.
Self::DenseI32(vec) if (key + 1) == vec.len() as u32 => {
vec.pop();
true
}
// If the key is out of range then don't do anything.
Self::DenseI32(vec) if key >= vec.len() as u32 => false,
Self::DenseF64(vec) if (key + 1) == vec.len() as u32 => {
vec.pop();
true
}
Self::DenseF64(vec) if key >= vec.len() as u32 => false,
Self::DenseElement(vec) if (key + 1) == vec.len() as u32 => {
vec.pop();
true
}
Self::DenseElement(vec) if key >= vec.len() as u32 => false,
// Slow Paths: non-contiguous storage.
Self::Sparse(map) => map.remove(&key).is_some(),
_ => self.convert_to_sparse_and_delete(key),
}
}

/// Check if we contain the key to a property descriptor.
fn contains_key(&self, key: u32) -> bool {
match self {
Expand Down

0 comments on commit d033160

Please sign in to comment.