The format parser, GZip decoder, integrity checks and tokenization run outside the scoring loop. Runtime cost is determined by the model's in-memory data structure, key representation and the amount of work per call.
| Workload | API/data path | Reason |
|---|---|---|
| Prepared individual n-gram, dense table | Bind scores = model.scores; access scores[key] |
Direct NumPy array indexing, without a per-call API wrapper |
| Existing short candidates, dense n=1..6 | legacy = model.as_legacy_grams() once, then legacy.calculate_cost(ids) |
Exactly the old class and scoring implementation |
| Longer candidates / batches | model.calculate_cost(ids) or model.score_keys(keys) |
Vectorized dense key generation and score gathering |
| Large word/syllable models | Sparse model; bind score dictionary/default before repeated direct access if needed | Avoids storing V**n combinations |
The public lookup_key call is also prepared/unchecked, but an explicit Python
method call may cost more than an inlined scores[key] expression. For sparse
models scores.get(key, default) is an available raw primitive. The checked
lookup_ids, lookup_tokens and encode_key APIs deliberately include
validation and/or string lookup; they should not be compared to a bare array
access as if they did the same work.
model = load_model("model.ctls2")
ids = model.encode_tokens(tokens) # outside the optimizer loop
key = model.encode_key(ids[:model.order])
if model.storage == "dense":
scores = model.scores
value = scores[key]
else:
get = model.scores.get
default = model.default_score
value = get(key, default)The radix encoding is key = key * V + token_id in token order. A moving valid
window may update its key as (key % V**(n-1)) * V + next_id. The library has a
rolling arbitrary-precision path and a vectorized machine-integer path. A
solver modifying only some positions can reuse/update affected prepared windows
rather than re-tokenizing whole candidates. Such incremental solver integration
is an application choice, not an automatic property of loading a new format.
The included report and raw samples compare the same scores and candidates against the original classes, with n=2..5 and lengths 16, 64, 256 and 4096. They separate raw/prepared scalar access, complete candidate scoring and already-prepared key batches. Numeric results and the exact adapter method identity are verified before timing.
In this recorded synthetic run, the vectorized dense path achieved about 0.3x the old throughput for 16-token candidates: fixed preparation overhead made it slower. For 256-token candidates it was 2.38–3.44x faster, and for 4096-token candidates 4.71–8.05x faster. The exact legacy adapter preserves the original scoring path for short candidates. These observations are guidance, not a hard-coded automatic threshold or a claim about every CPU.
Dense prepared raw accesses measured about 62–67 ns and the old multidimensional tensor accesses about 71–105 ns in this run. Public prepared lookup calls include Python-call overhead; see the complete table instead of assuming every API has the latency of a raw index. Sparse models additionally depend on hash-table load, locality and the number of integer key digits.
Dense float32 scores require 4 * V**n bytes, excluding counts and temporary
arrays. The generator's automatic mode chooses dense only under its explicit
budget (64 MiB by default); sparse models store retained observed combinations.
Raw count tables and training dictionaries also need memory. GZip reduces file
size but does not reduce the live dense array.
Sparse table lookup has expected constant-time table access for fixed-size keys. Arbitrary-precision integer hashing, encoding and equality depend on key length. A 50,000-word vocabulary or a 100-syllable 12-gram model therefore cannot be promised the same latency as a small character bigram under all workloads. The benchmark includes such cases without inventing a feasible legacy dense baseline.
Unknown ID positions contribute zero while retaining the original positional
denominator in calculate_cost, matching the old API. This is a compatibility
rule, not a missing-token probability estimate. Callers should normally prepare
valid IDs or deliberately choose their unknown-token policy.
Keep configuration fixed during a prepared hot loop. If changing the sparse
default_score, refresh any cached lookup_key callable, because it is a
prepared snapshot. Mutating dense array entries updates the shared legacy
adapter as well. Changing vocabulary/order requires constructing a new model.