Skip to content

Commit

Permalink
README.md: avoid []byte -> string allocation/copy (coocood#98)
Browse files Browse the repository at this point in the history
FreeCache invests a lot of effort to minimize GC overhead, yet the README example has a case where a (potentially very large) copy is made just as part of a `fmt.Println` call. With this change, it uses `fmt.Printf("%s\n", ...)`, which should be used in any case involving `Println` and a `[]byte` that's intended to be interpreted as string data.

Change-Id: I01e6067e3c2da12dfd75e391728265b749b848ca
  • Loading branch information
extemporalgenome authored and wangjingxuan committed Sep 27, 2021
1 parent a308261 commit aea4bb6
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ got, err := cache.Get(key)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(got))
fmt.Printf("%s\n", got)
}
affected := cache.Del(key)
fmt.Println("deleted key ", affected)
Expand Down
4 changes: 0 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ func (cache *Cache) Touch(key []byte, expireSeconds int) (err error) {
func (cache *Cache) Get(key []byte) (value []byte, err error) {
hashVal := hashFunc(key)
segID := hashVal & segmentAndOpVal
cache.locks[segID].Lock()
value, _, err = cache.segments[segID].get(key, nil, hashVal, false)
cache.locks[segID].Unlock()
return
}

Expand All @@ -96,9 +94,7 @@ func (cache *Cache) Get(key []byte) (value []byte, err error) {
func (cache *Cache) GetFn(key []byte, fn func([]byte) error) (err error) {
hashVal := hashFunc(key)
segID := hashVal & segmentAndOpVal
cache.locks[segID].Lock()
err = cache.segments[segID].view(key, fn, hashVal, false)
cache.locks[segID].Unlock()
return
}

Expand Down

0 comments on commit aea4bb6

Please sign in to comment.