Skip to main content

size2class_len

Function size2class_len 

Source
pub const fn size2class_len(max_class: usize, min_block: usize) -> usize
Expand description

The size2class array length for a scheme whose largest class is max_class: one u8 per min_block-sized bucket from 0 up to and including max_class. A consumer uses this in a const expression to pin the L generic of SizeClasses.

§Memory cost

L (max_class / min_block + 1) is the byte size of the size2class LUT SizeClasses embeds, and it is NOT something a consumer picks directly – it falls out of min_block, growth, geo_count, and extras together. It scales with max_class / min_block, not with the number of classes N, so a scheme with FEWER classes can still produce a LARGER LUT than one with more: a realistic scheme (min_block = 16, growth = (5, 4), geo_count = 40, nine extras up to 16 KiB; the crate itself has no defaults) with 49 classes and max_class = 258752 gives L = 16173. table itself is only N * size_of::<usize>() = 392 bytes on a 64-bit target; the LUT dominates – table + size2class together are ~16.18 KiB, and size_of::<SizeClasses<49, 16173>>() itself is ~16.20 KiB on a 64-bit target, the difference being the struct’s two scalar fields plus alignment padding. But a smaller min_block can outweigh a smaller class count entirely: min_block = 8 with just 24 classes (growth = (3, 2), no extras) reaches max_class = 145648 and L = 18207, a LARGER object than the 49-class example above. Concretely, for that same 49-class example the sparsity this scaling implies is large: buckets 888..=16172 — 15285 of the 16173 total, 94.5% — all resolve to just the 14 largest classes (indices 35..=48), because class sizes grow geometrically while the LUT’s own resolution stays a flat min_block.

§Panics

Panics – identically in const evaluation and at runtime, since this is a pub const fn callable either way – if min_block is not a power of two, or if max_class / min_block + 1 overflows usize (reachable only for min_block == 1 and max_class == usize::MAX; for any min_block >= 2 the quotient cannot reach usize::MAX).

The + 1 overflow check is explicit rather than relying on the profile’s default: a release-profile const evaluation reached through a const fn call follows the crate’s overflow-checks setting and can silently wrap to 0 otherwise (https://github.com/rust-lang/rust/issues/74823).