mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 19:17:38 +08:00
33 lines
570 B
Go
33 lines
570 B
Go
package intern
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// String Interning is a technique for reducing the memory footprint of large
|
|
// strings. It can re-use strings that are already in memory.
|
|
|
|
type StringInterner struct {
|
|
mu sync.RWMutex
|
|
strings map[string]string
|
|
}
|
|
|
|
func NewStringInterner() *StringInterner {
|
|
return &StringInterner{
|
|
strings: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
func (i *StringInterner) Intern(s string) string {
|
|
i.mu.RLock()
|
|
if v, ok := i.strings[s]; ok {
|
|
i.mu.RUnlock()
|
|
return v
|
|
}
|
|
i.mu.RUnlock()
|
|
i.mu.Lock()
|
|
i.strings[s] = s
|
|
i.mu.Unlock()
|
|
return s
|
|
}
|