1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-10-01 17:00:45 +00:00

memory: Use a string pool for Commands

This commit is contained in:
Kane York 2016-01-17 17:45:37 -08:00
parent c85e8b10c3
commit abb032f0c1
5 changed files with 65 additions and 6 deletions

View file

@ -0,0 +1,37 @@
package server
import (
"sync"
)
type StringPool struct {
sync.RWMutex
lookup map[string]Command
}
func NewStringPool() *StringPool {
return &StringPool{lookup: make(map[string]Command)}
}
// doesn't lock, doesn't check for dupes.
func (p *StringPool) _Intern_Setup(s string) {
p.lookup[s] = Command(s)
}
func (p *StringPool) Intern(s string) Command {
p.RLock()
ss, exists := p.lookup[s]
p.RUnlock()
if exists {
return ss
}
p.Lock()
defer p.Unlock()
ss, exists = p.lookup[s]
if exists {
return ss
}
p.lookup[s] = Command(string([]byte(s)))
return s
}