https://github.com/dzdx/imstrmap
an immutable string map that optimize memory usage
https://github.com/dzdx/imstrmap
golang immutable memory-optimization stringmap
Last synced: 4 months ago
JSON representation
an immutable string map that optimize memory usage
- Host: GitHub
- URL: https://github.com/dzdx/imstrmap
- Owner: dzdx
- Created: 2019-11-04T10:12:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-11-04T14:04:01.000Z (over 5 years ago)
- Last Synced: 2024-12-27T06:41:46.859Z (6 months ago)
- Topics: golang, immutable, memory-optimization, stringmap
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ImmutableStringMap
Compared with the string map, ImmutableStringMap memory usage is small, you can specify the keys for index accelerate Get method.
More suitable for the map that resident memory, and will not change after initialization
## example
```
var (
indexerFactory = imstrmap.NewIndexerFactory([]string{"indexkey"})
)func run(){
m := imstrmap.FromMap(map[string]string{...}, indexerFactory)
value, ok := m.Get("a")
m.Range(func(key string, value string){
fmt.Println(key, value)
}
for k, v := range m.Map(){
fmt.Println(k, v)}
}
```## benchmark
goos: darwin
goarch: amd64
### testdata:
```
{
"locality": "vlocality",
"a": "a",
"b": "b",
"c": "c",
"das": "das",
"huhqw": "huhqw",
"uyoqw": "uyoqw",
"y9qw": "y9qw",
"juioq": "juioq",
"qqeq": "qqeq",
"vqrqasas": "vqrqasas",
"hqw": "hqw",
"asdqw": "asdqw",
"asqwqwe": "asqwqwe"
}
```### memory
| count | ImmutableStringMap | map[string]string |
|---|---|---|
| 10000 | 3735552 | 12820480 |### time
|action | ImmutableStringMap | map[string]string |
|---|---|---|
| indexkey get | 77.2 ns/op | 28.5 ns/op |
| noindexkey get | 324 ns/op | 28.5 ns/op |
| range | 445 ns/op | 167 ns/op |
| toMap | 1096 ns/op | |### API
|name | intro |
|---|---|
| NewIndexerFactory | specify keys to create an index factory that can accelerate Get method|
| FromMap | create an ImmutableStringMap from a map[string]string and an indexerfactory|
| .Get | get value by key store in this map |
| .Range | iterate pass k, v to the func in args |
| .Map | convert current ImmutableStringMap to a map[string]string |