← All lints

domain_keyed_map

L0412Performancewarn

Map or Set keyed by a domain

a Map or Set keyed by a domain

Flagged
domain V = 1..=5
var m: Map<V, Int> = Map()
m[V(1)] = 42
print(m[V(1)] ?? 0)
Write instead
domain V = 1..=5
var m: [V]?Int = None
m[V(1)] = 42
print(m[V(1)] ?? 0)

Why

A Map<V, T> or Set<V> keyed by a dense domain V pays hashing or tree overhead on every lookup. Use a flat [V]?T or [V]Bool array instead.

The practice it serves

Code that does more work than it needs to. Family: S.sorted: sets, bags, maps, intervals.

Not flagged

A near miss: close to the pattern, but not it.

Near miss
domain V = 1..=5
var m: Map<Int, Int> = Map()
m[1_000_000] = 42
print(m.get(1_000_000) ?? 0)

Turn it off

For one block, wrap it in with lint(domain_keyed_map: allow):. For a project, add domain_keyed_map = "allow" under [lints] in cp.toml.

Generated from spec/lints/domain_keyed_map, the same record the linter is built from.