mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Do not use defer for unlocking in Scratch
It is a fairly costly operation: ``` benchmark old ns/op new ns/op delta BenchmarkScratchGet-4 109 31.6 -71.01% benchmark old allocs new allocs delta BenchmarkScratchGet-4 0 0 +0.00% benchmark old bytes new bytes delta BenchmarkScratchGet-4 0 0 +0.00% ´´´
This commit is contained in:
parent
fa1a9653e5
commit
c846b051f3
1 changed files with 11 additions and 10 deletions
|
@ -31,11 +31,11 @@ type Scratch struct {
|
||||||
//
|
//
|
||||||
// If the first add for a key is an array or slice, then the next value(s) will be appended.
|
// If the first add for a key is an array or slice, then the next value(s) will be appended.
|
||||||
func (c *Scratch) Add(key string, newAddend interface{}) (string, error) {
|
func (c *Scratch) Add(key string, newAddend interface{}) (string, error) {
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
|
|
||||||
var newVal interface{}
|
var newVal interface{}
|
||||||
|
c.mu.RLock()
|
||||||
existingAddend, found := c.values[key]
|
existingAddend, found := c.values[key]
|
||||||
|
c.mu.RUnlock()
|
||||||
if found {
|
if found {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -57,7 +57,9 @@ func (c *Scratch) Add(key string, newAddend interface{}) (string, error) {
|
||||||
} else {
|
} else {
|
||||||
newVal = newAddend
|
newVal = newAddend
|
||||||
}
|
}
|
||||||
|
c.mu.Lock()
|
||||||
c.values[key] = newVal
|
c.values[key] = newVal
|
||||||
|
c.mu.Unlock()
|
||||||
return "", nil // have to return something to make it work with the Go templates
|
return "", nil // have to return something to make it work with the Go templates
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,46 +67,45 @@ func (c *Scratch) Add(key string, newAddend interface{}) (string, error) {
|
||||||
// This value can later be retrieved with Get.
|
// This value can later be retrieved with Get.
|
||||||
func (c *Scratch) Set(key string, value interface{}) string {
|
func (c *Scratch) Set(key string, value interface{}) string {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
|
||||||
|
|
||||||
c.values[key] = value
|
c.values[key] = value
|
||||||
|
c.mu.Unlock()
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns a value previously set by Add or Set
|
// Get returns a value previously set by Add or Set
|
||||||
func (c *Scratch) Get(key string) interface{} {
|
func (c *Scratch) Get(key string) interface{} {
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
defer c.mu.RUnlock()
|
val := c.values[key]
|
||||||
|
c.mu.RUnlock()
|
||||||
|
|
||||||
return c.values[key]
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetInMap stores a value to a map with the given key in the Node context.
|
// SetInMap stores a value to a map with the given key in the Node context.
|
||||||
// This map can later be retrieved with GetSortedMapValues.
|
// This map can later be retrieved with GetSortedMapValues.
|
||||||
func (c *Scratch) SetInMap(key string, mapKey string, value interface{}) string {
|
func (c *Scratch) SetInMap(key string, mapKey string, value interface{}) string {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
|
||||||
|
|
||||||
_, found := c.values[key]
|
_, found := c.values[key]
|
||||||
if !found {
|
if !found {
|
||||||
c.values[key] = make(map[string]interface{})
|
c.values[key] = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
c.values[key].(map[string]interface{})[mapKey] = value
|
c.values[key].(map[string]interface{})[mapKey] = value
|
||||||
|
c.mu.Unlock()
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSortedMapValues returns a sorted map previously filled with SetInMap
|
// GetSortedMapValues returns a sorted map previously filled with SetInMap
|
||||||
func (c *Scratch) GetSortedMapValues(key string) interface{} {
|
func (c *Scratch) GetSortedMapValues(key string) interface{} {
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
defer c.mu.RUnlock()
|
|
||||||
|
|
||||||
if c.values[key] == nil {
|
if c.values[key] == nil {
|
||||||
|
c.mu.RUnlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
unsortedMap := c.values[key].(map[string]interface{})
|
unsortedMap := c.values[key].(map[string]interface{})
|
||||||
|
c.mu.RUnlock()
|
||||||
var keys []string
|
var keys []string
|
||||||
for mapKey := range unsortedMap {
|
for mapKey := range unsortedMap {
|
||||||
keys = append(keys, mapKey)
|
keys = append(keys, mapKey)
|
||||||
|
|
Loading…
Reference in a new issue