From 3269a47ee7e72afab95d117e9618fe25f524d5b4 Mon Sep 17 00:00:00 2001 From: lnu Date: Mon, 1 Mar 2021 06:11:35 +0100 Subject: [PATCH] feat: use RWMutex for map access --- src/environment.go | 8 ++++---- src/regex.go | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/environment.go b/src/environment.go index fd266989df55..3da9b866d2d6 100644 --- a/src/environment.go +++ b/src/environment.go @@ -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) { @@ -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 @@ -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 } diff --git a/src/regex.go b/src/regex.go index c12226ce428e..8cd9f57bf205 100644 --- a/src/regex.go +++ b/src/regex.go @@ -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 }