Skip to content

Commit

Permalink
Optimize FuncType allocations (#1026)
Browse files Browse the repository at this point in the history
* optimize func type allocations

Previously the alloc method was optimized for FuncTypes that already existed which was quite uncommon. Now it is optimized for the opposite case.

* apply rustfmt
  • Loading branch information
Robbepop committed May 11, 2024
1 parent 2e7a28f commit f3e148f
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions crates/collections/src/arena/dedup.rs
@@ -1,5 +1,5 @@
use super::{Arena, ArenaIndex, Iter, IterMut};
use crate::Map;
use crate::{map, Map};
use core::{
hash::Hash,
ops::{Index, IndexMut},
Expand Down Expand Up @@ -80,23 +80,18 @@ where
Idx: ArenaIndex,
T: Hash + Ord + Clone,
{
/// Returns the next entity index.
fn next_index(&self) -> Idx {
self.entities.next_index()
}

/// Allocates a new entity and returns its index.
///
/// # Note
///
/// Only allocates if the entity does not already exist in the [`DedupArena`].
pub fn alloc(&mut self, entity: T) -> Idx {
match self.entity2idx.get(&entity) {
Some(index) => *index,
None => {
let index = self.next_index();
self.entity2idx.insert(entity.clone(), index);
match self.entity2idx.entry(entity.clone()) {
map::Entry::Occupied(entry) => *entry.get(),
map::Entry::Vacant(entry) => {
let index = self.entities.next_index();
self.entities.alloc(entity);
entry.insert(index);
index
}
}
Expand Down

0 comments on commit f3e148f

Please sign in to comment.