mnemo-engine 0.8.0__tar.gz → 0.9.0__tar.gz
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.
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/PKG-INFO +1 -1
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/Cargo.lock +43 -2
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/Cargo.toml +13 -1
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/benches/corpus.rs +6 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/benches/open.rs +41 -1
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/benches/recall.rs +0 -38
- mnemo_engine-0.9.0/mnemo/examples/open_rss.rs +254 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/bin/mnemo.rs +519 -5
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/lib.rs +13 -3
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/mcp.rs +578 -3
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/memory.rs +505 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/store.rs +1427 -8
- mnemo_engine-0.9.0/mnemo/src/transcript.rs +324 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/tests/cli_smoke.rs +99 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/tests/integration.rs +826 -4
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo-python/Cargo.lock +2 -2
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo-python/Cargo.toml +1 -1
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo-python/src/lib.rs +507 -33
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo-python/test_mnemo.py +161 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/pyproject.toml +1 -1
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/README.md +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/.gitignore +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/README.md +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/benches/cache.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/benches/embed_overhead.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/benches/write.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/deny.toml +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/examples/ann_defaults_v0_6_0.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/examples/ann_diagnose.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/examples/ann_recall_grid.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/examples/ann_recall_grid_v2.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/examples/embedder_server.py +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/examples/file_size_breakdown.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/examples/quickstart.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/examples/ranking_v2.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/examples/session.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/cache.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/crypto.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/embed.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/embed_cmd.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/error.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/format.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/index.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/pager.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/result_cache.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/session.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/src/wal.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/tests/dur1_torn_header.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/tests/fix1_correctness_sweep.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/tests/proptest_store.proptest-regressions +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/tests/proptest_store.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo/tests/sec1_nonce_uniqueness.rs +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo-python/.gitignore +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/mnemo-python/README.md +0 -0
- {mnemo_engine-0.8.0 → mnemo_engine-0.9.0}/python/mnemo_engine/__init__.py +0 -0
|
@@ -183,6 +183,16 @@ version = "0.3.0"
|
|
|
183
183
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
184
184
|
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
|
185
185
|
|
|
186
|
+
[[package]]
|
|
187
|
+
name = "cc"
|
|
188
|
+
version = "1.11.1"
|
|
189
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
190
|
+
checksum = "4ff2a8126fff0f8c839080d619675a585e61720bb12d2baceae45769fd66f155"
|
|
191
|
+
dependencies = [
|
|
192
|
+
"find-msvc-tools",
|
|
193
|
+
"shlex",
|
|
194
|
+
]
|
|
195
|
+
|
|
186
196
|
[[package]]
|
|
187
197
|
name = "cfg-if"
|
|
188
198
|
version = "1.0.5"
|
|
@@ -420,6 +430,12 @@ version = "2.5.0"
|
|
|
420
430
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
421
431
|
checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223"
|
|
422
432
|
|
|
433
|
+
[[package]]
|
|
434
|
+
name = "find-msvc-tools"
|
|
435
|
+
version = "0.1.15"
|
|
436
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
437
|
+
checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48"
|
|
438
|
+
|
|
423
439
|
[[package]]
|
|
424
440
|
name = "fnv"
|
|
425
441
|
version = "1.0.7"
|
|
@@ -596,6 +612,15 @@ version = "0.2.16"
|
|
|
596
612
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
597
613
|
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
|
|
598
614
|
|
|
615
|
+
[[package]]
|
|
616
|
+
name = "libmimalloc-sys"
|
|
617
|
+
version = "0.1.49"
|
|
618
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
619
|
+
checksum = "6a45a52f43e1c16f667ccfe4dd8c85b7f7c204fd5e3bf46c5b0db9a5c3c0b8e9"
|
|
620
|
+
dependencies = [
|
|
621
|
+
"cc",
|
|
622
|
+
]
|
|
623
|
+
|
|
599
624
|
[[package]]
|
|
600
625
|
name = "linux-raw-sys"
|
|
601
626
|
version = "0.4.15"
|
|
@@ -608,9 +633,18 @@ version = "2.8.3"
|
|
|
608
633
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
609
634
|
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
|
610
635
|
|
|
636
|
+
[[package]]
|
|
637
|
+
name = "mimalloc"
|
|
638
|
+
version = "0.1.43"
|
|
639
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
640
|
+
checksum = "68914350ae34959d83f732418d51e2427a794055d0b9529f48259ac07af65633"
|
|
641
|
+
dependencies = [
|
|
642
|
+
"libmimalloc-sys",
|
|
643
|
+
]
|
|
644
|
+
|
|
611
645
|
[[package]]
|
|
612
646
|
name = "mnemo-engine"
|
|
613
|
-
version = "0.
|
|
647
|
+
version = "0.9.0"
|
|
614
648
|
dependencies = [
|
|
615
649
|
"aes-gcm",
|
|
616
650
|
"argon2",
|
|
@@ -618,6 +652,7 @@ dependencies = [
|
|
|
618
652
|
"clap",
|
|
619
653
|
"criterion",
|
|
620
654
|
"fs4",
|
|
655
|
+
"mimalloc",
|
|
621
656
|
"proptest",
|
|
622
657
|
"rand",
|
|
623
658
|
"rmp",
|
|
@@ -634,7 +669,7 @@ dependencies = [
|
|
|
634
669
|
|
|
635
670
|
[[package]]
|
|
636
671
|
name = "mnemo-node"
|
|
637
|
-
version = "0.
|
|
672
|
+
version = "0.9.0"
|
|
638
673
|
dependencies = [
|
|
639
674
|
"mnemo-engine",
|
|
640
675
|
"napi",
|
|
@@ -1075,6 +1110,12 @@ dependencies = [
|
|
|
1075
1110
|
"digest",
|
|
1076
1111
|
]
|
|
1077
1112
|
|
|
1113
|
+
[[package]]
|
|
1114
|
+
name = "shlex"
|
|
1115
|
+
version = "2.0.1"
|
|
1116
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
1117
|
+
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
|
1118
|
+
|
|
1078
1119
|
[[package]]
|
|
1079
1120
|
name = "slab"
|
|
1080
1121
|
version = "0.4.12"
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
# The CLI binary name stays `mnemo`, so users still invoke `mnemo` on
|
|
11
11
|
# the command line after `cargo install mnemo-engine`.
|
|
12
12
|
name = "mnemo-engine"
|
|
13
|
-
version = "0.
|
|
13
|
+
version = "0.9.0"
|
|
14
14
|
edition = "2021"
|
|
15
15
|
rust-version = "1.75"
|
|
16
16
|
# `benches/corpus.rs` is a shared helper module pulled into real benches
|
|
@@ -108,6 +108,13 @@ sha2 = "=0.10.8"
|
|
|
108
108
|
# published crate carries criterion only as an optional feature,
|
|
109
109
|
# never resolved by default consumers.
|
|
110
110
|
criterion = { version = "=0.5.1", features = ["html_reports"], optional = true }
|
|
111
|
+
# Phase 12.1 MEM-1 open_rss example variant 4 (`#[global_allocator]`).
|
|
112
|
+
# Pure Rust microsoft/mimalloc binding; gated behind `mimalloc_bench`
|
|
113
|
+
# so default consumers of the crate don't carry the dep. Lives under
|
|
114
|
+
# `[dependencies]` for the same reason criterion does — `optional`
|
|
115
|
+
# fields are only honoured on top-level deps, and we need the example
|
|
116
|
+
# binary (built from this crate) to link against it when requested.
|
|
117
|
+
mimalloc = { version = "=0.1.43", default-features = false, optional = true }
|
|
111
118
|
|
|
112
119
|
[dev-dependencies]
|
|
113
120
|
tempfile = "=3.10.1"
|
|
@@ -156,6 +163,11 @@ failpoints = []
|
|
|
156
163
|
# `--all-targets --features failpoints,benches`; the MSRV CI job
|
|
157
164
|
# never sees them. Not for production use.
|
|
158
165
|
benches = ["dep:criterion"]
|
|
166
|
+
# Phase 12.1 MEM-1 open_rss example variant 4: swap the global
|
|
167
|
+
# allocator for mimalloc inside the example binary only. Default
|
|
168
|
+
# consumers do not carry the dep. Combine with `--features benches`
|
|
169
|
+
# to actually build the example.
|
|
170
|
+
mimalloc_bench = ["dep:mimalloc"]
|
|
159
171
|
|
|
160
172
|
[profile.release]
|
|
161
173
|
lto = true
|
|
@@ -71,6 +71,12 @@ pub fn secure_kdf_cfg(dimensions: usize) -> MnemoConfig {
|
|
|
71
71
|
/// show that advantage.
|
|
72
72
|
#[allow(dead_code)]
|
|
73
73
|
pub fn gen_clustered_vectors(n: usize, dims: usize, k_clusters: usize, seed: u64) -> Vec<Vec<f32>> {
|
|
74
|
+
// MEM-1: a swapped (seed, k_clusters) pair once asked for
|
|
75
|
+
// 0xDEAD_BEEF ≈ 3.7 billion centres and read as a 13–43 GB "leak".
|
|
76
|
+
assert!(
|
|
77
|
+
(1..=n.max(1_000)).contains(&k_clusters),
|
|
78
|
+
"k_clusters looks like a swapped argument"
|
|
79
|
+
);
|
|
74
80
|
// Hand-rolled splitmix64 → xoroshiro128+ because the `rand` crate's
|
|
75
81
|
// RNG API moved between 0.8 and 0.9 and we're pinned to 0.8.6; a
|
|
76
82
|
// tiny deterministic RNG here keeps the benches independent of
|
|
@@ -29,7 +29,10 @@ use mnemo_engine::{Memory, MemoryType, Mnemo};
|
|
|
29
29
|
|
|
30
30
|
#[path = "corpus.rs"]
|
|
31
31
|
mod corpus;
|
|
32
|
-
use corpus::{
|
|
32
|
+
use corpus::{
|
|
33
|
+
fast_kdf_cfg, gen_clustered_vectors, secure_kdf_cfg, setup_cached_corpus, DIMENSIONS,
|
|
34
|
+
PASSPHRASE,
|
|
35
|
+
};
|
|
33
36
|
|
|
34
37
|
const OPEN_CORPUS_SIZE: usize = 100;
|
|
35
38
|
|
|
@@ -103,9 +106,46 @@ fn bench_open_latency_secure_kdf(c: &mut Criterion) {
|
|
|
103
106
|
group.finish();
|
|
104
107
|
}
|
|
105
108
|
|
|
109
|
+
/// Phase 12.1.1 lane-A DISPATCH remeasure (v0.8.1): open latency on an
|
|
110
|
+
/// **indexed** 10K-memory file with fast KDF. This isolates the
|
|
111
|
+
/// non-KDF cost dominated by catalog + ANN-index load. Pairs with the
|
|
112
|
+
/// 100K variant below so the README's "open ~15 ms" line can be
|
|
113
|
+
/// replaced with numbers that reflect what agents actually pay.
|
|
114
|
+
fn bench_open_latency_indexed_10k(c: &mut Criterion) {
|
|
115
|
+
let cached = setup_cached_corpus(10_000, DIMENSIONS);
|
|
116
|
+
let mut group = c.benchmark_group("open");
|
|
117
|
+
group.sample_size(30);
|
|
118
|
+
group.bench_function("open_latency_indexed_10k", |b| {
|
|
119
|
+
b.iter(|| {
|
|
120
|
+
let db = Mnemo::open(&cached, PASSPHRASE).expect("open");
|
|
121
|
+
black_box(db);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
group.finish();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/// Phase 12.1.1 lane-A DISPATCH remeasure (v0.8.1): open latency on an
|
|
128
|
+
/// **indexed** 100K-memory file with fast KDF. This is the number
|
|
129
|
+
/// Planner-A called out as "~115 ms in my sandbox" that doesn't
|
|
130
|
+
/// regress v0.7.0 → v0.8.0 but needs its own README mention.
|
|
131
|
+
fn bench_open_latency_indexed_100k(c: &mut Criterion) {
|
|
132
|
+
let cached = setup_cached_corpus(100_000, DIMENSIONS);
|
|
133
|
+
let mut group = c.benchmark_group("open");
|
|
134
|
+
group.sample_size(20);
|
|
135
|
+
group.bench_function("open_latency_indexed_100k", |b| {
|
|
136
|
+
b.iter(|| {
|
|
137
|
+
let db = Mnemo::open(&cached, PASSPHRASE).expect("open");
|
|
138
|
+
black_box(db);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
group.finish();
|
|
142
|
+
}
|
|
143
|
+
|
|
106
144
|
criterion_group!(
|
|
107
145
|
open_benches,
|
|
108
146
|
bench_open_latency_fast_kdf,
|
|
109
147
|
bench_open_latency_secure_kdf,
|
|
148
|
+
bench_open_latency_indexed_10k,
|
|
149
|
+
bench_open_latency_indexed_100k,
|
|
110
150
|
);
|
|
111
151
|
criterion_main!(open_benches);
|
|
@@ -112,48 +112,10 @@ fn bench_recall_1m(c: &mut Criterion) {
|
|
|
112
112
|
bench_recall_pair(c, 1_000_000, "1m", 10);
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
/// Phase 12.1 lane-A REVIEW remeasure: first recall on a *fresh*
|
|
116
|
-
/// `Mnemo` handle. Opens the cached 100K corpus, issues a single
|
|
117
|
-
/// recall, closes the handle — repeated per Criterion iteration. The
|
|
118
|
-
/// pre-v0.8.0 code called `ensure_key_caches()` inside recall itself,
|
|
119
|
-
/// which decrypted every live record on the first call — roughly 1 s
|
|
120
|
-
/// at 100K with hardware AES. Post-fix, the first recall only decrypts
|
|
121
|
-
/// the ANN shortlist plus whatever the widen / exact-scan loop needs
|
|
122
|
-
/// (typically a few hundred records), so this cell should sit near
|
|
123
|
-
/// the pure per-query cost of `ivfpq_recall_100k`.
|
|
124
|
-
fn bench_first_recall_fresh_handle_100k(c: &mut Criterion) {
|
|
125
|
-
use mnemo_engine::{RecallRequest, ScoreWeights};
|
|
126
|
-
let cached = setup_cached_corpus(100_000, DIMENSIONS);
|
|
127
|
-
let queries = gen_clustered_vectors(DIMENSIONS, 20, 0xDEAD_BEEF, 100);
|
|
128
|
-
let mut group = c.benchmark_group("ivfpq_first_recall_fresh_handle_100k");
|
|
129
|
-
group.sample_size(20);
|
|
130
|
-
group.bench_function("first_recall", |b| {
|
|
131
|
-
let mut i = 0usize;
|
|
132
|
-
b.iter(|| {
|
|
133
|
-
// Open a *fresh* handle every iteration so the key-cache
|
|
134
|
-
// lazy init (removed in v0.8.0) and page-cache are cold.
|
|
135
|
-
// Use `open_corpus_with_index` so the IVF+PQ path is live;
|
|
136
|
-
// the "fresh handle" invariant we want to measure is the
|
|
137
|
-
// one that pre-v0.8.0 would have paid for key-cache init
|
|
138
|
-
// on every first recall.
|
|
139
|
-
let mut db = open_corpus_with_index(&cached);
|
|
140
|
-
let q = &queries[i % queries.len()];
|
|
141
|
-
i += 1;
|
|
142
|
-
let req = RecallRequest::new(q.clone())
|
|
143
|
-
.top_k(10)
|
|
144
|
-
.track_access(false)
|
|
145
|
-
.weights(ScoreWeights::default());
|
|
146
|
-
let _ = black_box(db.recall(&req).unwrap());
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
group.finish();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
115
|
criterion_group!(
|
|
153
116
|
recall_benches,
|
|
154
117
|
bench_recall_10k,
|
|
155
118
|
bench_recall_100k,
|
|
156
119
|
bench_recall_1m,
|
|
157
|
-
bench_first_recall_fresh_handle_100k,
|
|
158
120
|
);
|
|
159
121
|
criterion_main!(recall_benches);
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
//! Fresh-handle loop harness (PR MEM-1; `fresh_handle_loop_n50`).
|
|
2
|
+
//!
|
|
3
|
+
//! Opens a cached corpus, measures the first recall on each fresh
|
|
4
|
+
//! handle, drops the handle, and prints the process's RSS after the
|
|
5
|
+
//! drop. Run it read-write, then read-only:
|
|
6
|
+
//!
|
|
7
|
+
//! ```text
|
|
8
|
+
//! cargo run --release --example open_rss --features benches -- --corpus 100000
|
|
9
|
+
//! cargo run --release --example open_rss --features benches -- --corpus 100000 --read-only
|
|
10
|
+
//! ```
|
|
11
|
+
//!
|
|
12
|
+
//! MEM-1 closed in v0.9.0: the 13–43 GB "growth" was this harness
|
|
13
|
+
//! (and the deleted `first_recall` bench) passing `0xDEAD_BEEF` as
|
|
14
|
+
//! `k_clusters` to `gen_clustered_vectors`, not an engine or allocator
|
|
15
|
+
//! leak. `corpus.rs` now asserts on implausible `k_clusters`.
|
|
16
|
+
//!
|
|
17
|
+
//! The macOS allocator switches from the diagnostic are kept as
|
|
18
|
+
//! optional knobs: `MallocSpaceEfficient=1 MallocAggressiveMadvise=1`
|
|
19
|
+
//! in the environment, `--pressure-relief` (calls
|
|
20
|
+
//! `malloc_zone_pressure_relief` after each drop), and
|
|
21
|
+
//! `--features benches,mimalloc_bench` (mimalloc as the global
|
|
22
|
+
//! allocator, in this example only).
|
|
23
|
+
|
|
24
|
+
#[cfg(feature = "mimalloc_bench")]
|
|
25
|
+
use mimalloc::MiMalloc;
|
|
26
|
+
#[cfg(feature = "mimalloc_bench")]
|
|
27
|
+
#[global_allocator]
|
|
28
|
+
static GLOBAL: MiMalloc = MiMalloc;
|
|
29
|
+
|
|
30
|
+
use std::hint::black_box;
|
|
31
|
+
use std::process::Command;
|
|
32
|
+
use std::time::Instant;
|
|
33
|
+
|
|
34
|
+
use mnemo_engine::{Mnemo, RecallRequest, ScoreWeights};
|
|
35
|
+
|
|
36
|
+
#[path = "../benches/corpus.rs"]
|
|
37
|
+
mod corpus;
|
|
38
|
+
use corpus::{gen_clustered_vectors, setup_cached_corpus, DIMENSIONS, PASSPHRASE};
|
|
39
|
+
|
|
40
|
+
const OPEN_LOOPS: usize = 50;
|
|
41
|
+
|
|
42
|
+
/// Macro picks the active-allocator banner so the CLI header leaves
|
|
43
|
+
/// no ambiguity about which variant produced the numbers.
|
|
44
|
+
const ALLOCATOR_BANNER: &str = if cfg!(feature = "mimalloc_bench") {
|
|
45
|
+
"mimalloc (variant 4)"
|
|
46
|
+
} else {
|
|
47
|
+
"system default (variant 1/2/3 depending on env + --pressure-relief)"
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/// macOS `libSystem` purge hook. The planner-A DISPATCH wants variant
|
|
51
|
+
/// 3 to call this after every handle drop. Declared only on
|
|
52
|
+
/// `target_os = "macos"`; other platforms fall back to a no-op so the
|
|
53
|
+
/// example still builds.
|
|
54
|
+
#[cfg(target_os = "macos")]
|
|
55
|
+
mod purge {
|
|
56
|
+
#[link(name = "System", kind = "dylib")]
|
|
57
|
+
extern "C" {
|
|
58
|
+
fn malloc_zone_pressure_relief(zone: *mut std::ffi::c_void, goal: usize) -> usize;
|
|
59
|
+
}
|
|
60
|
+
pub fn pressure_relief() -> usize {
|
|
61
|
+
// SAFETY: libSystem's `malloc_zone_pressure_relief` accepts a
|
|
62
|
+
// null zone (meaning "all zones") and a goal of 0 (meaning
|
|
63
|
+
// "as much as you can give back"). Returns the number of
|
|
64
|
+
// bytes released.
|
|
65
|
+
unsafe { malloc_zone_pressure_relief(std::ptr::null_mut(), 0) }
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
#[cfg(not(target_os = "macos"))]
|
|
69
|
+
mod purge {
|
|
70
|
+
pub fn pressure_relief() -> usize {
|
|
71
|
+
0
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// Snapshot the current process's resident set size (RSS) in kB via
|
|
76
|
+
/// `ps -o rss= -p <pid>`. Portable enough for macOS + Linux.
|
|
77
|
+
fn rss_kb() -> u64 {
|
|
78
|
+
let pid = std::process::id();
|
|
79
|
+
let out = Command::new("ps")
|
|
80
|
+
.args(["-o", "rss=", "-p", &pid.to_string()])
|
|
81
|
+
.output();
|
|
82
|
+
out.ok()
|
|
83
|
+
.and_then(|o| {
|
|
84
|
+
String::from_utf8(o.stdout)
|
|
85
|
+
.ok()
|
|
86
|
+
.and_then(|s| s.trim().parse::<u64>().ok())
|
|
87
|
+
})
|
|
88
|
+
.unwrap_or(0)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn one_iteration(
|
|
92
|
+
path: &std::path::Path,
|
|
93
|
+
q: &[f32],
|
|
94
|
+
read_only: bool,
|
|
95
|
+
pressure_relief: bool,
|
|
96
|
+
) -> (f64, u64, u64) {
|
|
97
|
+
let mut db = if read_only {
|
|
98
|
+
Mnemo::open_read_only(path, PASSPHRASE).expect("open_read_only")
|
|
99
|
+
} else {
|
|
100
|
+
Mnemo::open(path, PASSPHRASE).expect("open")
|
|
101
|
+
};
|
|
102
|
+
let req = RecallRequest::new(q.to_vec())
|
|
103
|
+
.top_k(10)
|
|
104
|
+
.track_access(false)
|
|
105
|
+
.weights(ScoreWeights::default());
|
|
106
|
+
let start = Instant::now();
|
|
107
|
+
let hits = db.recall(&req).expect("recall");
|
|
108
|
+
let elapsed = start.elapsed();
|
|
109
|
+
black_box(hits);
|
|
110
|
+
drop(db);
|
|
111
|
+
// Variant 3: ask libSystem to return free pages to the OS before
|
|
112
|
+
// we sample RSS.
|
|
113
|
+
let released = if pressure_relief {
|
|
114
|
+
purge::pressure_relief() as u64
|
|
115
|
+
} else {
|
|
116
|
+
0
|
|
117
|
+
};
|
|
118
|
+
let rss = rss_kb();
|
|
119
|
+
(elapsed.as_secs_f64() * 1_000.0, rss, released)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fn percentile(sorted: &[f64], pct: f64) -> f64 {
|
|
123
|
+
if sorted.is_empty() {
|
|
124
|
+
return 0.0;
|
|
125
|
+
}
|
|
126
|
+
let idx = ((pct / 100.0) * (sorted.len() as f64 - 1.0)).round() as usize;
|
|
127
|
+
sorted[idx.min(sorted.len() - 1)]
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
fn median(sorted: &[f64]) -> f64 {
|
|
131
|
+
percentile(sorted, 50.0)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/// Parse the CLI args (hand-rolled to avoid pulling in clap for a
|
|
135
|
+
/// one-shot measurement example). Accepts `--corpus N`, `--loops N`,
|
|
136
|
+
/// `--pressure-relief`, `--read-only`.
|
|
137
|
+
struct Args {
|
|
138
|
+
corpus: usize,
|
|
139
|
+
loops: usize,
|
|
140
|
+
pressure_relief: bool,
|
|
141
|
+
read_only_phase: bool,
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
fn parse_args() -> Args {
|
|
145
|
+
let mut args = Args {
|
|
146
|
+
corpus: 100_000,
|
|
147
|
+
loops: OPEN_LOOPS,
|
|
148
|
+
pressure_relief: false,
|
|
149
|
+
read_only_phase: false,
|
|
150
|
+
};
|
|
151
|
+
let mut it = std::env::args().skip(1).peekable();
|
|
152
|
+
while let Some(a) = it.next() {
|
|
153
|
+
match a.as_str() {
|
|
154
|
+
"--corpus" => {
|
|
155
|
+
args.corpus = it
|
|
156
|
+
.next()
|
|
157
|
+
.and_then(|v| v.parse().ok())
|
|
158
|
+
.expect("--corpus needs a positive integer");
|
|
159
|
+
}
|
|
160
|
+
"--loops" => {
|
|
161
|
+
args.loops = it
|
|
162
|
+
.next()
|
|
163
|
+
.and_then(|v| v.parse().ok())
|
|
164
|
+
.expect("--loops needs a positive integer");
|
|
165
|
+
}
|
|
166
|
+
"--pressure-relief" => args.pressure_relief = true,
|
|
167
|
+
"--read-only" => args.read_only_phase = true,
|
|
168
|
+
other => panic!("unknown arg: {other}"),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
args
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
fn main() {
|
|
175
|
+
let args = parse_args();
|
|
176
|
+
println!("mnemo PR MEM-1 open_rss harness");
|
|
177
|
+
println!(" pid : {}", std::process::id());
|
|
178
|
+
println!(
|
|
179
|
+
" corpus size : {} memories, {}-dim vectors",
|
|
180
|
+
args.corpus, DIMENSIONS
|
|
181
|
+
);
|
|
182
|
+
println!(" loops : {}", args.loops);
|
|
183
|
+
println!(
|
|
184
|
+
" phase : {}",
|
|
185
|
+
if args.read_only_phase {
|
|
186
|
+
"read-only"
|
|
187
|
+
} else {
|
|
188
|
+
"read-write"
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
println!(
|
|
192
|
+
" pressure_relief : {}",
|
|
193
|
+
if args.pressure_relief {
|
|
194
|
+
"on (variant 3)"
|
|
195
|
+
} else {
|
|
196
|
+
"off"
|
|
197
|
+
}
|
|
198
|
+
);
|
|
199
|
+
println!(" allocator : {ALLOCATOR_BANNER}");
|
|
200
|
+
println!(
|
|
201
|
+
" macOS env : MallocSpaceEfficient={} MallocAggressiveMadvise={}",
|
|
202
|
+
std::env::var("MallocSpaceEfficient").unwrap_or_else(|_| "unset".into()),
|
|
203
|
+
std::env::var("MallocAggressiveMadvise").unwrap_or_else(|_| "unset".into()),
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
let cached = setup_cached_corpus(args.corpus, DIMENSIONS);
|
|
207
|
+
let queries = gen_clustered_vectors(args.loops, DIMENSIONS, 100, 0xDEAD_BEEF);
|
|
208
|
+
println!("rss_kb at start: {}\n", rss_kb());
|
|
209
|
+
|
|
210
|
+
println!(
|
|
211
|
+
"{:>4} {:>14} {:>14} {:>14}",
|
|
212
|
+
"iter", "first_recall_ms", "rss_kb_after", "released_kb"
|
|
213
|
+
);
|
|
214
|
+
let mut ms_log = Vec::with_capacity(args.loops);
|
|
215
|
+
let mut rss_log = Vec::with_capacity(args.loops);
|
|
216
|
+
for i in 0..args.loops {
|
|
217
|
+
let q = &queries[i % queries.len()];
|
|
218
|
+
let (ms, rss, released) =
|
|
219
|
+
one_iteration(&cached, q, args.read_only_phase, args.pressure_relief);
|
|
220
|
+
println!("{i:>4} {ms:>14.3} {rss:>14} {:>14}", released / 1024);
|
|
221
|
+
ms_log.push(ms);
|
|
222
|
+
rss_log.push(rss);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
let mut sorted = ms_log.clone();
|
|
226
|
+
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
|
|
227
|
+
println!("\n== summary ==");
|
|
228
|
+
println!(
|
|
229
|
+
"first-recall ms: median {:.3}, p95 {:.3}",
|
|
230
|
+
median(&sorted),
|
|
231
|
+
percentile(&sorted, 95.0),
|
|
232
|
+
);
|
|
233
|
+
let start = *rss_log.first().unwrap_or(&0);
|
|
234
|
+
let end = *rss_log.last().unwrap_or(&0);
|
|
235
|
+
let peak = *rss_log.iter().max().unwrap_or(&0);
|
|
236
|
+
println!(
|
|
237
|
+
"rss_kb: start {start}, end {end}, peak {peak}, delta {}",
|
|
238
|
+
end as i64 - start as i64
|
|
239
|
+
);
|
|
240
|
+
// Slope over the second half: positive = climbing, near-zero =
|
|
241
|
+
// plateau.
|
|
242
|
+
let slope = {
|
|
243
|
+
if rss_log.len() < 4 {
|
|
244
|
+
0.0
|
|
245
|
+
} else {
|
|
246
|
+
let half = rss_log.len() / 2;
|
|
247
|
+
let last_half = &rss_log[half..];
|
|
248
|
+
let first = *last_half.first().unwrap_or(&0) as f64;
|
|
249
|
+
let last = *last_half.last().unwrap_or(&0) as f64;
|
|
250
|
+
(last - first) / (last_half.len() as f64 - 1.0)
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
println!("rss_kb slope (second half): {:.2} kB/iter", slope);
|
|
254
|
+
}
|