Optimize for present keys with PairedVerifiedConstMap
mainIf most of your lookups are for keys that are present in the map, use PairedVerifiedConstMap via constmap.NewPaired(keys, values).
How it works:
Unlike VerifiedConstMap which stores fingerprints and values in separate arrays (requiring up to six cache line reads), PairedVerifiedConstMap stores each value next to its fingerprint. This reduces the lookup to three cache line reads.
When to use:
- Use
PairedVerifiedConstMapwhen most lookups are for present keys (faster due to fewer cache lines). - Use
VerifiedConstMapwhen most lookups are for absent keys (faster because the fingerprint array is smaller and more likely to be cache-resident).
Note: The serialized formats are different (Paired uses magic bytes PMAP0001) and are not interchangeable. You can convert an existing VerifiedConstMap using vm.Paired().
pm, err := constmap.NewPaired(keys, values)
if err != nil {
log.Fatal(err)
}
fmt.Println(pm.Map("banana")) // 200
fmt.Println(pm.Map("grape")) // constmap.NotFound