github.com/Mrs4s/MiraiGo@v0.0.0-20240226124653-54bdd873e3fe/client/internal/intern/string.go (about) 1 package intern 2 3 import ( 4 "sync" 5 ) 6 7 // String Interning is a technique for reducing the memory footprint of large 8 // strings. It can re-use strings that are already in memory. 9 10 type StringInterner struct { 11 mu sync.RWMutex 12 strings map[string]string 13 } 14 15 func NewStringInterner() *StringInterner { 16 return &StringInterner{ 17 strings: make(map[string]string), 18 } 19 } 20 21 func (i *StringInterner) Intern(s string) string { 22 i.mu.RLock() 23 if v, ok := i.strings[s]; ok { 24 i.mu.RUnlock() 25 return v 26 } 27 i.mu.RUnlock() 28 i.mu.Lock() 29 i.strings[s] = s 30 i.mu.Unlock() 31 return s 32 }