Skip to content

Commit

Permalink
feat: use RWMutex for map access
Browse files Browse the repository at this point in the history
  • Loading branch information
lnu authored and JanDeDobbeleer committed Mar 1, 2021
1 parent 7d785df commit 3269a47
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/environment.go
Expand Up @@ -68,7 +68,7 @@ type environmentInfo interface {

type commandCache struct {
commands map[string]string
lock sync.Mutex
lock sync.RWMutex
}

func (c *commandCache) set(command, path string) {
Expand All @@ -78,8 +78,8 @@ func (c *commandCache) set(command, path string) {
}

func (c *commandCache) get(command string) (string, bool) {
c.lock.Lock()
defer c.lock.Unlock()
c.lock.RLock()
defer c.lock.RUnlock()
if cmd, ok := c.commands[command]; ok {
command = cmd
return command, true
Expand All @@ -97,7 +97,7 @@ func (env *environment) init(args *args) {
env.args = args
cmdCache := &commandCache{
commands: make(map[string]string),
lock: sync.Mutex{},
lock: sync.RWMutex{},
}
env.cmdCache = cmdCache
}
Expand Down
4 changes: 3 additions & 1 deletion src/regex.go
Expand Up @@ -7,12 +7,14 @@ import (

var (
regexCache map[string]*regexp.Regexp = make(map[string]*regexp.Regexp)
regexCacheLock = sync.Mutex{}
regexCacheLock = sync.RWMutex{}
)

func getCompiledRegex(pattern string) *regexp.Regexp {
// try in cache first
regexCacheLock.RLock()
re := regexCache[pattern]
regexCacheLock.RUnlock()
if re != nil {
return re
}
Expand Down

0 comments on commit 3269a47

Please sign in to comment.