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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! File-backed storage for a [`ContiguousVectors`] buffer.
//!
//! # Why this exists
//!
//! A quantized index keeps two representations of every vector: the codes it
//! traverses on, and the f32 it re-ranks with. The codes are small; the f32 is
//! not. While that f32 arena is an anonymous allocation it is **un-evictable**,
//! so the index's resident set is `f32 + graph + codes` — larger than the
//! unquantized index it was supposed to shrink (#2112).
//!
//! Backing the arena with a file makes those pages evictable: the kernel can
//! reclaim them under pressure and fault them back in when a re-rank touches
//! them. The resident floor becomes `codes + graph`, which is the whole point
//! of the quantized modes on a small device.
//!
//! # Why the pages are the same bytes the index already persists
//!
//! `{basename}.vectors` was already a raw little-endian f32 blob behind a
//! short header — the arena's own memory layout, written out one value at a
//! time. Mapping that file instead of deserializing it into a fresh
//! allocation removes a full copy from load and makes growth allocation-free:
//! extending the file leaves the existing pages in place, where the heap path
//! has to `memcpy` the whole arena into a bigger block.
//!
//! # Byte order
//!
//! The mapped bytes are native-endian f32: the arena *is* the file, so no
//! conversion happens on either side. That makes an arena file
//! self-consistent on any target but **not portable between targets of
//! different endianness** — it is an index-local cache, not an interchange
//! format, and must be rebuilt rather than copied across such a boundary.
//!
//! Worth stating because the neighbouring `{basename}.vectors` format does
//! the opposite: it converts explicitly (`to_le_bytes`/`from_le_bytes`), so it
//! *is* portable. Anything that later maps that file directly inherits this
//! constraint and must gate on `target_endian`.
//!
//! # Availability
//!
//! `persistence`-only. Without it (WASM, `--no-default-features`) there is no
//! filesystem to map and [`ContiguousVectors`] keeps its anonymous backing;
//! this module is not compiled at all.
//!
//! [`ContiguousVectors`]: crate::perf_optimizations::ContiguousVectors
use ;
use ;
use io;
use ;
use NonNull;
/// Byte offset of the f32 region inside a file-backed arena.
///
/// One page, so the mapped data region starts page-aligned — which also
/// satisfies the arena's 64-byte cache-line preference. The header itself
/// occupies the first bytes of that page; the rest is reserved padding.
///
/// A whole page is deliberate overkill, and the reason is soundness rather
/// than speed. [`ContiguousVectors`] hands out `&[f32]` built with
/// `slice::from_raw_parts`, whose contract requires the pointer to be
/// *properly aligned* — a misaligned arena would be undefined behaviour at the
/// first `get`, long before any SIMD ran. (The SIMD kernels themselves all
/// load unaligned, so they would not have been the ones to complain.) Starting
/// the data region on a page boundary keeps that requirement satisfied by
/// construction instead of by arithmetic that a later edit could break.
///
/// [`ContiguousVectors`]: crate::perf_optimizations::ContiguousVectors
pub const DATA_OFFSET: usize = 4096;
/// A file mapping that owns the bytes behind a
/// [`ContiguousVectors`](crate::perf_optimizations::ContiguousVectors) buffer.
///
/// # Invariants
///
/// - `map` is a mapping of at least `DATA_OFFSET + byte_len` bytes of `file`.
/// - The address the mapping lives at is owned by the kernel, not stored
/// inline in this struct, so moving a `FileArena` does **not** move the
/// bytes. That is what makes it sound for `ContiguousVectors` to hold a
/// `NonNull` into the mapping while also owning the `FileArena`.
/// - **This process holds an exclusive advisory lock on `file`, and no second
/// `FileArena` can map the same path.** This one is not a nicety: an
/// anonymous allocation is unique because the allocator says so, but a path
/// is not, and two arenas over one file would be two `&mut [f32]` aliases of
/// the same bytes — undefined behaviour that `ContiguousVectors`'
/// `unsafe impl Send`/`Sync` would then carry across threads. The lock is
/// what makes the uniqueness those impls assume actually true, so it is
/// taken before the mapping and released only when `file` closes.
/// - `path` is kept for diagnostics and for reopening after a grow.
pub
// SAFETY: `FileArena` is `Send` because everything it owns is, and the raw
// pointer it caches addresses memory the kernel owns rather than this thread.
// - Condition 1: `MmapMut`, `File` and `PathBuf` are all `Send`.
// - Condition 2: `data` points into `map`, whose address is assigned by the
// kernel and unaffected by moving this struct, so the pointer stays valid
// wherever the arena goes.
// - Condition 3: the exclusive lock taken in `from_file` means no other
// `FileArena` addresses the same bytes, so moving this one cannot create an
// alias on the receiving thread.
// SAFETY: Moving an arena between threads leaves its mapping intact and unique.
unsafe
// SAFETY: `FileArena` is `Sync` because shared references expose no mutation.
// - Condition 1: `&self` reaches only `data_ptr`, `flush`, `evict` and
// `path`. None writes through the mapping: `evict` discards resident pages
// and the next read re-faults the same bytes from the page cache, which is
// not a mutation any reader can observe. Every mutating entry point
// (`grow`) takes `&mut self`.
// - Condition 2: handing out the cached pointer is not itself a write — the
// caller's own aliasing rules govern what it does with it, exactly as for
// the heap backing.
// SAFETY: Concurrent shared references to an arena cannot mutate it.
unsafe
/// What an opening arena does with the bytes the file already holds.
///
/// A named choice rather than a `bool` parameter, because the two callers read
/// as opposites at the call site (`create` discards, `open` keeps) and the
/// distinction decides whether the arena starts zeroed or starts from disk.
///
/// `pub(crate)` so `ContiguousVectors`' two file-backed constructors speak the
/// same vocabulary instead of re-deriving it from a bool of their own.
pub