The bytemap package provides an implementation of a map that uses []byte keys, called ByteSliceMap. The map is implemented as a Trie data structure. It is based off of triemap, but adds support for generics.
To use the ByteSliceMap, first import the package:
import "github.com/synapsecns/sanguine/core/bytemap"
Then, create a new ByteSliceMap with the desired value type:
m := &bytemap.ByteSliceMap[string]{}
Values can be inserted into the map using either []byte
or string
keys. To insert a value with a string
key, use the PutString
method:
m.PutString("key", "value")
To insert a value with a []byte key, use the Put method:
m.Put([]byte{0x01, 0x02}, "value")
Values can be retrieved from the map using either []byte
or string
keys. To retrieve a value with a string key, use the GetString method:
value, exists := m.GetString("key")
To retrieve a value with a []byte
key, use the Get
method:
value, exists := m.Get([]byte{0x01, 0x02})
The exists
return value indicates whether the key exists in the map.
The ByteSliceMap seems to perform worse than a regular map[string]V
, even when casting []byte
to string. Therefore, it may not be suitable for performance-critical applications.