Socket
Socket
Sign inDemoInstall

github.com/google/triemap

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/google/triemap

Package triemap allows creating a map out of `[]rune` and `[]byte` without casting with `string(v)`. Due to lack of generics the values are stored and retrieved as `interface{}` and require type asserting like `m.Get(k).(Foo)`. Compared to casting a rune or byte slice to a string, putting data in the map is actually better using the std map like `m[string(k)] = v`, however getting data out of the map is much faster with the `RuneSliceMap`. `ByteSliceMap` is always slower than casting to string and using a standard map, but it reduces allocations, so it's a decent alternative on memory-constrained resources. `RuneSliceMap` performs much faster and produces less allocations than casting to string and using standard maps.


Version published

Readme

Source

triemap

Map using []rune or []byte instead of string

Disclaimer: This is not an official Google product.

  • RuneSliceMap is much faster than casting and using a map like: map[string(slice)].
  • ByteSliceMap is considerably slower than standard maps, but it reduces allocations.

Usage

var m triemap.RuneSliceMap

m.PutString("foo", 123)
m.Put([]rune{'b', 'a', 'r'}, 456)

v, ok := m.Get([]rune{'f', 'o', 'o'})
fmt.Println(v, ok) // 123, true

v, ok = m.GetString("bar")
fmt.Println(v, ok) // 456, true

v, ok = m.GetString("baz")
fmt.Println(v, ok) // nil, baz

Benchmarks

name                     time/op
GetByteMap20/ByteMap-16  1.82µs ± 0%
GetByteMap20/StdMap-16    883ns ±14%
GetRuneMap20/RuneMap-16   639ns ± 0%
GetRuneMap20/StdMap-16   1.45µs ± 5%
PutByteMap20/ByteMap-16  2.13µs ± 2%
PutByteMap20/StdMap-16    579ns ± 6%
PutRuneMap20/RuneMap-16  1.09µs ± 5%
PutRuneMap20/StdMap-16   1.26µs ± 4%

name                     alloc/op
GetByteMap20/ByteMap-16   0.00B
GetByteMap20/StdMap-16     320B ± 0%
GetRuneMap20/RuneMap-16   0.00B
GetRuneMap20/StdMap-16     320B ± 0%
PutByteMap20/ByteMap-16    320B ± 0%
PutByteMap20/StdMap-16     128B ± 0%
PutRuneMap20/RuneMap-16    320B ± 0%
PutRuneMap20/StdMap-16     208B ± 0%

name                     allocs/op
GetByteMap20/ByteMap-16    0.00
GetByteMap20/StdMap-16     20.0 ± 0%
GetRuneMap20/RuneMap-16    0.00
GetRuneMap20/StdMap-16     20.0 ± 0%
PutByteMap20/ByteMap-16    20.0 ± 0%
PutByteMap20/StdMap-16     19.0 ± 0%
PutRuneMap20/RuneMap-16    20.0 ± 0%
PutRuneMap20/StdMap-16     20.0 ± 0%

Code of Conduct

Same as Go

Contributing

Read our contributions doc.

tl;dr: Get Google's CLA and send a PR!

Licence

Apache 2.0

FAQs

Last updated on 19 Jun 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc