diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-04-28 02:32:50 +0200 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-04-28 02:32:50 +0200 |
commit | a4dc83d8f09cfb675334bc58db66f552a4e4d099 (patch) | |
tree | ef08235fdb50b96c7e1420fc46a2bff17b658a29 /skate | |
parent | 77b04eece2faad6710c42a0965edb970692235f8 (diff) | |
download | refcat-a4dc83d8f09cfb675334bc58db66f552a4e4d099.tar.gz refcat-a4dc83d8f09cfb675334bc58db66f552a4e4d099.zip |
use sync.Pool for temporary data
Diffstat (limited to 'skate')
-rw-r--r-- | skate/schema.go | 34 | ||||
-rw-r--r-- | skate/set/set.go | 8 | ||||
-rw-r--r-- | skate/set/set_test.go | 3 |
3 files changed, 37 insertions, 8 deletions
diff --git a/skate/schema.go b/skate/schema.go index baead09..dea3509 100644 --- a/skate/schema.go +++ b/skate/schema.go @@ -5,6 +5,7 @@ import ( "regexp" "strconv" "strings" + "sync" "git.archive.org/martin/cgraph/skate/isbn" "git.archive.org/martin/cgraph/skate/set" @@ -14,6 +15,16 @@ var ( isbn10Regex = regexp.MustCompile(`[O0-9xX -]{10,18}`) isbn13Regex = regexp.MustCompile(`9[O0-9xX -]{12,20}`) + rune16pool = sync.Pool{ + New: func() interface{} { + return make([]rune, 0, 16) + }, + } + setPool = sync.Pool{ + New: func() interface{} { + return set.New() + }, + } ) // RefToRelease converts a ref to a release. Set a extra.skate.status flag to @@ -66,27 +77,36 @@ func parseIsbn(s string) []string { var ( candidates10 = isbn10Regex.FindAllString(s, -1) candidates13 = isbn13Regex.FindAllString(s, -1) - valid = set.New() + u []rune + z string ) + valid := setPool.Get().(*set.Set) + valid.Clear() + defer setPool.Put(valid) for _, v := range append(candidates10, candidates13...) { - var u []rune + u = rune16pool.Get().([]rune) + u = u[:0] for _, c := range v { + if c == 'O' { + c = '0' + } if c >= '0' && c <= '9' || c == 'x' || c == 'X' { u = append(u, c) } } - s := string(u) - if !isbn.Validate(s) { + z = string(u) + rune16pool.Put(u) + if !isbn.Validate(z) { continue } - if len(s) < 12 { - w, err := isbn.To13(s) + if len(z) < 12 { + w, err := isbn.To13(z) if err != nil { continue } valid.Add(w) } else { - valid.Add(s) + valid.Add(z) } } return valid.Slice() diff --git a/skate/set/set.go b/skate/set/set.go index a198094..6bad47e 100644 --- a/skate/set/set.go +++ b/skate/set/set.go @@ -5,9 +5,15 @@ import ( "strings" ) -// Set implements basic string set operations. +// Set implements basic string set operations, not thread-safe. type Set map[string]struct{} +func (s *Set) Clear() { + for k := range *s { + delete(*s, k) + } +} + // Add adds an element. func (s *Set) Add(v string) *Set { (*s)[v] = struct{}{} diff --git a/skate/set/set_test.go b/skate/set/set_test.go index c319759..403b6df 100644 --- a/skate/set/set_test.go +++ b/skate/set/set_test.go @@ -36,4 +36,7 @@ func TestSet(t *testing.T) { top.Add("2") top.Add("3") is.Equal(r.TopK(2), &top) + + r.Clear() + is.Equal(r.Len(), 0) } |