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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 12df2510a9227427fde360e27b0238e688da0f65afa802ba5ae5ff8777cdda30
|
|
4
|
+
data.tar.gz: ef809a0cf4e5b9411850f87622be84fa3dbc17277221a60a8f9f1ac94fd8c21a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 532fb0e37f5538a7cb2a816c724557fbfc2544accaed40a156f46a727474c6641be24147c1632aa7437242abbbbce22c874d16b6e88fd1ce37283ca220a7dc4b
|
|
7
|
+
data.tar.gz: edae3a084f71536e888e834a8b42a85d54c6f4bd9053c50e30e11a9aafe2ba7bceeff3e4af7f3dab63254f1240dc46201d5c7fab395ca0d85133c796bbdf65f5
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,75 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.6.1](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.6.0...probabilistic_machines/v0.6.1) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* fix wrapped classes at load so building a value runs no Ruby ([48b4a0b](https://github.com/seuros/probabilistic_machines/commit/48b4a0b4328216cb6700520f2c98e0c22da6f4aa))
|
|
9
|
+
|
|
10
|
+
## [0.6.0](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.5.0...probabilistic_machines/v0.6.0) (2026-11-16)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### ⚠ BREAKING CHANGES
|
|
14
|
+
|
|
15
|
+
* refuse a TDigest quantile outside 0..=1
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* refuse a TDigest quantile outside 0..=1 ([848858f](https://github.com/seuros/probabilistic_machines/commit/848858fd54ce7d158d419584dafa811efd25bda4))
|
|
20
|
+
|
|
21
|
+
## [0.5.0](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.4.0...probabilistic_machines/v0.5.0) (2026-11-14)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### ⚠ BREAKING CHANGES
|
|
25
|
+
|
|
26
|
+
* adopt matryona 0.4 utf8_with so key errors name the received class
|
|
27
|
+
|
|
28
|
+
### Features
|
|
29
|
+
|
|
30
|
+
* adopt matryona 0.4 utf8_with so key errors name the received class ([b6e8546](https://github.com/seuros/probabilistic_machines/commit/b6e85463277321ac5363720bb94b1dc51d4adff6))
|
|
31
|
+
|
|
32
|
+
## [0.4.0](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.3.0...probabilistic_machines/v0.4.0) (2026-11-14)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### ⚠ BREAKING CHANGES
|
|
36
|
+
|
|
37
|
+
* adopt matryona 0.3 exact strings, real/finite and named merge partners
|
|
38
|
+
|
|
39
|
+
### Features
|
|
40
|
+
|
|
41
|
+
* adopt matryona 0.3 exact strings, real/finite and named merge partners ([bea30f3](https://github.com/seuros/probabilistic_machines/commit/bea30f3f9ffac8d007535993fd6337e91113cc28))
|
|
42
|
+
|
|
43
|
+
## [0.3.0](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.2.0...probabilistic_machines/v0.3.0) (2026-11-14)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
### ⚠ BREAKING CHANGES
|
|
47
|
+
|
|
48
|
+
* adopt matryona-machines 0.2: exact merge partners, symbols! tables, caret rb-sys, missing_docs
|
|
49
|
+
|
|
50
|
+
### Features
|
|
51
|
+
|
|
52
|
+
* adopt matryona-machines 0.2: exact merge partners, symbols! tables, caret rb-sys, missing_docs ([0baae63](https://github.com/seuros/probabilistic_machines/commit/0baae63c30b5a53bdf57646cc590c81877b5ff62))
|
|
53
|
+
|
|
54
|
+
## [0.2.0](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.1.4...probabilistic_machines/v0.2.0) (2026-11-14)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
### ⚠ BREAKING CHANGES
|
|
58
|
+
|
|
59
|
+
* private Hitter/Centroid fields, refuse non-UTF-8 keys and non-canonical HeavyHitters frames, sealed exact-class values
|
|
60
|
+
|
|
61
|
+
### Features
|
|
62
|
+
|
|
63
|
+
* private Hitter/Centroid fields, refuse non-UTF-8 keys and non-canonical HeavyHitters frames, sealed exact-class values ([acbb087](https://github.com/seuros/probabilistic_machines/commit/acbb0872a0c7fb67bffab4fca2859b40019650d0))
|
|
64
|
+
|
|
65
|
+
## [0.1.4](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.1.3...probabilistic_machines/v0.1.4) (2026-11-06)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
### Bug Fixes
|
|
69
|
+
|
|
70
|
+
* keep test modules out of the packaged crate ([e3c1a1f](https://github.com/seuros/probabilistic_machines/commit/e3c1a1f7376dbada6efe782c1d4d264273483432))
|
|
71
|
+
* share frame encode/decode helpers across sketches ([067d23d](https://github.com/seuros/probabilistic_machines/commit/067d23db55b8b9068fdef445a7f6dee483961a82))
|
|
72
|
+
|
|
3
73
|
## [0.1.3](https://github.com/seuros/probabilistic_machines/compare/probabilistic_machines/v0.1.2...probabilistic_machines/v0.1.3) (2026-11-01)
|
|
4
74
|
|
|
5
75
|
|
data/Cargo.lock
CHANGED
|
@@ -134,6 +134,15 @@ dependencies = [
|
|
|
134
134
|
"syn",
|
|
135
135
|
]
|
|
136
136
|
|
|
137
|
+
[[package]]
|
|
138
|
+
name = "matryona-machines"
|
|
139
|
+
version = "0.6.0"
|
|
140
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
141
|
+
checksum = "cf8a06ab02efd767270815a1f515a40bbf3bfc77038e9360676ebe8d47833b07"
|
|
142
|
+
dependencies = [
|
|
143
|
+
"magnus",
|
|
144
|
+
]
|
|
145
|
+
|
|
137
146
|
[[package]]
|
|
138
147
|
name = "memchr"
|
|
139
148
|
version = "2.8.3"
|
|
@@ -158,14 +167,18 @@ dependencies = [
|
|
|
158
167
|
|
|
159
168
|
[[package]]
|
|
160
169
|
name = "probabilistic-machines"
|
|
161
|
-
version = "0.
|
|
170
|
+
version = "0.6.0"
|
|
171
|
+
dependencies = [
|
|
172
|
+
"matryona-machines",
|
|
173
|
+
"sliderule-machines",
|
|
174
|
+
]
|
|
162
175
|
|
|
163
176
|
[[package]]
|
|
164
177
|
name = "probabilistic_machines_native"
|
|
165
|
-
version = "0.1.
|
|
178
|
+
version = "0.1.6"
|
|
166
179
|
dependencies = [
|
|
167
|
-
"bytes",
|
|
168
180
|
"magnus",
|
|
181
|
+
"matryona-machines",
|
|
169
182
|
"probabilistic-machines",
|
|
170
183
|
"rb-sys",
|
|
171
184
|
]
|
|
@@ -271,6 +284,12 @@ version = "1.3.0"
|
|
|
271
284
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
272
285
|
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
|
273
286
|
|
|
287
|
+
[[package]]
|
|
288
|
+
name = "sliderule-machines"
|
|
289
|
+
version = "0.3.0"
|
|
290
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
291
|
+
checksum = "1948b9304f5981d079f774b7441cb6b1d5aa3da7af54854e25a256e9fd6a049b"
|
|
292
|
+
|
|
274
293
|
[[package]]
|
|
275
294
|
name = "syn"
|
|
276
295
|
version = "2.0.119"
|
data/Cargo.toml
CHANGED
|
@@ -8,10 +8,12 @@ rust-version = "1.99"
|
|
|
8
8
|
|
|
9
9
|
[workspace.dependencies]
|
|
10
10
|
probabilistic-machines = { path = "ext/probabilistic_machines_native/core" }
|
|
11
|
+
matryona-machines = { version = ">=0.6", default-features = false }
|
|
11
12
|
|
|
12
13
|
[workspace.lints.rust]
|
|
13
14
|
unsafe_code = "forbid"
|
|
14
15
|
unused_must_use = "warn"
|
|
16
|
+
missing_docs = "warn"
|
|
15
17
|
|
|
16
18
|
[workspace.lints.clippy]
|
|
17
19
|
all = { level = "warn", priority = -1 }
|
data/README.md
CHANGED
|
@@ -18,7 +18,7 @@ is the same size on the way home as it was at launch.
|
|
|
18
18
|
```ruby
|
|
19
19
|
require 'probabilistic_machines'
|
|
20
20
|
|
|
21
|
-
visitors = ProbabilisticMachines::HyperLogLog.new(
|
|
21
|
+
visitors = ProbabilisticMachines::HyperLogLog.new(4096) # 4 KiB
|
|
22
22
|
50_000.times { |id| visitors.add("user-#{id % 12_000}") }
|
|
23
23
|
|
|
24
24
|
visitors.estimate # => 12_361.2
|
|
@@ -41,20 +41,21 @@ and it needs Ruby 4.0 and a Rust toolchain (or a platform gem).
|
|
|
41
41
|
## 📐 Sizes Are a Fixed Menu
|
|
42
42
|
|
|
43
43
|
The crate sizes every structure by a const generic, so each size is a separately
|
|
44
|
-
compiled type. The
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
|
49
|
-
|
|
|
50
|
-
| `
|
|
51
|
-
| `
|
|
52
|
-
| `
|
|
53
|
-
| `
|
|
44
|
+
compiled type. The constructor takes the size positionally, as the Rust type
|
|
45
|
+
takes its parameters; the extension carries the sizes in each class's constant
|
|
46
|
+
and raises `RangeError` for anything else (`TypeError` for a non-Integer):
|
|
47
|
+
|
|
48
|
+
| Class | Constructor | Supported (constant) |
|
|
49
|
+
| ---------------- | -------------------------------- | ---------------------------------------------------- |
|
|
50
|
+
| `HyperLogLog` | `new(registers)` | `REGISTERS`: 1024, 4096, 16384, 65536 |
|
|
51
|
+
| `CountMinSketch` | `new(width, depth)` | `WIDTHS`: 1024, 4096 by `DEPTHS`: 4, 8 (every pair) |
|
|
52
|
+
| `CuckooFilter` | `new(buckets)` | `BUCKETS`: 1024, 4096, 16384, 65536 |
|
|
53
|
+
| `HeavyHitters` | `new(counters)` | `COUNTERS`: 16, 64, 256 |
|
|
54
|
+
| `TDigest` | `new(compression)` | `MIN_COMPRESSION` (10) through 65535 |
|
|
54
55
|
|
|
55
56
|
```ruby
|
|
56
|
-
ProbabilisticMachines::HyperLogLog.new(
|
|
57
|
-
#
|
|
57
|
+
ProbabilisticMachines::HyperLogLog.new(3000)
|
|
58
|
+
# RangeError: registers 3000 is not supported, expected one of [1024, 4096, 16384, 65536]
|
|
58
59
|
```
|
|
59
60
|
|
|
60
61
|
---
|
|
@@ -62,14 +63,22 @@ ProbabilisticMachines::HyperLogLog.new(registers: 3000)
|
|
|
62
63
|
## 🌌 How Many Distinct
|
|
63
64
|
|
|
64
65
|
```ruby
|
|
65
|
-
hll = ProbabilisticMachines::HyperLogLog.new(
|
|
66
|
+
hll = ProbabilisticMachines::HyperLogLog.new(4096)
|
|
66
67
|
hll.add('user-1')
|
|
67
68
|
hll.add_hash(0x9e37_79b9_7f4a_7c15) # for streams that ship hashes, not keys
|
|
68
69
|
hll.estimate # Float
|
|
69
70
|
hll.standard_error # 1.04 / sqrt(registers)
|
|
70
|
-
hll.merge(other) # exact union
|
|
71
|
+
hll.merge(other) # :merged, exact union; :wrong_shape for another size
|
|
71
72
|
```
|
|
72
73
|
|
|
74
|
+
Nothing here refuses but a merge across sizes: registers only rise, so `add`
|
|
75
|
+
and `merge` are idempotent and have no limit to hit. The estimate is the same
|
|
76
|
+
Float on every machine whose `f64` is IEEE 754 binary64, rounded per operation:
|
|
77
|
+
linear counting takes its logarithm in integer fixed point from
|
|
78
|
+
`sliderule-machines`, within `10^-10` items of the exact value. A target that
|
|
79
|
+
evaluates in wider registers (32-bit x86 with the x87 FPU, no SSE2) may round
|
|
80
|
+
differently in the last bit.
|
|
81
|
+
|
|
73
82
|
Memory is one byte per register, so 4096 registers cost 4 KiB whether the stream
|
|
74
83
|
held ten ids or ten billion.
|
|
75
84
|
|
|
@@ -78,13 +87,20 @@ held ten ids or ten billion.
|
|
|
78
87
|
## 📊 How Often Each
|
|
79
88
|
|
|
80
89
|
```ruby
|
|
81
|
-
cms = ProbabilisticMachines::CountMinSketch.new(
|
|
82
|
-
cms.add('/login'
|
|
83
|
-
cms.
|
|
84
|
-
cms.
|
|
85
|
-
cms.
|
|
90
|
+
cms = ProbabilisticMachines::CountMinSketch.new(4096, 4)
|
|
91
|
+
cms.add('/login') # :added
|
|
92
|
+
cms.add_count('/login', 3) # :added, or :zero_count / :overflow
|
|
93
|
+
cms.add_hash_count(hash, 3) # for streams that ship hashes
|
|
94
|
+
cms.estimate('/login') # never below the truth
|
|
95
|
+
cms.estimate_hash(hash)
|
|
96
|
+
cms.total # every count added
|
|
97
|
+
cms.merge(other) # :merged, exact sum; :overflow, :wrong_shape
|
|
86
98
|
```
|
|
87
99
|
|
|
100
|
+
A counter is 32 bits. An addition or merge that would carry one past
|
|
101
|
+
`2**32 - 1` answers `:overflow` and changes nothing, so a counter never sticks
|
|
102
|
+
below the truth and the lower bound always holds.
|
|
103
|
+
|
|
88
104
|
A count is over the truth by at most `e * total / width`, with probability
|
|
89
105
|
`1 - e ** -depth` of staying inside it. Width buys accuracy, depth buys
|
|
90
106
|
confidence.
|
|
@@ -94,47 +110,63 @@ confidence.
|
|
|
94
110
|
## 📏 Quantiles
|
|
95
111
|
|
|
96
112
|
```ruby
|
|
97
|
-
td = ProbabilisticMachines::TDigest.new(
|
|
98
|
-
td.add(12.5)
|
|
99
|
-
td.
|
|
113
|
+
td = ProbabilisticMachines::TDigest.new(100)
|
|
114
|
+
td.add(12.5) # :added, or :not_finite / :overflow
|
|
115
|
+
td.add_weighted(12.5, 1_000.0) # one point standing for a thousand observations
|
|
100
116
|
td.quantile(0.99)
|
|
101
117
|
td.cdf(12.5)
|
|
102
|
-
td.centroids
|
|
103
|
-
td.minimum
|
|
104
|
-
td.
|
|
118
|
+
td.centroids # Array of frozen Centroid(mean, weight)
|
|
119
|
+
td.minimum # exact, as is maximum
|
|
120
|
+
td.compress
|
|
121
|
+
td.merge(other) # :merged; :wrong_shape for another compression
|
|
105
122
|
```
|
|
106
123
|
|
|
124
|
+
A NaN or infinite value or weight is `:not_finite`, a zero or negative weight
|
|
125
|
+
`:invalid_weight`, a total weight that would become infinite `:overflow`; each
|
|
126
|
+
changes nothing. Digests of different compressions do not merge: compression
|
|
127
|
+
only joins clusters, so the coarser one's clusters would break the finer one's
|
|
128
|
+
weight bound.
|
|
129
|
+
|
|
107
130
|
A quantile is off by about `1 / compression` of the distribution in the middle
|
|
108
131
|
and is exact at the extremes, in `compression * ln(n)` clusters rather than `n`
|
|
109
|
-
samples. An empty digest answers `Float::NAN`.
|
|
132
|
+
samples. An empty digest answers `Float::NAN`. `quantile(q)` raises
|
|
133
|
+
`RangeError` for a `q` outside `0..=1`, NaN included; the Rust core answers
|
|
134
|
+
`f64::NAN` there instead.
|
|
110
135
|
|
|
111
136
|
---
|
|
112
137
|
|
|
113
138
|
## 🛰️ Have I Seen This Contact Before
|
|
114
139
|
|
|
115
140
|
```ruby
|
|
116
|
-
cf = ProbabilisticMachines::CuckooFilter.new(
|
|
117
|
-
cf.insert('device-9') #
|
|
118
|
-
cf.
|
|
119
|
-
cf.remove('device-9') # unlike a Bloom filter, it can forget
|
|
141
|
+
cf = ProbabilisticMachines::CuckooFilter.new(4096)
|
|
142
|
+
cf.insert('device-9') # :inserted, or :full when wedged
|
|
143
|
+
cf.contains?('device-9')
|
|
144
|
+
cf.remove('device-9') # :cleared, or :not_held; unlike a Bloom filter, it can forget
|
|
145
|
+
cf.insert_hash(hash) # and contains_hash?, remove_hash
|
|
120
146
|
```
|
|
121
147
|
|
|
122
148
|
`buckets * 4` items in `buckets * 4` bytes. Never a false negative; about 3%
|
|
123
|
-
false positives.
|
|
124
|
-
|
|
149
|
+
false positives. The filter stores fingerprints and nothing else, so `remove`
|
|
150
|
+
answers about the fingerprint: `:not_held` proves it is in neither of the
|
|
151
|
+
item's buckets (nothing changed), while `:cleared` is no proof the item was
|
|
152
|
+
inserted, because a colliding item's fingerprint looks the same. Only remove
|
|
153
|
+
items you inserted, or you take the colliding item's fingerprint with it.
|
|
154
|
+
Inserting an item twice stores two copies. A cuckoo filter cannot merge, so
|
|
155
|
+
there is no `merge` here.
|
|
125
156
|
|
|
126
157
|
---
|
|
127
158
|
|
|
128
159
|
## 👑 Who Dominates the Stream
|
|
129
160
|
|
|
130
161
|
```ruby
|
|
131
|
-
hh = ProbabilisticMachines::HeavyHitters.new(
|
|
132
|
-
hh.add('10.0.0.1')
|
|
133
|
-
hh.
|
|
134
|
-
hh.top
|
|
135
|
-
hh.ranked
|
|
136
|
-
hh.
|
|
137
|
-
hh.
|
|
162
|
+
hh = ProbabilisticMachines::HeavyHitters.new(64)
|
|
163
|
+
hh.add('10.0.0.1') # :added
|
|
164
|
+
hh.add_count('10.0.0.1', 5) # :added, or :zero_count / :overflow
|
|
165
|
+
hh.top # Hitter(key, count, overestimate), nil when empty
|
|
166
|
+
hh.ranked # every tracked key, most frequent first
|
|
167
|
+
hh.hitters # every tracked key, in table order
|
|
168
|
+
hh.observed # observations fed in
|
|
169
|
+
hh.merge(other) # :merged; :overflow, :wrong_shape
|
|
138
170
|
```
|
|
139
171
|
|
|
140
172
|
After `n` observations every key seen more than `n / k` times is tracked, in `k`
|
|
@@ -142,47 +174,123 @@ counters and no heap per key. A counter can inherit the count of the key it
|
|
|
142
174
|
evicted, or what a merged-in table's own evictions may have hidden, so the true
|
|
143
175
|
count lies between `count - overestimate` and `count`.
|
|
144
176
|
|
|
145
|
-
Keys are UTF-8 strings here
|
|
146
|
-
|
|
147
|
-
hash came from, to hand back
|
|
148
|
-
|
|
177
|
+
Keys are UTF-8 strings here. Space-Saving takes any `Clone + Eq` key in Rust;
|
|
178
|
+
the extension counts 64-bit key hashes, so the table stays one fixed array, and
|
|
179
|
+
keeps the string each tracked hash came from, to hand back in each `Hitter`. A
|
|
180
|
+
key that is not exactly a String (a subclass instance or a `to_str` object
|
|
181
|
+
included) is a `TypeError`, one that is not UTF-8 text a `RangeError` (a valid
|
|
182
|
+
UTF-8 String, or ASCII bytes in any ASCII-compatible encoding; a binary String
|
|
183
|
+
holding UTF-8 bytes is not transcoded), and the key a `Hitter` holds is a
|
|
184
|
+
frozen copy. The other four classes take exactly a String of any bytes in any
|
|
185
|
+
encoding, read natively (no `#bytes` or `#to_str` runs). A zero count is `:zero_count`: it observes nothing, yet on a full
|
|
186
|
+
table it would evict the weakest key.
|
|
149
187
|
|
|
150
188
|
---
|
|
151
189
|
|
|
152
190
|
## 📦 Transmitting a Reading
|
|
153
191
|
|
|
154
|
-
`
|
|
155
|
-
|
|
156
|
-
|
|
192
|
+
`encoded` returns the crate's own byte frame, `decode` reads one back, and
|
|
193
|
+
`decode_into` reads one over a sketch you already hold. The frame names its own
|
|
194
|
+
shape, so `decode` needs no size. It reads the frame's fields against each
|
|
157
195
|
other and refuses a state no stream could have left behind, so an edited blob
|
|
158
|
-
|
|
196
|
+
answers `:malformed` instead of handing back a sketch that counts more than it
|
|
197
|
+
ever saw. Like every refusal, these are symbols, never exceptions.
|
|
159
198
|
|
|
160
199
|
```ruby
|
|
161
|
-
blob = hll.
|
|
162
|
-
ProbabilisticMachines::HyperLogLog.
|
|
163
|
-
ProbabilisticMachines::TDigest.
|
|
200
|
+
blob = hll.encoded # binary String
|
|
201
|
+
ProbabilisticMachines::HyperLogLog.decode(blob) # a HyperLogLog
|
|
202
|
+
ProbabilisticMachines::TDigest.decode(blob) # => :wrong_structure
|
|
203
|
+
hll.decode_into(other_size.encoded) # => :wrong_shape, hll untouched
|
|
164
204
|
```
|
|
165
205
|
|
|
166
206
|
Anything that reads the crate reads these frames, so an embedded device can ship
|
|
167
207
|
a sketch a Rails app folds in. `HeavyHitters` is the one blob that is not purely
|
|
168
208
|
the crate's: the crate's frame counts 64-bit key hashes, so the extension appends
|
|
169
209
|
its key table after it, one record of a 64-bit byte length and that many UTF-8
|
|
170
|
-
bytes per tracked key. `
|
|
171
|
-
table is
|
|
210
|
+
bytes per tracked key. `decode` rehashes each name, and answers `:malformed` for
|
|
211
|
+
a key table that is cut short or does not name every key the frame tracks.
|
|
172
212
|
|
|
173
213
|
```ruby
|
|
174
|
-
restored = ProbabilisticMachines::HeavyHitters.
|
|
175
|
-
restored.top
|
|
214
|
+
restored = ProbabilisticMachines::HeavyHitters.decode(hh.encoded)
|
|
215
|
+
restored.top.key # => '10.0.0.1', the string, not its hash
|
|
176
216
|
```
|
|
177
217
|
|
|
178
218
|
---
|
|
179
219
|
|
|
220
|
+
## 📜 The Contract
|
|
221
|
+
|
|
222
|
+
- **A refusal is a symbol and changes nothing.** It is the snake_case of the
|
|
223
|
+
core's `Refusal` variant; there is no catch-all symbol and no refusal raises.
|
|
224
|
+
- **Exceptions are for malformed calls.** Wrong type: `TypeError`. An Integer
|
|
225
|
+
outside its range, a size off the menu, a compression below 10, a `Hitter`
|
|
226
|
+
or `Centroid` the core would not build: `RangeError`. Mutating a frozen
|
|
227
|
+
sketch: `FrozenError`. `ArgumentError` only for a wrong number of arguments.
|
|
228
|
+
- **Values are valid or do not exist.** `Hitter.new(key, count, overestimate)`
|
|
229
|
+
needs `overestimate < count`; `Centroid.new(mean, weight)` a finite mean and
|
|
230
|
+
a finite, positive weight. A value you build is held to the same rule, by the
|
|
231
|
+
core, as one the gem returns. Both classes are sealed: subclassing one raises
|
|
232
|
+
`TypeError` and `allocate` is undefined. Sealing stops accidents, not
|
|
233
|
+
reflection (a named subclass's constant is bound before the raise,
|
|
234
|
+
`Marshal.load` skips `initialize`); the sketches only build these values and
|
|
235
|
+
take none back, so a forged one has no way into a sketch. The classes are
|
|
236
|
+
resolved once, when the extension loads.
|
|
237
|
+
- **One honoured repeat.** `CuckooFilter#remove` of an item whose fingerprint
|
|
238
|
+
is in neither bucket answers `:not_held` and changes nothing. Nothing else is
|
|
239
|
+
a repeat: a second `insert` stores a second copy, a second `add` counts again.
|
|
240
|
+
- **Determinism assumes binary64.** Every Float answer (estimates,
|
|
241
|
+
quantiles, cdf) is the same bits for the same state on any machine whose
|
|
242
|
+
`f64` arithmetic is IEEE 754 binary64 with one rounding per operation,
|
|
243
|
+
which is every 64-bit target Ruby runs on. x87 extended-precision
|
|
244
|
+
evaluation is outside that promise. Frames carry raw bits, so a blob
|
|
245
|
+
decodes to the same state everywhere.
|
|
246
|
+
- **No clock, no ids, no restarts.** No sketch reads time or issues a handle;
|
|
247
|
+
what survives a restart is what you `encoded`.
|
|
248
|
+
|
|
249
|
+
### Every answer
|
|
250
|
+
|
|
251
|
+
| Call | Honoured | Refusals |
|
|
252
|
+
| --- | --- | --- |
|
|
253
|
+
| `HyperLogLog#add`, `#add_hash` | `nil` | none |
|
|
254
|
+
| `HyperLogLog#merge(other)` | `:merged` | `:wrong_shape` |
|
|
255
|
+
| `CountMinSketch#add(item)` | `:added` | `:overflow` |
|
|
256
|
+
| `CountMinSketch#add_count(item, n)`, `#add_hash_count(hash, n)` | `:added` | `:zero_count`, `:overflow` |
|
|
257
|
+
| `CountMinSketch#merge(other)` | `:merged` | `:overflow`, `:wrong_shape` |
|
|
258
|
+
| `CuckooFilter#insert(item)`, `#insert_hash(hash)` | `:inserted` | `:full` |
|
|
259
|
+
| `CuckooFilter#remove(item)`, `#remove_hash(hash)` | `:cleared`, `:not_held` (*repeat*) | none |
|
|
260
|
+
| `HeavyHitters#add(key)` | `:added` | `:overflow` |
|
|
261
|
+
| `HeavyHitters#add_count(key, n)` | `:added` | `:zero_count`, `:overflow` |
|
|
262
|
+
| `HeavyHitters#merge(other)` | `:merged` | `:overflow`, `:wrong_shape` |
|
|
263
|
+
| `TDigest#add(value)` | `:added` | `:not_finite`, `:overflow` |
|
|
264
|
+
| `TDigest#add_weighted(value, weight)` | `:added` | `:not_finite`, `:invalid_weight`, `:overflow` |
|
|
265
|
+
| `TDigest#merge(other)` | `:merged` | `:wrong_shape`, `:overflow` |
|
|
266
|
+
| `TDigest#compress` | `nil` | none |
|
|
267
|
+
| `.decode(bytes)` | the sketch | `:truncated`, `:wrong_structure`, `:wrong_shape` (not `TDigest`), `:malformed` (not `CuckooFilter`) |
|
|
268
|
+
| `#decode_into(bytes)` | `:decoded` | as `.decode` |
|
|
269
|
+
|
|
270
|
+
Queries (`estimate`, `contains?`, `count`, `top`, `quantile`, ...) answer values
|
|
271
|
+
and never refuse. No answer depends on or records a clock.
|
|
272
|
+
|
|
273
|
+
| Refusal | Meaning |
|
|
274
|
+
| --- | --- |
|
|
275
|
+
| `:truncated` | The blob is shorter than the frame it starts. |
|
|
276
|
+
| `:wrong_structure` | The blob is another sketch's frame. |
|
|
277
|
+
| `:wrong_shape` | The blob, or the sketch given to `merge`, has another size or compression. In Rust a merge across sizes does not compile. |
|
|
278
|
+
| `:malformed` | The blob holds contents no sequence of calls produces. |
|
|
279
|
+
| `:full` | The cuckoo filter found no slot after 64 evictions; expect it past about 95% of `capacity`. |
|
|
280
|
+
| `:overflow` | A counter or total would pass `2**32 - 1` (count-min), `2**64 - 1` (heavy hitters) or stay finite (t-digest weight). |
|
|
281
|
+
| `:zero_count` | A count of zero. |
|
|
282
|
+
| `:not_finite` | A t-digest value or weight is NaN or infinite. |
|
|
283
|
+
| `:invalid_weight` | A t-digest weight is zero or negative. |
|
|
284
|
+
|
|
285
|
+
---
|
|
286
|
+
|
|
180
287
|
## ⚙️ The Rust Core
|
|
181
288
|
|
|
182
289
|
The sketches themselves are the `probabilistic-machines` crate under
|
|
183
|
-
`ext/probabilistic_machines_native/core`: `no_std`,
|
|
184
|
-
|
|
185
|
-
|
|
290
|
+
`ext/probabilistic_machines_native/core`: `no_std`, `unsafe` forbidden, one
|
|
291
|
+
dependency (`sliderule-machines`, for integer logarithms), allocating only for
|
|
292
|
+
`TDigest` and for the `encoded` frames this extension returns, and verified on
|
|
293
|
+
bare metal. The binding crate beside it under
|
|
186
294
|
`ffi/` is the Ruby surface and is not published.
|
|
187
295
|
|
|
188
296
|
---
|
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "probabilistic-machines"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.6.0"
|
|
4
4
|
edition.workspace = true
|
|
5
5
|
rust-version.workspace = true
|
|
6
6
|
authors = ["Abdelkader Boudih <oss@seuros.com>"]
|
|
7
7
|
license = "MIT"
|
|
8
|
-
description = "Sketches for distinct counts, quantiles and frequencies - no_std,
|
|
8
|
+
description = "Sketches for distinct counts, quantiles and frequencies - no_std, fixed-size except the t-digest"
|
|
9
9
|
repository = "https://github.com/seuros/probabilistic_machines"
|
|
10
10
|
keywords = ["hyperloglog", "t-digest", "count-min", "sketch", "no-std"]
|
|
11
11
|
categories = ["no-std", "algorithms", "data-structures"]
|
|
12
12
|
readme = "README.md"
|
|
13
|
-
|
|
13
|
+
exclude = ["src/tests/", "src/**/tests.rs", "tests/"]
|
|
14
14
|
[lib]
|
|
15
15
|
name = "probabilistic_machines"
|
|
16
16
|
crate-type = ["lib"]
|
|
17
17
|
|
|
18
18
|
[features]
|
|
19
|
-
default = ["
|
|
20
|
-
std = ["alloc"]
|
|
19
|
+
default = ["alloc"]
|
|
21
20
|
alloc = []
|
|
22
21
|
|
|
22
|
+
[dependencies]
|
|
23
|
+
sliderule-machines = ">=0.3"
|
|
24
|
+
|
|
25
|
+
[dev-dependencies]
|
|
26
|
+
matryona-machines = { workspace = true, features = ["testing"] }
|
|
27
|
+
|
|
23
28
|
[lints]
|
|
24
29
|
workspace = true
|