Skip to content

Commit 9898893

Browse files
committed
Improve the comments
1 parent 506fc17 commit 9898893

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ idna = "1.0.3"
3434
serde = { workspace = true }
3535
serde_json = { workspace = true }
3636
seahash = "4.1.0"
37+
# rustc-hash v1.1.0 provides a better performance than 2.x, chromium pins the same version.
3738
rustc-hash = { version = "1.1.0", default-features = false }
3839
memchr = "2.4"
3940
base64 = "0.22"

src/flatbuffers/containers/hash_index.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,36 @@ use std::marker::PhantomData;
88

99
use crate::flatbuffers::containers::fb_index::FbIndex;
1010

11+
/// A trait for hash table builder keys, i.e. String.
12+
/// The default value is used to mark empty slots.
1113
pub(crate) trait HashKey: Eq + std::hash::Hash + Default + Clone {
14+
/// Returns true if the key is empty.
1215
fn is_empty(&self) -> bool;
1316
}
1417

15-
pub(crate) trait FbHashKey: Eq + std::hash::Hash {
16-
fn is_empty(&self) -> bool;
17-
}
18-
19-
impl HashKey for String {
18+
impl<T: Eq + std::hash::Hash + Default + Clone> HashKey for T {
2019
fn is_empty(&self) -> bool {
21-
self.is_empty()
20+
self == &T::default()
2221
}
2322
}
2423

24+
/// A trait for hash table view keys that can be used in flatbuffers, i.e. &str.
25+
/// The implementation must synchronized with matching HashKey trait.
26+
pub(crate) trait FbHashKey: Eq + std::hash::Hash {
27+
/// Returns true if the key is empty.
28+
fn is_empty(&self) -> bool;
29+
}
30+
2531
impl FbHashKey for &str {
2632
fn is_empty(&self) -> bool {
2733
str::is_empty(self)
2834
}
2935
}
3036

37+
/// An internal function to find a slot in the hash table for the given key.
38+
/// Returns the slot index.
39+
/// 'table_size' is the table size. It must be a power of two.
40+
/// 'probe' must return true at least for one slot (supposing the table isn't full).
3141
pub fn find_slot<I: std::hash::Hash>(
3242
key: &I,
3343
table_size: usize,
@@ -46,6 +56,9 @@ pub fn find_slot<I: std::hash::Hash>(
4656
}
4757
}
4858

59+
/// A flatbuffer-compatible view of a hash table.
60+
/// It's used to access the hash table without copying the keys and values.
61+
/// Is loaded from HashIndexBuilder data, serialized into a flatbuffer.
4962
pub(crate) struct HashIndexView<I: FbHashKey, V, Keys: FbIndex<I>, Values: FbIndex<V>> {
5063
indexes: Keys,
5164
values: Values,
@@ -79,6 +92,8 @@ impl<I: FbHashKey, V, Keys: FbIndex<I>, Values: FbIndex<V>> HashIndexView<I, V,
7992
}
8093

8194
#[cfg(test)]
95+
/// Returns the number of non-empty slots in the hash table.
96+
/// Slow, use only for tests.
8297
pub fn len(&self) -> usize {
8398
let mut len = 0;
8499
for i in 0..self.capacity() {
@@ -90,12 +105,18 @@ impl<I: FbHashKey, V, Keys: FbIndex<I>, Values: FbIndex<V>> HashIndexView<I, V,
90105
}
91106
}
92107

108+
/// A builder for a hash table.
109+
/// The default value is used to mark empty slots.
110+
/// `consume()` output is suppose to be serialized into a flatbuffer and
111+
/// used as a HashIndexView.
93112
pub(crate) struct HashIndexBuilder<I, V> {
94113
indexes: Vec<I>,
95114
values: Vec<V>,
96115
size: usize,
97116
}
98117

118+
/// An internal function to hash a key.
119+
/// The hash must be persistent across different runs of the program.
99120
fn get_hash<I: std::hash::Hash>(key: &I) -> usize {
100121
// RustC Hash is 2x faster than DefaultHasher.
101122
use rustc_hash::FxHasher;

0 commit comments

Comments
 (0)