mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-21 02:55:37 -08:00
feat: use RWMutex for map access
This commit is contained in:
parent
7d785df08a
commit
3269a47ee7
|
@ -68,7 +68,7 @@ type environmentInfo interface {
|
||||||
|
|
||||||
type commandCache struct {
|
type commandCache struct {
|
||||||
commands map[string]string
|
commands map[string]string
|
||||||
lock sync.Mutex
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *commandCache) set(command, path string) {
|
func (c *commandCache) set(command, path string) {
|
||||||
|
@ -78,8 +78,8 @@ func (c *commandCache) set(command, path string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *commandCache) get(command string) (string, bool) {
|
func (c *commandCache) get(command string) (string, bool) {
|
||||||
c.lock.Lock()
|
c.lock.RLock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.RUnlock()
|
||||||
if cmd, ok := c.commands[command]; ok {
|
if cmd, ok := c.commands[command]; ok {
|
||||||
command = cmd
|
command = cmd
|
||||||
return command, true
|
return command, true
|
||||||
|
@ -97,7 +97,7 @@ func (env *environment) init(args *args) {
|
||||||
env.args = args
|
env.args = args
|
||||||
cmdCache := &commandCache{
|
cmdCache := &commandCache{
|
||||||
commands: make(map[string]string),
|
commands: make(map[string]string),
|
||||||
lock: sync.Mutex{},
|
lock: sync.RWMutex{},
|
||||||
}
|
}
|
||||||
env.cmdCache = cmdCache
|
env.cmdCache = cmdCache
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,14 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
regexCache map[string]*regexp.Regexp = make(map[string]*regexp.Regexp)
|
regexCache map[string]*regexp.Regexp = make(map[string]*regexp.Regexp)
|
||||||
regexCacheLock = sync.Mutex{}
|
regexCacheLock = sync.RWMutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func getCompiledRegex(pattern string) *regexp.Regexp {
|
func getCompiledRegex(pattern string) *regexp.Regexp {
|
||||||
// try in cache first
|
// try in cache first
|
||||||
|
regexCacheLock.RLock()
|
||||||
re := regexCache[pattern]
|
re := regexCache[pattern]
|
||||||
|
regexCacheLock.RUnlock()
|
||||||
if re != nil {
|
if re != nil {
|
||||||
return re
|
return re
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue