probabilistic_machines 0.1.3 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +70 -0
- data/Cargo.lock +22 -3
- data/Cargo.toml +2 -0
- data/README.md +166 -58
- data/ext/probabilistic_machines_native/core/Cargo.toml +10 -5
- data/ext/probabilistic_machines_native/core/src/countmin.rs +117 -99
- data/ext/probabilistic_machines_native/core/src/cuckoo.rs +109 -81
- data/ext/probabilistic_machines_native/core/src/hash.rs +9 -17
- data/ext/probabilistic_machines_native/core/src/hyperloglog.rs +104 -92
- data/ext/probabilistic_machines_native/core/src/lib.rs +144 -22
- data/ext/probabilistic_machines_native/core/src/tdigest.rs +175 -90
- data/ext/probabilistic_machines_native/core/src/topk.rs +253 -126
- data/ext/probabilistic_machines_native/ffi/Cargo.toml +5 -11
- data/ext/probabilistic_machines_native/ffi/src/countmin.rs +111 -70
- data/ext/probabilistic_machines_native/ffi/src/cuckoo.rs +102 -44
- data/ext/probabilistic_machines_native/ffi/src/data.rs +102 -55
- data/ext/probabilistic_machines_native/ffi/src/hyperloglog.rs +92 -49
- data/ext/probabilistic_machines_native/ffi/src/init.rs +15 -0
- data/ext/probabilistic_machines_native/ffi/src/lib.rs +74 -109
- data/ext/probabilistic_machines_native/ffi/src/tdigest.rs +91 -90
- data/ext/probabilistic_machines_native/ffi/src/topk.rs +181 -136
- data/lib/probabilistic_machines/version.rb +1 -1
- data/lib/probabilistic_machines.rb +40 -2
- data/sig/probabilistic_machines.rbs +129 -56
- metadata +2 -1
|
@@ -1,24 +1,63 @@
|
|
|
1
1
|
//! Which keys dominate a stream, in `K` counters instead of one per key.
|
|
2
2
|
|
|
3
3
|
use crate::hash::usize_low_u16;
|
|
4
|
-
use crate::{
|
|
4
|
+
use crate::{Refusal, TAG_TOPK, open_frame};
|
|
5
5
|
|
|
6
|
+
/// Tag, the two-byte counter count, the eight-byte observation count and the
|
|
7
|
+
/// two-byte tracked count.
|
|
6
8
|
const HEADER_LEN: usize = 13;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const OVERESTIMATE_AT: usize = 16;
|
|
9
|
+
/// Bytes of one record field: key, count, overestimate, in that order.
|
|
10
|
+
const FIELD_LEN: usize = 8;
|
|
11
|
+
const RECORD_LEN: usize = 3 * FIELD_LEN;
|
|
11
12
|
|
|
12
13
|
/// A tracked key, its counter, and how much of that counter it may have
|
|
13
14
|
/// inherited from keys evicted before it or from a merged-in stream that
|
|
14
15
|
/// tracked it no longer.
|
|
15
16
|
///
|
|
16
|
-
/// The true count lies in `count - overestimate ..= count`.
|
|
17
|
+
/// The true count lies in `count - overestimate ..= count`. Every count a table
|
|
18
|
+
/// takes is at least one, so a tracked key has occurred at least once and its
|
|
19
|
+
/// overestimate stays below its count; [`new`](Self::new) holds a hand-built
|
|
20
|
+
/// hitter to that.
|
|
17
21
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
18
22
|
pub struct Hitter<T> {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
key: T,
|
|
24
|
+
count: u64,
|
|
25
|
+
overestimate: u64,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
impl<T> Hitter<T> {
|
|
29
|
+
/// A hitter, or `None` when `overestimate` is not below `count`: the
|
|
30
|
+
/// interval `count - overestimate ..= count` would claim no occurrence.
|
|
31
|
+
#[must_use]
|
|
32
|
+
pub fn new(key: T, count: u64, overestimate: u64) -> Option<Self> {
|
|
33
|
+
if overestimate >= count {
|
|
34
|
+
return None;
|
|
35
|
+
}
|
|
36
|
+
Some(Self {
|
|
37
|
+
key,
|
|
38
|
+
count,
|
|
39
|
+
overestimate,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// The tracked key.
|
|
44
|
+
#[must_use]
|
|
45
|
+
pub const fn key(&self) -> &T {
|
|
46
|
+
&self.key
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// Its counter: at least its true count.
|
|
50
|
+
#[must_use]
|
|
51
|
+
pub const fn count(&self) -> u64 {
|
|
52
|
+
self.count
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// How much of the counter it may have inherited: the true count is at
|
|
56
|
+
/// least `count - overestimate`.
|
|
57
|
+
#[must_use]
|
|
58
|
+
pub const fn overestimate(&self) -> u64 {
|
|
59
|
+
self.overestimate
|
|
60
|
+
}
|
|
22
61
|
}
|
|
23
62
|
|
|
24
63
|
/// Space-Saving over `K` counters: whatever is frequent ends up tracked.
|
|
@@ -35,19 +74,26 @@ pub struct Hitter<T> {
|
|
|
35
74
|
/// a `u32` id, or hashing with [`hash64`](crate::hash64) the way the Ruby
|
|
36
75
|
/// binding does, keeps the table [`Copy`] and the scan cache-friendly.
|
|
37
76
|
///
|
|
77
|
+
/// Counters are `u64`. An addition or a merge that would carry a counter or
|
|
78
|
+
/// the observation count past [`u64::MAX`] is refused with
|
|
79
|
+
/// [`Refusal::Overflow`] and changes nothing, so no counter saturates and the
|
|
80
|
+
/// bounds above always hold. `K` is at least one; a table of zero counters
|
|
81
|
+
/// does not compile.
|
|
82
|
+
///
|
|
38
83
|
/// ```
|
|
39
84
|
/// use probabilistic_machines::HeavyHitters;
|
|
40
85
|
///
|
|
41
86
|
/// let mut traffic = HeavyHitters::<u32, 8>::new();
|
|
42
87
|
/// for request in 0..1_000_u32 {
|
|
43
|
-
/// traffic.add(request % 97)
|
|
88
|
+
/// traffic.add(request % 97)?;
|
|
44
89
|
/// }
|
|
45
90
|
/// for _ in 0..5_000 {
|
|
46
|
-
/// traffic.add(7)
|
|
91
|
+
/// traffic.add(7)?;
|
|
47
92
|
/// }
|
|
48
93
|
///
|
|
49
94
|
/// assert_eq!(traffic.observed(), 6_000);
|
|
50
95
|
/// assert!(traffic.count(&7) >= 5_000);
|
|
96
|
+
/// # Ok::<(), probabilistic_machines::Refusal>(())
|
|
51
97
|
/// ```
|
|
52
98
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
53
99
|
pub struct HeavyHitters<T, const K: usize> {
|
|
@@ -56,15 +102,21 @@ pub struct HeavyHitters<T, const K: usize> {
|
|
|
56
102
|
}
|
|
57
103
|
|
|
58
104
|
impl<T: Clone + Eq, const K: usize> HeavyHitters<T, K> {
|
|
105
|
+
/// Evaluated by [`new`](Self::new), so a zero-counter table is a compile
|
|
106
|
+
/// error rather than one that silently drops every key.
|
|
107
|
+
const AT_LEAST_ONE_COUNTER: () = assert!(K >= 1, "a table needs at least one counter");
|
|
108
|
+
|
|
109
|
+
/// An empty table: every counter free.
|
|
59
110
|
#[must_use]
|
|
60
111
|
pub const fn new() -> Self {
|
|
112
|
+
let () = Self::AT_LEAST_ONE_COUNTER;
|
|
61
113
|
Self {
|
|
62
114
|
slots: [const { None }; K],
|
|
63
115
|
observed: 0,
|
|
64
116
|
}
|
|
65
117
|
}
|
|
66
118
|
|
|
67
|
-
/// Observations fed in
|
|
119
|
+
/// Observations fed in: the sum of every count added or merged.
|
|
68
120
|
#[must_use]
|
|
69
121
|
pub const fn observed(&self) -> u64 {
|
|
70
122
|
self.observed
|
|
@@ -76,6 +128,7 @@ impl<T: Clone + Eq, const K: usize> HeavyHitters<T, K> {
|
|
|
76
128
|
self.slots.iter().flatten().count()
|
|
77
129
|
}
|
|
78
130
|
|
|
131
|
+
/// Whether the table tracks no key.
|
|
79
132
|
#[must_use]
|
|
80
133
|
pub fn is_empty(&self) -> bool {
|
|
81
134
|
self.slots.iter().all(Option::is_none)
|
|
@@ -100,13 +153,34 @@ impl<T: Clone + Eq, const K: usize> HeavyHitters<T, K> {
|
|
|
100
153
|
self.hitters().max_by_key(|hitter| hitter.count)
|
|
101
154
|
}
|
|
102
155
|
|
|
103
|
-
|
|
104
|
-
|
|
156
|
+
/// Records one occurrence of `key`.
|
|
157
|
+
///
|
|
158
|
+
/// # Errors
|
|
159
|
+
///
|
|
160
|
+
/// As [`add_count`](Self::add_count).
|
|
161
|
+
pub fn add(&mut self, key: T) -> Result<(), Refusal> {
|
|
162
|
+
self.add_count(key, 1)
|
|
105
163
|
}
|
|
106
164
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
165
|
+
/// Records `count` occurrences of `key`.
|
|
166
|
+
///
|
|
167
|
+
/// # Errors
|
|
168
|
+
///
|
|
169
|
+
/// [`Refusal::ZeroCount`] when `count` is zero: it observes nothing, yet
|
|
170
|
+
/// on a full table it would evict the weakest key. [`Refusal::Overflow`]
|
|
171
|
+
/// when the observation count, the key's counter or the evicted floor plus
|
|
172
|
+
/// `count` would pass [`u64::MAX`]. The table is unchanged either way.
|
|
173
|
+
pub fn add_count(&mut self, key: T, count: u64) -> Result<(), Refusal> {
|
|
174
|
+
if count == 0 {
|
|
175
|
+
return Err(Refusal::ZeroCount);
|
|
176
|
+
}
|
|
177
|
+
let Some(observed) = self.observed.checked_add(count) else {
|
|
178
|
+
return Err(Refusal::Overflow);
|
|
179
|
+
};
|
|
180
|
+
let (slot, hitter) = self.absorbed(key, count, 0)?;
|
|
181
|
+
*slot = Some(hitter);
|
|
182
|
+
self.observed = observed;
|
|
183
|
+
Ok(())
|
|
110
184
|
}
|
|
111
185
|
|
|
112
186
|
/// Folds another table in. Counts of keys tracked by both are exact sums.
|
|
@@ -114,18 +188,38 @@ impl<T: Clone + Eq, const K: usize> HeavyHitters<T, K> {
|
|
|
114
188
|
/// up to that side's eviction floor, so it takes the floor on both its
|
|
115
189
|
/// bounds: the counter still covers the true count and the interval
|
|
116
190
|
/// `count - overestimate ..= count` still holds it.
|
|
117
|
-
|
|
191
|
+
///
|
|
192
|
+
/// # Errors
|
|
193
|
+
///
|
|
194
|
+
/// [`Refusal::Overflow`] when any counter, overestimate or the observation
|
|
195
|
+
/// count would pass [`u64::MAX`]. The merge is worked out on a copy and
|
|
196
|
+
/// stored only once every step fits, so a refused merge changes nothing.
|
|
197
|
+
pub fn merge(&mut self, other: &Self) -> Result<(), Refusal> {
|
|
198
|
+
let Some(observed) = self.observed.checked_add(other.observed) else {
|
|
199
|
+
return Err(Refusal::Overflow);
|
|
200
|
+
};
|
|
118
201
|
let floor = other.floor();
|
|
119
|
-
|
|
202
|
+
let mut merged = self.clone();
|
|
203
|
+
for tracked in merged.slots.iter_mut().flatten() {
|
|
120
204
|
if other.hitters().all(|hitter| hitter.key != tracked.key) {
|
|
121
|
-
|
|
122
|
-
|
|
205
|
+
let (Some(count), Some(overestimate)) = (
|
|
206
|
+
tracked.count.checked_add(floor),
|
|
207
|
+
tracked.overestimate.checked_add(floor),
|
|
208
|
+
) else {
|
|
209
|
+
return Err(Refusal::Overflow);
|
|
210
|
+
};
|
|
211
|
+
tracked.count = count;
|
|
212
|
+
tracked.overestimate = overestimate;
|
|
123
213
|
}
|
|
124
214
|
}
|
|
125
215
|
for hitter in other.hitters() {
|
|
126
|
-
|
|
216
|
+
let (slot, absorbed) =
|
|
217
|
+
merged.absorbed(hitter.key.clone(), hitter.count, hitter.overestimate)?;
|
|
218
|
+
*slot = Some(absorbed);
|
|
127
219
|
}
|
|
128
|
-
|
|
220
|
+
merged.observed = observed;
|
|
221
|
+
*self = merged;
|
|
222
|
+
Ok(())
|
|
129
223
|
}
|
|
130
224
|
|
|
131
225
|
/// How often an untracked key may have occurred: nothing while a counter is
|
|
@@ -137,41 +231,70 @@ impl<T: Clone + Eq, const K: usize> HeavyHitters<T, K> {
|
|
|
137
231
|
self.hitters().map(|hitter| hitter.count).min().unwrap_or(0)
|
|
138
232
|
}
|
|
139
233
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
234
|
+
/// Where `key` goes and what it holds once `count` and `overestimate` are
|
|
235
|
+
/// added: its own slot if tracked, else the first free one, else the
|
|
236
|
+
/// weakest counter's (the first of the lowest), which it takes over along
|
|
237
|
+
/// with that counter as its floor. Computes without storing, so a caller
|
|
238
|
+
/// can refuse first.
|
|
239
|
+
fn absorbed(
|
|
240
|
+
&mut self,
|
|
241
|
+
key: T,
|
|
242
|
+
count: u64,
|
|
243
|
+
overestimate: u64,
|
|
244
|
+
) -> Result<(&mut Option<Hitter<T>>, Hitter<T>), Refusal> {
|
|
245
|
+
let mut free = None;
|
|
246
|
+
let mut weakest: Option<(&mut Option<Hitter<T>>, u64)> = None;
|
|
247
|
+
for slot in &mut self.slots {
|
|
248
|
+
match slot.as_ref().map(|tracked| (tracked.key == key, tracked)) {
|
|
249
|
+
Some((true, tracked)) => {
|
|
250
|
+
let base = (tracked.count, tracked.overestimate);
|
|
251
|
+
return Self::raised(slot, key, base, count, overestimate);
|
|
252
|
+
}
|
|
253
|
+
Some((false, tracked)) => {
|
|
254
|
+
let held = tracked.count;
|
|
255
|
+
if weakest.as_ref().is_none_or(|(_, floor)| held < *floor) {
|
|
256
|
+
weakest = Some((slot, held));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
None => {
|
|
260
|
+
if free.is_none() {
|
|
261
|
+
free = Some(slot);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
150
265
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
key,
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
266
|
+
match (free, weakest) {
|
|
267
|
+
(Some(slot), _) => Self::raised(slot, key, (0, 0), count, overestimate),
|
|
268
|
+
(None, Some((slot, floor))) => {
|
|
269
|
+
Self::raised(slot, key, (floor, floor), count, overestimate)
|
|
270
|
+
}
|
|
271
|
+
(None, None) => {
|
|
272
|
+
debug_assert!(false, "K >= 1 is checked when the table is built");
|
|
273
|
+
Err(Refusal::Full)
|
|
274
|
+
}
|
|
159
275
|
}
|
|
276
|
+
}
|
|
160
277
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
278
|
+
/// `slot` with the hitter for `key` it would hold: `base` (count,
|
|
279
|
+
/// overestimate) raised by `count` and `overestimate`.
|
|
280
|
+
fn raised(
|
|
281
|
+
slot: &mut Option<Hitter<T>>,
|
|
282
|
+
key: T,
|
|
283
|
+
base: (u64, u64),
|
|
284
|
+
count: u64,
|
|
285
|
+
overestimate: u64,
|
|
286
|
+
) -> Result<(&mut Option<Hitter<T>>, Hitter<T>), Refusal> {
|
|
287
|
+
let (Some(count), Some(overestimate)) =
|
|
288
|
+
(base.0.checked_add(count), base.1.checked_add(overestimate))
|
|
166
289
|
else {
|
|
167
|
-
return;
|
|
290
|
+
return Err(Refusal::Overflow);
|
|
168
291
|
};
|
|
169
|
-
let
|
|
170
|
-
*weakest = Hitter {
|
|
292
|
+
let hitter = Hitter {
|
|
171
293
|
key,
|
|
172
|
-
count
|
|
173
|
-
overestimate
|
|
294
|
+
count,
|
|
295
|
+
overestimate,
|
|
174
296
|
};
|
|
297
|
+
Ok((slot, hitter))
|
|
175
298
|
}
|
|
176
299
|
}
|
|
177
300
|
|
|
@@ -186,46 +309,28 @@ impl<const K: usize> HeavyHitters<u64, K> {
|
|
|
186
309
|
/// Length of the [`encode`](Self::encode) frame.
|
|
187
310
|
pub const ENCODED_LEN: usize = HEADER_LEN + K * RECORD_LEN;
|
|
188
311
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
/// # Errors
|
|
196
|
-
///
|
|
197
|
-
/// [`CodecError::Truncated`] when `out` is shorter than
|
|
198
|
-
/// [`ENCODED_LEN`](Self::ENCODED_LEN).
|
|
199
|
-
pub fn encode(&self, out: &mut [u8]) -> Result<usize, CodecError> {
|
|
200
|
-
let Some(frame) = out.get_mut(..Self::ENCODED_LEN) else {
|
|
201
|
-
return Err(CodecError::Truncated);
|
|
202
|
-
};
|
|
203
|
-
self.write_frame(frame);
|
|
204
|
-
Ok(Self::ENCODED_LEN)
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/// The table's frame, exactly [`ENCODED_LEN`](Self::ENCODED_LEN) bytes.
|
|
208
|
-
#[cfg(feature = "alloc")]
|
|
209
|
-
#[must_use]
|
|
210
|
-
pub fn encoded(&self) -> alloc::boxed::Box<[u8]> {
|
|
211
|
-
let mut frame = alloc::vec![0_u8; Self::ENCODED_LEN].into_boxed_slice();
|
|
212
|
-
self.write_frame(&mut frame);
|
|
213
|
-
frame
|
|
214
|
-
}
|
|
312
|
+
encoders!(
|
|
313
|
+
"table",
|
|
314
|
+
"The frame is the tag, `K`, the observation count, the number of tracked",
|
|
315
|
+
"keys and `K` records of key, count and overestimate. Records past the",
|
|
316
|
+
"tracked count are zero."
|
|
317
|
+
);
|
|
215
318
|
|
|
216
319
|
/// Fills a frame of exactly [`ENCODED_LEN`](Self::ENCODED_LEN) bytes.
|
|
217
|
-
fn write_frame(&self,
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
for (slot, hitter) in
|
|
226
|
-
slot[
|
|
227
|
-
|
|
228
|
-
|
|
320
|
+
fn write_frame(&self, header: &mut [u8; HEADER_LEN], body: &mut [u8]) {
|
|
321
|
+
let [tag, k0, k1, observed @ .., n0, n1] = header;
|
|
322
|
+
*tag = TAG_TOPK;
|
|
323
|
+
[*k0, *k1] = Self::COUNTERS;
|
|
324
|
+
*observed = self.observed.to_le_bytes();
|
|
325
|
+
[*n0, *n1] = usize_low_u16(self.len()).to_le_bytes();
|
|
326
|
+
body.fill(0);
|
|
327
|
+
|
|
328
|
+
for (slot, hitter) in records_in_mut(body).iter_mut().zip(self.hitters()) {
|
|
329
|
+
*slot = [
|
|
330
|
+
hitter.key.to_le_bytes(),
|
|
331
|
+
hitter.count.to_le_bytes(),
|
|
332
|
+
hitter.overestimate.to_le_bytes(),
|
|
333
|
+
];
|
|
229
334
|
}
|
|
230
335
|
}
|
|
231
336
|
|
|
@@ -233,11 +338,13 @@ impl<const K: usize> HeavyHitters<u64, K> {
|
|
|
233
338
|
///
|
|
234
339
|
/// # Errors
|
|
235
340
|
///
|
|
236
|
-
/// [`
|
|
237
|
-
/// when it holds another structure,
|
|
238
|
-
/// written with a different counter
|
|
239
|
-
/// `
|
|
240
|
-
|
|
341
|
+
/// [`Refusal::Truncated`] when `bytes` is too short,
|
|
342
|
+
/// [`Refusal::WrongStructure`] when it holds another structure,
|
|
343
|
+
/// [`Refusal::WrongShape`] when it was written with a different counter
|
|
344
|
+
/// count, [`Refusal::Malformed`] when it claims more tracked keys than `K`,
|
|
345
|
+
/// holds records no stream could have left, or has bytes in the slots past
|
|
346
|
+
/// the tracked records, which `encode` always zeroes.
|
|
347
|
+
pub fn decode(bytes: &[u8]) -> Result<Self, Refusal> {
|
|
241
348
|
let mut table = Self::new();
|
|
242
349
|
table.decode_into(bytes)?;
|
|
243
350
|
Ok(table)
|
|
@@ -251,62 +358,82 @@ impl<const K: usize> HeavyHitters<u64, K> {
|
|
|
251
358
|
/// # Errors
|
|
252
359
|
///
|
|
253
360
|
/// The same as [`decode`](Self::decode).
|
|
254
|
-
pub fn decode_into(&mut self, bytes: &[u8]) -> Result<(),
|
|
255
|
-
let
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return Err(CodecError::Tag);
|
|
260
|
-
}
|
|
261
|
-
if frame[1..3] != Self::COUNTERS {
|
|
262
|
-
return Err(CodecError::Parameters);
|
|
263
|
-
}
|
|
264
|
-
let tracked = usize::from(u16::from_le_bytes([frame[11], frame[12]]));
|
|
265
|
-
if tracked > K {
|
|
266
|
-
return Err(CodecError::Parameters);
|
|
361
|
+
pub fn decode_into(&mut self, bytes: &[u8]) -> Result<(), Refusal> {
|
|
362
|
+
let (header, body) = open_frame::<HEADER_LEN>(bytes, Self::ENCODED_LEN, TAG_TOPK)?;
|
|
363
|
+
let [_, k0, k1, observed @ .., n0, n1] = *header;
|
|
364
|
+
if [k0, k1] != Self::COUNTERS {
|
|
365
|
+
return Err(Refusal::WrongShape);
|
|
267
366
|
}
|
|
367
|
+
// The body holds exactly `K` records, so this refuses exactly the
|
|
368
|
+
// tracked counts above `K`.
|
|
369
|
+
let tracked = usize::from(u16::from_le_bytes([n0, n1]));
|
|
370
|
+
let Some((records, unused)) = records_in(body).split_at_checked(tracked) else {
|
|
371
|
+
return Err(Refusal::Malformed);
|
|
372
|
+
};
|
|
268
373
|
|
|
269
|
-
let observed = u64::from_le_bytes(
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
if
|
|
273
|
-
|
|
374
|
+
let observed = u64::from_le_bytes(observed);
|
|
375
|
+
// `encode` zeroes every slot past the tracked records: one frame per
|
|
376
|
+
// state, so anything else there is not a frame this table wrote.
|
|
377
|
+
if unused
|
|
378
|
+
.as_flattened()
|
|
379
|
+
.as_flattened()
|
|
380
|
+
.iter()
|
|
381
|
+
.any(|byte| *byte != 0)
|
|
382
|
+
|| !records_are_coherent(records, observed)
|
|
383
|
+
{
|
|
384
|
+
return Err(Refusal::Malformed);
|
|
274
385
|
}
|
|
275
386
|
|
|
276
387
|
self.observed = observed;
|
|
277
|
-
|
|
278
|
-
|
|
388
|
+
let mut records = records.iter();
|
|
389
|
+
for slot in &mut self.slots {
|
|
390
|
+
*slot = records.next().map(hitter);
|
|
279
391
|
}
|
|
280
|
-
self.slots[tracked..].fill(None);
|
|
281
392
|
Ok(())
|
|
282
393
|
}
|
|
283
394
|
}
|
|
284
395
|
|
|
396
|
+
/// One record: its key, count and overestimate fields.
|
|
397
|
+
type Record = [[u8; FIELD_LEN]; 3];
|
|
398
|
+
|
|
399
|
+
/// The whole records `body` holds.
|
|
400
|
+
const fn records_in(body: &[u8]) -> &[Record] {
|
|
401
|
+
body.as_chunks::<FIELD_LEN>().0.as_chunks::<3>().0
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/// The whole records `body` holds, to fill.
|
|
405
|
+
const fn records_in_mut(body: &mut [u8]) -> &mut [Record] {
|
|
406
|
+
body.as_chunks_mut::<FIELD_LEN>().0.as_chunks_mut::<3>().0
|
|
407
|
+
}
|
|
408
|
+
|
|
285
409
|
/// A record as the hitter it stands for.
|
|
286
|
-
fn hitter(
|
|
410
|
+
const fn hitter(&[key, count, overestimate]: &Record) -> Hitter<u64> {
|
|
287
411
|
Hitter {
|
|
288
|
-
key: u64::from_le_bytes(
|
|
289
|
-
count: u64::from_le_bytes(
|
|
290
|
-
overestimate: u64::from_le_bytes(
|
|
412
|
+
key: u64::from_le_bytes(key),
|
|
413
|
+
count: u64::from_le_bytes(count),
|
|
414
|
+
overestimate: u64::from_le_bytes(overestimate),
|
|
291
415
|
}
|
|
292
416
|
}
|
|
293
417
|
|
|
294
418
|
/// Whether the records are ones Space-Saving could have tracked: a counter
|
|
295
|
-
/// stands for at least `count - overestimate` occurrences,
|
|
296
|
-
/// the observations they came from,
|
|
297
|
-
/// one counter.
|
|
419
|
+
/// stands for at least `count - overestimate` occurrences, at least one (every
|
|
420
|
+
/// count added is) and no more than the observations they came from, and a
|
|
421
|
+
/// key holds one counter.
|
|
298
422
|
///
|
|
299
423
|
/// The counter itself may pass the observation count: a merge lifts a key the
|
|
300
424
|
/// other table evicted to that table's eviction floor and widens the
|
|
301
425
|
/// overestimate with it, so the two tables' floors can stack above the two
|
|
302
426
|
/// streams' length while the occurrences the interval claims stay inside it.
|
|
303
|
-
fn records_are_coherent(records: &[
|
|
427
|
+
fn records_are_coherent(records: &[Record], observed: u64) -> bool {
|
|
304
428
|
records.iter().enumerate().all(|(index, record)| {
|
|
305
429
|
let tracked = hitter(record);
|
|
306
|
-
tracked
|
|
307
|
-
|
|
308
|
-
|
|
430
|
+
tracked
|
|
431
|
+
.count
|
|
432
|
+
.checked_sub(tracked.overestimate)
|
|
433
|
+
.is_some_and(|claimed| claimed > 0 && claimed <= observed)
|
|
434
|
+
&& records
|
|
309
435
|
.iter()
|
|
436
|
+
.take(index)
|
|
310
437
|
.all(|earlier| hitter(earlier).key != tracked.key)
|
|
311
438
|
})
|
|
312
439
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "probabilistic_machines_native"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.6"
|
|
4
4
|
edition.workspace = true
|
|
5
5
|
rust-version.workspace = true
|
|
6
6
|
authors = ["Abdelkader Boudih <oss@seuros.com>"]
|
|
@@ -15,18 +15,12 @@ crate-type = ["cdylib"]
|
|
|
15
15
|
workspace = true
|
|
16
16
|
|
|
17
17
|
[dependencies]
|
|
18
|
-
probabilistic-machines = { workspace = true, features = ["
|
|
19
|
-
magnus = { version = "
|
|
20
|
-
|
|
21
|
-
# implements the stable API for a Ruby rb-sys has no pre-generated bindings
|
|
22
|
-
# for, such as a preview. magnus asks for "stable-api" without it, and
|
|
23
|
-
# default-features = false drops the default that would have supplied it, so
|
|
24
|
-
# removing this line breaks the build on every future Ruby.
|
|
18
|
+
probabilistic-machines = { workspace = true, features = ["alloc"] }
|
|
19
|
+
magnus = { version = "0.9.3", default-features = false, features = ["bytes", "io"] }
|
|
20
|
+
matryona-machines = { workspace = true, features = ["ruby"] }
|
|
25
21
|
# Nothing here calls rb-sys directly: this is the compiled fallback that
|
|
26
22
|
# implements the stable API for a Ruby rb-sys ships no pre-generated bindings
|
|
27
23
|
# for, such as a preview. magnus asks for "stable-api" without it, and its
|
|
28
24
|
# default-features = false drops the default that would have supplied it, so
|
|
29
25
|
# dropping this line breaks the build on every Ruby newer than rb-sys.
|
|
30
|
-
rb-sys = { version = "
|
|
31
|
-
|
|
32
|
-
bytes = "1"
|
|
26
|
+
rb-sys = { version = "0.9.130", default-features = false, features = ["stable-api-compiled-fallback"] }
|