Skip to content

Commit

Permalink
perf: skip count is only checked in state default
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito committed Jan 18, 2025
1 parent 8ed5e37 commit daec047
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ Benchmarks were performed against [`ua-parser/uap-go`](https://github.com/ua-par
cd ./benchmarks
go test -bench=. -benchmem ./...

MedamaParserGetSingle-12 3806265 314.8 ns/op 0 B/op 0 allocs/op
MedamaParserGetSingle-12 3871813 308.3 ns/op 0 B/op 0 allocs/op
MileusnaParserGetSingle-12 1322602 917.3 ns/op 600 B/op 16 allocs/op
UAPParserGetSingle-12 986428 1159 ns/op 233 B/op 8 allocs/op

MedamaParserGetAll-12 71653 15960 ns/op 0 B/op 0 allocs/op
MedamaParserGetAll-12 71804 15544 ns/op 0 B/op 0 allocs/op
MileusnaParserGetAll-12 28375 42301 ns/op 28031 B/op 716 allocs/op
UAPParserGetAll-12 18645 56951 ns/op 10179 B/op 344 allocs/op
```
Expand Down
11 changes: 6 additions & 5 deletions trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,14 @@ type RuneTrie struct {
func (trie *RuneTrie) Get(key string) UserAgent {
state := stateDefault
node := trie

var ua UserAgent

// Number of runes to skip when iterating over the trie. This is used
// to skip over version numbers or language codes.
var skipCount uint8

for i, r := range key {
if skipCount > 0 {
skipCount--
continue
}

switch state {
case stateSkipWhitespace:
if r == ' ' {
Expand All @@ -95,6 +91,11 @@ func (trie *RuneTrie) Get(key string) UserAgent {
}

case stateDefault:
if skipCount > 0 {
skipCount--
continue
}

// Strip any other version numbers from other products to get more hits to the trie.
//
// Also do not use a switch here as Go does not generate a jump table for switch
Expand Down

0 comments on commit daec047

Please sign in to comment.