internal: avoid allocation In order to pass a value as a variadic argument, Go allocates a slice. This slice needs to be on the heap if the method is an interface method, as it is unknown whether the slice will escape. This can be avoided by returning a pointer instead of an interface. Change-Id: I6f0cb9a0c512515815116f0d0a515813ee7afaf5 Reviewed-on: https://go-review.googlesource.com/30273 Run-TryBot: Marcel van Lohuizen <mpvl@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Nigel Tao <nigeltao@golang.org>
diff --git a/internal/match.go b/internal/match.go index 03190b9..a67fcac 100644 --- a/internal/match.go +++ b/internal/match.go
@@ -29,21 +29,23 @@ // // A Match will indicate an Exact match if the language matches after // canonicalization and High if the matched tag is a parent. -func NewInheritanceMatcher(t []language.Tag) language.Matcher { - tags := make(inheritanceMatcher) +func NewInheritanceMatcher(t []language.Tag) *InheritanceMatcher { + tags := &InheritanceMatcher{make(map[language.Tag]int)} for i, tag := range t { ct, err := language.All.Canonicalize(tag) if err != nil { ct = tag } - tags[ct] = i + tags.index[ct] = i } return tags } -type inheritanceMatcher map[language.Tag]int +type InheritanceMatcher struct { + index map[language.Tag]int +} -func (m inheritanceMatcher) Match(want ...language.Tag) (language.Tag, int, language.Confidence) { +func (m InheritanceMatcher) Match(want ...language.Tag) (language.Tag, int, language.Confidence) { for _, t := range want { ct, err := language.All.Canonicalize(t) if err != nil { @@ -51,7 +53,7 @@ } conf := language.Exact for { - if index, ok := m[ct]; ok { + if index, ok := m.index[ct]; ok { return ct, index, conf } if ct == language.Und {