package main
import "sync"
var (
stringCache map[string]string
stringCacheMu sync.Mutex
stringCacheTotal int // allows to calculate efficiency
)
// internString ensures there's only one string instance for a given s.
// Should only be used for strings that you believe have multiple copies.
// It saves memory in that case. If used for mostly unique strings, it'll
// create overhead
func internString(s string) string {
if len(s) == 0 {
return s
}
stringCacheMu.Lock()
defer stringCacheMu.Unlock()
if stringCache == nil {
stringCache = make(map[string]string)
}
stringCacheTotal++
res, ok := stringCache[s]
if ok {
return res
}
stringCache[s] = s
return s
}