Skip to content

Commit

Permalink
Merge pull request #37 from lithammer/no-alloc-transformer
Browse files Browse the repository at this point in the history
Add fast path for `noopTransformer`
  • Loading branch information
lithammer authored Apr 29, 2022
2 parents 0987143 + c4debc2 commit 9b4074e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion fuzzy/fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func noopTransformer() transform.Transformer {
return transform.Nop
return nopTransformer{}
}

func foldTransformer() transform.Transformer {
Expand Down Expand Up @@ -234,6 +234,11 @@ func (r Ranks) Less(i, j int) bool {
}

func stringTransform(s string, t transform.Transformer) (transformed string) {
// Fast path for the nop transformer to prevent unnecessary allocations.
if _, ok := t.(nopTransformer); ok {
return s
}

var err error
transformed, _, err = transform.String(t, s)
if err != nil {
Expand Down Expand Up @@ -262,3 +267,9 @@ func (unicodeFoldTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc
}

func (unicodeFoldTransformer) Reset() {}

type nopTransformer struct{ transform.NopResetter }

func (nopTransformer) Transform(dst []byte, src []byte, atEOF bool) (int, int, error) {
return 0, len(src), nil
}

0 comments on commit 9b4074e

Please sign in to comment.