Expand description
Compact maps for the per-unit data: SmallMap (a sorted boxed slice) and TextMap (text to text).
A BTreeMap allocates a whole node (room for 11 entries) on its first insert, about 1.3 KB for a unit’s
inline codes even when it holds two of them. A unit holds a few entries, so a sorted boxed slice with
binary-search lookup holds the same data in a third of the memory and is just as fast to read.
Both behave like BTreeMap<String, _> wherever a caller can tell: keys iterate in str order (which is
String order), a second insert of a key replaces the value, JSON is an object with its keys in that order,
and reading a JSON object with a repeated key keeps the last value. Two things differ: iteration yields
(&str, &V), and the methods take &str keys.
Two representations, one behaviour. Up to SMALL_MAX entries the map is a sorted boxed slice, exact
size, so an insert or a remove reallocates it and shifts at most SMALL_MAX entries. Above that it moves its
entries into a boxed BTreeMap (one extra allocation for a map that is large anyway), where an insert costs
O(log n): a unit with 200,000 inline codes inserted one at a time in any order is built in linear-log time,
not quadratic. It moves back to the slice when a remove or retain leaves SMALL_MIN entries or fewer (the
gap stops a map that hovers around the limit from switching on every call). Iteration order, JSON bytes,
PartialEq, Eq, Hash, Ord, Debug and the Index panic are the same in both representations; nothing
but SmallMap::is_compact tells them apart. Inline size: 16 bytes.
Bulk building (collect, extend on a slice, From<BTreeMap>, From<[(K, V); N]>, reading JSON) sorts once
and picks the representation for the final size.
Structs§
- Into
Iter - Owning iterator over
(Box<str>, V), in key order. - Iter
- Iterator over
(&str, &V), in key order. - IterMut
- Iterator over
(&str, &mut V), in key order. - Keys
- Iterator over the keys, in order.
- Small
Map - A map from text keys to
V. See the module docs. - TextMap
- A map from text to text (a unit’s hidden values, a context’s extras), stored as a
SmallMapofBox<str>. It reads and writes&str:TextMap::getgivesOption<&str>and iteration gives(&str, &str), so callers never see how the text is stored. JSON is an object of strings, as aBTreeMap<String, String>. - Values
- Iterator over the values, in key order.
- Values
Mut - Iterator over the values, mutable, in key order.
Constants§
- SMALL_
MAX - Most entries a map keeps in its sorted slice; one more and it moves to a
BTreeMap. - SMALL_
MIN - A map in a
BTreeMapmoves back to a slice when a remove leaves this many entries or fewer.
Type Aliases§
- Text
Into Iter - Owning iterator over a
TextMap’s(Box<str>, Box<str>), in key order. - Text
Iter - Iterator over a
TextMap’s(&str, &str), in key order. - Text
Values - Iterator over a
TextMap’s values as&str, in key order.