Skip to content

Repository files navigation

hypermap-go

hypermap-go provides hypermap.Map: a compact, generic, insertion-ordered map for Go.

Go ReferenceGitHub License

Tip

Use hypermap.New[K, V](capacity) when the maximum live entry count is known; it avoids growth during the initial fill. Different-key churn may still grow the hash index to preserve amortized write cost.

Install

go get -u github.com/colduction/hypermap-go@latest

Usage

package main
import (
"fmt""github.com/colduction/hypermap-go"
)
funcmain() {
m:= hypermap.New[string, int](3)
m.Set("b", 2)
m.Set("a", 1)
m.Set("c", 3)
m.MoveToFront("c")
forkey, value:=rangem.Range() {
fmt.Println(key, value)
}
}

URL query encoding

hypermap.QueryMap is a Map[string, []string] with an Encode method that renders the entries as a URL query string in the current key order. It embeds Map, so every map method (Set, Get, MoveToFront, Range, …) is available on it as well.

m:=hypermap.NewQueryMap(3)
m.Set("name", []string{"ada lovelace"})
m.Set("tags", []string{"math", "code"})
fmt.Println(m.Encode()) // name=ada+lovelace&tags=math&tags=code

Tip

Encode allocates once when it emits output. A nil/empty map, or one whose value slices are all empty, returns "" without that output allocation.

Features

CapabilityBehavior
Zero valueReady to use without initialization.
OrderingPreserves insertion order; replacing a value keeps the key in place.
Lookup and mutationO(1) average for lookup, insert, delete, movement, and front/back access.
ReplacementReplace updates only an existing key; Set inserts or replaces.
IterationRange provides iter.Seq2; RangeFunc is the lower-overhead callback form.
Storage reuseClear keeps allocated storage, while Reset releases it.
Query encodingQueryMap.Encode renders string/[]string entries as a query string.

Important

Do not copy a Map after initialization or mutation. It does not synchronize access; share one across goroutines only with external synchronization, or shard independent maps by key or worker for write-heavy services.

Benchmarks

Median time with 4,096 int/int entries; lower is better. These are the maintained headline workloads, and the fastest result in each row is bold.

Operationhypermapwk8elliotchancelorenzosainovs best rival
Get4.799 ns5.413 ns5.457 ns5.480 ns11.3% faster
Replace5.897 ns10.46 ns12.10 ns7.751 ns23.9% faster
Delete + set24.14 ns91.35 ns63.05 ns75.04 ns61.7% faster
Move front/back16.03 ns27.07 ns27.34 ns40.8% faster
Range all4.030 µs10.13 µs4.653 µs4.863 µs13.4% faster
Fill new map53.51 µs175.4 µs119.9 µs267.4 µs55.4% faster

Every operation on an already-populated Hypermap in the table is 0 B/op and 0 allocs/op. Filling a capacity-sized map uses 147,456 B and 2 allocations, versus 278,896–492,776 B and 4,114–8,213 allocations for the alternatives. The broader suite also measures misses, strings, tiny maps, fragmented traversal, early stop, unhinted construction, and different-key churn.

Methodology and reproduction

Results are medians from 10 one-second samples using Go 1.26.5 on Windows 11 amd64 and an AMD Ryzen 9 7950X, pinned to logical CPU 2 with GOMAXPROCS=1. Capacity hints are used where supported. Range all and Fill new map process all 4,096 entries. Replacement and traversal use each package's fastest non-allocating API: Hypermap uses Replace and RangeFunc. Timings can vary with map seed and system clock state; the table reports ten-sample medians, not universal dominance.

Compared versions:

The source, validation checks, and pinned dependency versions are in benchmarks. The maintained Windows headline command is:

cd benchmarks
$benchProcess=Get-Process-Id $PID$benchProcess.ProcessorAffinity= [IntPtr]4$benchProcess.PriorityClass='High'$headline='^(BenchmarkGet|BenchmarkSetReplace|BenchmarkDeleteSet|BenchmarkMoveToFrontBack|BenchmarkRange|BenchmarkFill)$'
go test -run '^$'-bench $headline-benchmem -benchtime=100ms -count=1-cpu=1|Out-Null
go test -run '^$'-bench $headline-benchmem -benchtime=1s -count=10-cpu=1

Run the broader workload matrix with -bench '^BenchmarkWorkload'. Built-in map fast paths can still win for some 8- or 64-entry operations, so the table is not a claim of universal superiority across all key types, sizes, or machines. See the performance design and tradeoffs for the data layout, research basis, allocation caveats, and complete protocol.

License

This project is released under the MIT License. See LICENSE.

About

Package hypermap provides compact, generic insertion-ordered maps for allocation-conscious Go services.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages