1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! The append-only string interner behind label, relationship-type and
//! property-key ids (ADR-0033), and its plan-time [`TokenResolver`] seam
//! (ADR-0541).
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;
use dashmap::DashMap;
#[derive(Debug, Default)]
pub(crate) struct Interner {
to_id: DashMap<Arc<str>, u32>,
/// Dense, append-only id→string table. Entries are never removed or
/// reassigned, so a read for a committed id is a plain shard lookup; only
/// `intern` mutates (on first sight of a string).
to_str: DashMap<u32, Arc<str>>,
/// Monotonic id source. Separate from `to_str.len()` so two concurrent
/// first-time interns of distinct strings cannot observe the same len.
next_id: AtomicU32,
/// The interner's whole retained bytes: distinct interned strings
/// (`Arc` clones are not added again) plus the label-vec cache's own
/// copies. One counter, so the store's enforced logical-size account
/// sees the cache too.
bytes: AtomicU64,
/// One label vector per distinct label-id list. Walk stubs clone the
/// `Arc` instead of rebuilding the strings.
label_vecs: DashMap<Vec<u32>, Arc<Vec<String>>>,
}
#[cfg(test)]
thread_local! {
/// `Interner::get_id` calls on this thread — test-only, read through
/// [`take_get_id_calls`].
static GET_ID_CALLS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
/// Returns and resets this thread's `Interner::get_id` call count.
#[cfg(test)]
pub(crate) fn take_get_id_calls() -> u64 {
GET_ID_CALLS.with(|c| c.replace(0))
}
impl relate_names::TokenResolver for Interner {
fn resolve(&self, name: &str) -> relate_names::Token {
match self.get_id(name) {
Some(id) => relate_names::Token::Id(relate_names::TokenId(id)),
None => relate_names::Token::Absent,
}
}
}
impl Interner {
pub(crate) fn intern(&self, s: &str) -> u32 {
if let Some(id) = self.to_id.get(s) {
return *id;
}
// `entry` holds the shard write lock, so a concurrent `intern` of the
// same string takes the occupied arm and both callers get one id.
match self.to_id.entry(Arc::from(s)) {
dashmap::mapref::entry::Entry::Occupied(e) => *e.get(),
dashmap::mapref::entry::Entry::Vacant(e) => {
let arc = e.key().clone();
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
// Publish id→str before the id is reachable via `to_id`, so a
// `get(id)` that observes the id always finds the string.
self.to_str.insert(id, arc);
e.insert(id);
self.bytes.fetch_add(
crate::memory::interned_string_bytes(s.len()),
Ordering::Relaxed,
);
id
}
}
}
pub(crate) fn bytes(&self) -> u64 {
self.bytes.load(Ordering::Relaxed)
}
/// Distinct interned strings — the census harness's key count.
#[cfg(feature = "research")]
pub(crate) fn key_count(&self) -> usize {
self.to_id.len()
}
pub(crate) fn get_id(&self, s: &str) -> Option<u32> {
#[cfg(test)]
GET_ID_CALLS.with(|c| c.set(c.get() + 1));
self.to_id.get(s).map(|v| *v)
}
/// Reverse lookup of an interned id. Every reachable id was published
/// by [`Interner::intern`] before use, so a miss is a bug, not data:
/// panic instead of silently returning the empty string.
pub(crate) fn get(&self, id: u32) -> Arc<str> {
self.to_str
.get(&id)
.map(|v| Arc::clone(&*v))
.expect("interned id is published before use")
}
/// The label strings for `ids`, in id order — the interner's one
/// id→text conversion, which the stub cache and full materialization
/// both read.
pub(crate) fn label_strings(&self, ids: &[u32]) -> Vec<String> {
ids.iter().map(|id| String::from(&*self.get(*id))).collect()
}
/// The label strings for `ids`, shared with every other stub of the
/// same label list.
pub(crate) fn cached_labels(&self, ids: &[u32]) -> Arc<Vec<String>> {
if let Some(hit) = self.label_vecs.get(ids) {
return Arc::clone(hit.value());
}
match self.label_vecs.entry(ids.to_vec()) {
// A racer that lost the entry decision never builds the vector:
// the losing path only clones the winner's `Arc`.
dashmap::mapref::entry::Entry::Occupied(entry) => Arc::clone(entry.get()),
dashmap::mapref::entry::Entry::Vacant(entry) => {
let labels = Arc::new(self.label_strings(ids));
let string_bytes = labels.iter().map(|label| label.len() as u64).sum();
self.bytes.fetch_add(
crate::memory::label_vec_cache_bytes(ids.len(), string_bytes),
Ordering::Relaxed,
);
entry.insert(Arc::clone(&labels));
labels
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn intern_roundtrip() {
let i = Interner::default();
let a = i.intern("TRANSFER");
let b = i.intern("TRANSFER");
assert_eq!(a, b);
assert_eq!(&*i.get(a), "TRANSFER");
assert_eq!(i.get_id("OTHER"), None);
}
#[test]
fn resolver_matches_interner_ids() {
use relate_names::{Token, TokenId, TokenResolver};
let i = Interner::default();
// label, relationship type, property key share the interner's id space
for name in ["Person", "KNOWS", "since"] {
let id = i.intern(name);
assert_eq!(i.resolve(name), Token::Id(TokenId(id)));
}
for name in ["Absent", "MISSING", "nokey"] {
assert_eq!(i.resolve(name), Token::Absent);
}
}
#[test]
#[should_panic(expected = "interned id is published before use")]
fn get_unknown_id_panics() {
let i = Interner::default();
let _ = i.get(0);
}
/// The id table is append-only and concurrent `intern` calls race to mint
/// ids. Every distinct string must get exactly one id, repeated interns of
/// the same string must agree, and no id may ever resolve to the wrong
/// string (which the old `len()`-based counter could cause).
#[test]
fn intern_concurrent_ids_are_stable_and_unique() {
use std::collections::{HashMap, HashSet};
use std::sync::Arc as StdArc;
let interner = StdArc::new(Interner::default());
let words: Vec<String> = (0..64).map(|n| format!("label-{n}")).collect();
let handles: Vec<_> = (0..8)
.map(|t| {
let interner = StdArc::clone(&interner);
let words = words.clone();
std::thread::spawn(move || {
let mut seen = HashMap::new();
for round in 0..32 {
for n in 0..words.len() {
// Rotate start so threads interleave differently.
let w = &words[(n + t + round) % words.len()];
let id = interner.intern(w);
// Idempotent: the same string always yields the
// same id, even under concurrent first-sight.
if let Some(prev) = seen.insert(w.clone(), id) {
assert_eq!(prev, id, "id changed for {w}");
}
// The reverse direction must agree immediately.
assert_eq!(&*interner.get(id), w.as_str());
}
}
})
})
.collect();
for h in handles {
h.join().unwrap();
}
// Exactly one id per distinct string, and the id space is dense from 0.
let ids: HashSet<u32> = words.iter().map(|w| interner.intern(w)).collect();
assert_eq!(ids.len(), words.len(), "duplicate ids for distinct strings");
assert_eq!(*ids.iter().min().unwrap(), 0);
assert_eq!(*ids.iter().max().unwrap(), (words.len() - 1) as u32);
}
}