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
//! Size classes.
//!
//! # Why eight subdivisions per octave, not four
//!
//! Two terms of the accounting contract pull against each other: finer
//! classes cut **rounding**, coarser classes cut **span slack** (fewer
//! classes means fewer partial spans sitting around). Both are real, so
//! the choice is not taste.
//!
//! §8.1 of the RFC settles it. Rounding is the only term that scales
//! with the dataset; slack is O(classes × shards) and constant in the
//! data. **Spend the constant to shrink the term that scales.** Hence
//! eight subdivisions per octave — worst-case rounding ≈ 11.1 %, against
//! the ~20 % that four subdivisions would give.
//!
//! Below 128 bytes the classes step by 8, and the bound there is
//! **absolute rather than relative**: at most 7 bytes wasted, but that
//! is 29 % of a 24-byte class. The relative bound cannot be rescued at
//! that end — 8 bytes is the granularity floor, since finer classes
//! would not keep slots 8-byte aligned. Saying "the step is finer so it
//! costs nothing" would have been wrong, and the class table's own test
//! said so before this comment was written.
//!
//! # Why spans are one uniform size after all
//!
//! The first draft sized spans per class, reasoning that a fixed span
//! size makes slack proportional to the class count — which the decision
//! above deliberately increases. Two things overturned it.
//!
//! Geometry: `dealloc` finds a pointer's span by masking, which needs
//! uniform span geometry. Variable spans would need a lookup structure
//! on the free path, paid on every deallocation, to save address space.
//!
//! And the worry was misplaced. Spans hand out slots by bumping a
//! cursor, so the untouched tail of a span is **mapped but never
//! resident** — it costs address space, not memory. That is why the
//! accounting splits slack into touched (`span_free`) and untouched
//! (`virgin`): only the first is RSS. A large uniform span whose tail is
//! never reached is close to free in the metric that matters.
//!
//! # Examples
//!
//! ```
//! use kevy_alloc::class::{index_of, size_of};
//! // a 100-byte request is served from the 104-byte class
//! assert_eq!(size_of(index_of(100, 8).unwrap()), 104);
//! ```
/// The largest allocation served by a size class. Above this, requests
/// are mapped directly and returned with `unmap`.
///
/// Raised 8 KiB → 32 KiB by the M1 decomposition. The old cap assumed
/// large allocations were infrequent — and under a cross-shard load the
/// dispatch and reply buffers sit just past 8 KiB, so every one paid an
/// mmap on birth and a munmap on death, eight shards serialised on the
/// process-wide mmap_lock: **40 % of server self time** was
/// `__x64_sys_munmap` + `vm_mmap_pgoff` (finding
/// measured (the mmap-lock convoy finding). glibc recycles those
/// buffers from its arena with zero syscalls, which is the entire
/// cross-shard gap. A 64 KiB span still holds 2–8 slots at these sizes.
///
/// # Examples
///
/// ```
/// use kevy_alloc::class::{MAX_SMALL, index_of};
/// assert!(index_of(MAX_SMALL, 8).is_some());
/// assert_eq!(index_of(MAX_SMALL + 1, 8), None); // mapped directly instead
/// ```
pub const MAX_SMALL: usize = 32_768;
/// Alignment every class satisfies natively, because every class is a
/// multiple of it and spans are aligned far beyond it.
///
/// # Examples
///
/// ```
/// use kevy_alloc::class::{CLASSES, MIN_ALIGN};
/// assert!(CLASSES.iter().all(|&c| c as usize % MIN_ALIGN == 0));
/// ```
pub const MIN_ALIGN: usize = 8;
/// Strongest alignment served by picking a suitable class rather than by
/// over-allocating. Requests above this go to the `GlobalAlloc` shim's
/// over-aligned path.
///
/// 16 matters enough to be worth serving directly — `u128`, `AtomicU64`
/// pairs and most SIMD vectors ask for it — and it costs only skipping
/// to the next class when the natural one is not a multiple of 16.
///
/// # Examples
///
/// ```
/// use kevy_alloc::class::{MAX_NATIVE_ALIGN, index_of};
/// assert!(index_of(64, MAX_NATIVE_ALIGN).is_some());
/// assert_eq!(index_of(64, MAX_NATIVE_ALIGN * 2), None);
/// ```
pub const MAX_NATIVE_ALIGN: usize = 16;
/// Every span is this many bytes, whatever class it serves. Uniform
/// geometry is what lets `dealloc` find a span by masking; see the
/// module docs for why the variable-size draft lost. 64 KiB gives the
/// largest class eight slots and the smallest four thousand.
///
/// # Examples
///
/// ```
/// use kevy_alloc::class::{MAX_SMALL, SPAN_BYTES, index_of, slots_per_span};
/// assert_eq!(slots_per_span(index_of(MAX_SMALL, 8).unwrap()), SPAN_BYTES / MAX_SMALL);
/// ```
pub const SPAN_BYTES: usize = 64 * 1024;
/// The class table: every size a slot may have, ascending.
///
/// Written out rather than generated so it can be read and checked. A
/// stone's most important property is that a reviewer can see what it
/// does; 79 numbers are cheaper to audit than the loop that would emit
/// them.
///
/// # Examples
///
/// ```
/// use kevy_alloc::class::{CLASSES, MAX_SMALL};
/// assert!(CLASSES.windows(2).all(|w| w[0] < w[1]));
/// assert_eq!(*CLASSES.last().unwrap() as usize, MAX_SMALL);
/// ```
pub const CLASSES: = ;
/// Number of size classes.
///
/// # Examples
///
/// ```
/// use kevy_alloc::class::{NCLASSES, MAX_SMALL, index_of};
/// assert_eq!(index_of(MAX_SMALL, 8), Some(NCLASSES - 1));
/// ```
pub const NCLASSES: usize = CLASSES.len;
/// Lookup granularity: one table entry per 8 bytes of request size.
const GRAIN: usize = 8;
const LOOKUP_LEN: usize = MAX_SMALL / GRAIN + 1;
/// `size -> class index`, resolved by table rather than by arithmetic so
/// the hot path is one load and no branching over the octave structure.
static LOOKUP: = build_lookup;
const
/// The class index serving `size` at `align`, or `None` when the request
/// belongs on the direct-mapping path (too large, or too strictly
/// aligned to serve from a class).
///
/// Slots sit at multiples of the class size inside a span, and span
/// bases are 64 KiB-aligned, so a slot's alignment is exactly the
/// alignment of its class size. Serving a 16-byte alignment therefore
/// means choosing a class that is a multiple of 16 — which, in the
/// 8-stepped region below 128, is always the very next one.
/// # Examples
///
/// ```
/// use kevy_alloc::class::{index_of, size_of};
///
/// // Every served size rounds UP to its class, never down.
/// let i = index_of(1, 1).unwrap();
/// assert!(size_of(i) >= 1);
/// let i = index_of(100, 1).unwrap();
/// assert!(size_of(i) >= 100);
///
/// // A strict alignment picks a class that is a multiple of it.
/// let i = index_of(24, 16).unwrap();
/// assert_eq!(size_of(i) % 16, 0);
///
/// // Too large, or too strictly aligned, is None — the direct-mapping
/// // path, not a wrong class.
/// assert_eq!(index_of(1 << 30, 1), None);
/// ```
/// Compile-time proof of the two facts `index_of` hands the optimiser:
/// every lookup entry is a class index, and wherever the 16-aligned path
/// steps up a class, that class exists and is a multiple of 16.
const LOOKUP_IS_SOUND: = ;
const _: = LOOKUP_IS_SOUND;
/// Slot size for a class index.
/// # Examples
///
/// ```
/// use kevy_alloc::class::{index_of, size_of};
/// // Classes ascend, so a bigger request never lands in a smaller slot.
/// let small = size_of(index_of(8, 1).unwrap());
/// let big = size_of(index_of(200, 1).unwrap());
/// assert!(small <= big);
/// ```
/// `ceil(2^32 / size)` per class — the reciprocal that turns the free
/// path's slot-index division into a multiply-shift.
///
/// Exactness (Granlund–Montgomery): with `m = ceil(2^32 / d)`,
/// `(n * m) >> 32 == n / d` for every `n` where
/// `n * (m*d − 2^32) < 2^32`. Here `m*d − 2^32 < d ≤ 2^15` and
/// `n < SPAN_BYTES = 2^16`, so the error product stays below `2^31`.
/// The unit test still checks every class at every span offset —
/// exhaustively, because a proof in a comment has no CI.
const RECIP: = ;
/// Divide a span offset by a class's slot size via the reciprocal
/// table. `off` must be below [`SPAN_BYTES`].
///
/// # Examples
///
/// ```
/// use kevy_alloc::class::{index_of, size_of, slot_of_offset};
/// let i = index_of(100, 8).unwrap();
/// let slot = size_of(i);
/// // an interior pointer into slot 3 maps back to slot 3
/// assert_eq!(slot_of_offset(3 * slot + 5, i), 3);
/// ```
/// Slots that fit in a span of this class.
///
/// # Examples
///
/// ```
/// use kevy_alloc::class::{SPAN_BYTES, index_of, slots_per_span};
/// assert_eq!(slots_per_span(index_of(16, 8).unwrap()), SPAN_BYTES / 16);
/// ```
pub const