Skip to content

Commit c8e71a2

Browse files
committed
add rustc_hash as a feature-gated dependency
1 parent 9992044 commit c8e71a2

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ categories = ["compression"]
1010

1111
[dependencies]
1212
nanoserde = { version = "^0.1.37", optional = true }
13+
rustc-hash = { version = "1.1.0", optional = true }
1314
serde = { version = "^1.0.0", optional = true, features = ["derive"] }
1415
structdiff-derive = { path = "derive", version = "=0.6.1" }
1516

@@ -19,6 +20,7 @@ structdiff-derive = { path = "derive", version = "=0.6.1" }
1920
"serde" = ["dep:serde", "structdiff-derive/serde"]
2021
"debug_diffs" = ["structdiff-derive/debug_diffs"]
2122
"generated_setters" = ["structdiff-derive/generated_setters"]
23+
"rustc_hash" = ["dep:rustc-hash"]
2224
"debug_asserts" = []
2325

2426
[dev-dependencies]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ For more examples take a look at [integration tests](/tests)
6565
- [`nanoserde`, `serde`] - Serialization of `Difference` derived associated types. Allows diffs to easily be sent over network.
6666
- `debug_diffs` - Derive `Debug` on the generated diff type
6767
- `generated_setters` - Enable generation of setters for struct fields. These setters automatically return a diff if a field's value is changed by the assignment.
68+
- `rustc_hash` - Use the (non-cryptographic) hash implementation from the `rustc-hash` crate instead of the default hasher. Much faster diff generation for collections at the cost of a dependency.
6869

6970
### Development status
7071
This is being used actively for my own projects, although it's mostly working now. PRs will be accepted for either more tests or functionality.

src/collections/unordered_array_like.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
use nanoserde::{DeBin, SerBin};
33
#[cfg(feature = "serde")]
44
use serde::{Deserialize, Serialize};
5-
use std::{collections::HashMap, hash::Hash};
5+
#[cfg(not(feature = "rustc_hash"))]
6+
type HashMap<K, V> = std::collections::HashMap<K, V>;
7+
#[cfg(feature = "rustc_hash")]
8+
type HashMap<K, V> =
9+
std::collections::HashMap<K, V, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
10+
11+
use std::hash::Hash;
612

713
#[derive(Debug, Clone)]
814
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -88,7 +94,9 @@ impl<'a, T: Clone + 'a> From<UnorderedArrayLikeDiff<&'a T>> for UnorderedArrayLi
8894
fn collect_into_map<'a, T: Hash + PartialEq + Eq + 'a, B: Iterator<Item = T>>(
8995
list: B,
9096
) -> HashMap<T, usize> {
91-
let mut map: HashMap<T, usize> = HashMap::with_capacity(list.size_hint().1.unwrap_or_default());
97+
let mut map: HashMap<T, usize> = HashMap::default();
98+
map.reserve(list.size_hint().1.unwrap_or_default());
99+
92100
for item in list {
93101
match map.get_mut(&item) {
94102
Some(count) => *count += 1,

src/collections/unordered_map_like.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
use nanoserde::{DeBin, SerBin};
33
#[cfg(feature = "serde")]
44
use serde::{Deserialize, Serialize};
5-
use std::{collections::HashMap, hash::Hash};
5+
#[cfg(not(feature = "rustc_hash"))]
6+
type HashMap<K, V> = std::collections::HashMap<K, V>;
7+
#[cfg(feature = "rustc_hash")]
8+
type HashMap<K, V> =
9+
std::collections::HashMap<K, V, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
10+
11+
use std::hash::Hash;
612

713
#[derive(Debug, Clone)]
814
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -111,8 +117,8 @@ fn collect_into_key_eq_map<
111117
>(
112118
list: B,
113119
) -> HashMap<&'a K, (&'a V, usize)> {
114-
let mut map: HashMap<&K, (&V, usize)> =
115-
HashMap::with_capacity(list.size_hint().1.unwrap_or_default());
120+
let mut map: HashMap<&K, (&V, usize)> = HashMap::default();
121+
map.reserve(list.size_hint().1.unwrap_or_default());
116122
for (key, value) in list {
117123
match map.get_mut(&key) {
118124
Some((_, count)) => *count += 1,
@@ -132,7 +138,9 @@ fn collect_into_key_value_eq_map<
132138
>(
133139
list: B,
134140
) -> HashMap<&'a K, (&'a V, usize)> {
135-
let mut map: HashMap<&K, (&V, usize)> = HashMap::new();
141+
let mut map: HashMap<&K, (&V, usize)> = HashMap::default();
142+
map.reserve(list.size_hint().1.unwrap_or_default());
143+
136144
for (key, value) in list {
137145
match map.get_mut(&key) {
138146
Some((ref current_val, count)) => match current_val == &value {

src/collections/unordered_map_like_recursive.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ use serde::{Deserialize, Serialize};
55
#[cfg(feature = "debug_diffs")]
66
use std::fmt::Debug;
77

8-
use std::{collections::HashMap, hash::Hash, marker::PhantomData};
8+
#[cfg(not(feature = "rustc_hash"))]
9+
type HashMap<K, V> = std::collections::HashMap<K, V>;
10+
#[cfg(feature = "rustc_hash")]
11+
type HashMap<K, V> =
12+
std::collections::HashMap<K, V, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
13+
14+
use std::{hash::Hash, marker::PhantomData};
915

1016
use crate::StructDiff;
1117

@@ -110,7 +116,9 @@ fn collect_into_key_eq_map<
110116
>(
111117
list: B,
112118
) -> HashMap<&'a K, &'a V> {
113-
let mut map: HashMap<&K, &V> = HashMap::with_capacity(list.size_hint().1.unwrap_or_default());
119+
let mut map: HashMap<&K, &V> = HashMap::default();
120+
map.reserve(list.size_hint().1.unwrap_or_default());
121+
114122
for (key, value) in list {
115123
map.insert(key, value);
116124
}

0 commit comments

Comments
 (0)