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

Re-export AHashMap and give it a trait extension #479

Merged
merged 2 commits into from
Nov 12, 2024
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
5 changes: 3 additions & 2 deletions rbx_dom_weak/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ pub fn into_raw(self) -> (Ref, HashMap<Ref, Instance, RandomState>) {
```
to
```rust
pub fn into_raw(self) -> (Ref, HashMap<Ref, Instance, ahash::RandomState>) {
pub fn into_raw(self) -> (Ref, AHashMap<Ref, Instance>) {
```

### Other changes
* Added `UstrMapExt`, a helper trait providing convenience methods `UstrMap::new` and `UstrMap::with_capacity`.
* Added `HashMapExt`, a helper trait providing convenience methods `UstrMap::new`, `UstrMap::with_capacity`, `AHashMap::new`, and `AHashMap::with_capacity`.
* Added re-exports for `AHashMap`.
* Added re-exports for `ustr` (a convenience function for creating `Ustr`s), `Ustr`, `UstrMap`, and `UstrSet`.
* Added `InstanceBuilder::with_property_capacity`, which can preallocate an `InstanceBuilder`'s property table. [#464]
* Added `WeakDom::reserve`, which can preallocate additional space for instances in the `WeakDom`. [#465]
Expand Down
20 changes: 10 additions & 10 deletions rbx_dom_weak/src/dom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::VecDeque;

use ahash::{HashMap, HashMapExt, HashSet, HashSetExt};
use ahash::{AHashMap, AHashSet};
use rbx_types::{Ref, UniqueId, Variant};
use ustr::ustr;

Expand All @@ -15,18 +15,18 @@ use crate::instance::{Instance, InstanceBuilder};
/// objects and insert them into the tree.
#[derive(Debug)]
pub struct WeakDom {
instances: HashMap<Ref, Instance>,
instances: AHashMap<Ref, Instance>,
root_ref: Ref,
unique_ids: HashSet<UniqueId>,
unique_ids: AHashSet<UniqueId>,
}

impl WeakDom {
/// Construct a new `WeakDom` described by the given [`InstanceBuilder`].
pub fn new(builder: InstanceBuilder) -> WeakDom {
let mut dom = WeakDom {
instances: HashMap::new(),
instances: AHashMap::new(),
root_ref: builder.referent,
unique_ids: HashSet::new(),
unique_ids: AHashSet::new(),
};

dom.insert(Ref::none(), builder);
Expand All @@ -42,7 +42,7 @@ impl WeakDom {
/// Consumes the WeakDom, returning its underlying root ref and backing
/// storage. This method is useful when tree-preserving operations are too
/// slow.
pub fn into_raw(self) -> (Ref, HashMap<Ref, Instance>) {
pub fn into_raw(self) -> (Ref, AHashMap<Ref, Instance>) {
(self.root_ref, self.instances)
}

Expand Down Expand Up @@ -435,24 +435,24 @@ impl<'a> Iterator for WeakDomDescendants<'a> {
impl Default for WeakDom {
fn default() -> WeakDom {
WeakDom {
instances: HashMap::new(),
instances: AHashMap::new(),
root_ref: Ref::none(),
unique_ids: HashSet::new(),
unique_ids: AHashSet::new(),
}
}
}

#[derive(Debug, Default)]
struct CloneContext {
queue: VecDeque<(Ref, Ref)>,
ref_rewrites: HashMap<Ref, Ref>,
ref_rewrites: AHashMap<Ref, Ref>,
}

impl CloneContext {
/// On any instances cloned during the operation, rewrite any Ref properties that
/// point to instances that were also cloned.
fn rewrite_refs(self, dest: &mut WeakDom) {
let mut existing_dest_refs = HashSet::new();
let mut existing_dest_refs = AHashSet::new();

for (_, new_ref) in self.ref_rewrites.iter() {
let instance = dest
Expand Down
28 changes: 22 additions & 6 deletions rbx_dom_weak/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod viewer;

pub use rbx_types as types;

pub use ahash::AHashMap;
pub use ustr::{ustr, Ustr, UstrMap, UstrSet};

pub use crate::{
Expand All @@ -55,22 +56,37 @@ pub use crate::{
viewer::{DomViewer, ViewedInstance},
};

/// Helper trait that provides convenience methods for `UstrMap`.
pub trait UstrMapExt {
/// Creates an empty `UstrMap` using the default value for its hasher.
/// Helper trait that provides convenience methods for `AHashMap` and `UstrMap`.
pub trait HashMapExt {
/// Constructs an empty map.
fn new() -> Self;

/// Creates an empty `UstrMap` with at least the specified capacity using
/// the default value for its hasher.
/// Constructs an empty map with at least the specified capacity.
fn with_capacity(capacity: usize) -> Self;
}

impl<V> UstrMapExt for UstrMap<V> {
impl<V> HashMapExt for UstrMap<V> {
/// Creates an empty `UstrMap` using the default value for its hasher.
fn new() -> Self {
UstrMap::default()
}

/// Creates an empty `UstrMap` with at least the specified capacity using
/// the default value for its hasher.
fn with_capacity(capacity: usize) -> Self {
UstrMap::with_capacity_and_hasher(capacity, Default::default())
}
}

impl<K, V> HashMapExt for AHashMap<K, V> {
/// Creates an empty `AHashMap` using the default value for its hasher.
fn new() -> Self {
AHashMap::default()
}

/// Creates an empty `AHashMap` with at least the specified capacity using
/// the default value for its hasher.
fn with_capacity(capacity: usize) -> Self {
AHashMap::with_capacity_and_hasher(capacity, Default::default())
}
}