Skip to content

Add Generics Support, More Idiomatic Go#31

Open
adrianosela wants to merge 1 commit into
dghubble:mainfrom
adrianosela:main
Open

Add Generics Support, More Idiomatic Go#31
adrianosela wants to merge 1 commit into
dghubble:mainfrom
adrianosela:main

Conversation

@adrianosela

@adrianosela adrianosela commented Jan 25, 2025

Copy link
Copy Markdown

Add Generics Support, More Idiomatic Go

  • Simplifies code for users of this package (avoids type assertions at runtime) and helps prevent runtime panics due to wrong type-assertions
  • Made lookup function e.g. Get() more idiomatic by returning true/false for whether item was found
  • Makes the individual trie types private e.g. PathTrie --> pathTrie, RuneTrie --> runeTrie and has constructors return the interface (better encapsulation)
  • Makes the following NOT true anymore for either implementation: Internal nodes have nil values so stored nil values cannot be distinguished and are excluded from walks., nil values can now be stored and distinguished from empty nodes.
  • Other misc. cosmetic changes.

Note that if existing users want to maintain the ability to store multiple different types of data in the trie they can still use the constructors with type any. e.g. before NewPathTrie(), now NewPathTrie[any](). While most users of this library will now be able to do NewPathTrie[string]() when they only ever expect to store strings, or NewPathTrie[*customStruct](), and so on...

UX

Before:

trie := NewPathTrie()

trie.Put("/hello/world", 27)

value := trie.Get("/hello/world")

// must do a type assertion before using value (cumbersome)
if intValue, ok := value.(int); ok {
    sum += intValue
}

// or risk a runtime panic by skipping the check part
otherSum += value.(int)

After:

trie := NewPathTrie[int]()

trie.Put("/hello/world", 27)

// value returned from lookup can be used directly
if value, ok := trie.Get("/hello/world"); ok {
    sum += value
}

Benchmark (TL;DR; No Changes)

Before:

✔ ~/go/src/github.com/dghubble/trie [main|✔]
10:15 $ go test -bench=.
goos: darwin
goarch: arm64
pkg: github.com/dghubble/trie
cpu: Apple M1 Pro
BenchmarkRuneTriePutStringKey-8   	 3474247	       323.8 ns/op	       9 B/op	       1 allocs/op
BenchmarkRuneTrieGetStringKey-8   	 3813277	       317.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkRuneTriePutPathKey-8     	 3481916	       337.2 ns/op	       9 B/op	       1 allocs/op
BenchmarkRuneTrieGetPathKey-8     	 3644137	       331.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkPathTriePutStringKey-8   	28855054	        40.92 ns/op	       8 B/op	       1 allocs/op
BenchmarkPathTrieGetStringKey-8   	39185912	        30.34 ns/op	       0 B/op	       0 allocs/op
BenchmarkPathTriePutPathKey-8     	16460866	        71.89 ns/op	       8 B/op	       1 allocs/op
BenchmarkPathTrieGetPathKey-8     	19688133	        60.69 ns/op	       0 B/op	       0 allocs/op
BenchmarkPathSegmenter-8          	45136537	        26.25 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/dghubble/trie	18.762s

After

✔ ~/go/src/github.com/adrianosela/trie [main|✔]
10:17 $ go test -bench=.
goos: darwin
goarch: arm64
pkg: github.com/dghubble/trie
cpu: Apple M1 Pro
BenchmarkRuneTriePutStringKey-8   	 3492538	       324.1 ns/op	       9 B/op	       1 allocs/op
BenchmarkRuneTrieGetStringKey-8   	 3845990	       306.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkRuneTriePutPathKey-8     	 3444878	       341.2 ns/op	       9 B/op	       1 allocs/op
BenchmarkRuneTrieGetPathKey-8     	 3705607	       322.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkPathTriePutStringKey-8   	30349173	        38.81 ns/op	       8 B/op	       1 allocs/op
BenchmarkPathTrieGetStringKey-8   	39880358	        29.80 ns/op	       0 B/op	       0 allocs/op
BenchmarkPathTriePutPathKey-8     	17097781	        69.07 ns/op	       8 B/op	       1 allocs/op
BenchmarkPathTrieGetPathKey-8     	19641246	        61.73 ns/op	       0 B/op	       0 allocs/op
BenchmarkPathSegmenter-8          	44727998	        26.20 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/dghubble/trie	18.609s

@adrianosela adrianosela changed the title Add Generics Support, More Idiomatic Get() Add Generics Support, More Idiomatic Go Jan 25, 2025
@adrianosela

Copy link
Copy Markdown
Author

Any thoughts on this @dghubble? Wondering if there is interest in this change or if I should close the PR. Feel free to close it if not.

@mistercpow

Copy link
Copy Markdown

+1 @dghubble @adrianosela

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants