summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2017-08-01 03:39:57 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2017-08-01 03:44:55 +0900
commit37370f057f5f39a54316bc7a048ab12b35004b7c (patch)
tree5acdc5b539fc785ffbceca1ee88948c02a1ab139
parentf4b46fad27b4d4f7f3f5649b5f2948c298c3a5ae (diff)
Do not use defer in performance-sensitive contexts
-rw-r--r--src/chunklist.go6
-rw-r--r--src/core.go2
-rw-r--r--src/util/eventbox.go10
3 files changed, 10 insertions, 8 deletions
diff --git a/src/chunklist.go b/src/chunklist.go
index f6bedcc5..63c60786 100644
--- a/src/chunklist.go
+++ b/src/chunklist.go
@@ -55,7 +55,6 @@ func CountItems(cs []*Chunk) int {
// Push adds the item to the list
func (cl *ChunkList) Push(data []byte) bool {
cl.mutex.Lock()
- defer cl.mutex.Unlock()
if len(cl.chunks) == 0 || cl.lastChunk().IsFull() {
newChunk := Chunk(make([]Item, 0, chunkSize))
@@ -64,15 +63,16 @@ func (cl *ChunkList) Push(data []byte) bool {
if cl.lastChunk().push(cl.trans, data, cl.count) {
cl.count++
+ cl.mutex.Unlock()
return true
}
+ cl.mutex.Unlock()
return false
}
// Snapshot returns immutable snapshot of the ChunkList
func (cl *ChunkList) Snapshot() ([]*Chunk, int) {
cl.mutex.Lock()
- defer cl.mutex.Unlock()
ret := make([]*Chunk, len(cl.chunks))
copy(ret, cl.chunks)
@@ -82,5 +82,7 @@ func (cl *ChunkList) Snapshot() ([]*Chunk, int) {
newChunk := *ret[cnt-1]
ret[cnt-1] = &newChunk
}
+
+ cl.mutex.Unlock()
return ret, cl.count
}
diff --git a/src/core.go b/src/core.go
index 74ff9e7f..0b90a51b 100644
--- a/src/core.go
+++ b/src/core.go
@@ -205,7 +205,6 @@ func Run(opts *Options, revision string) {
delay := true
ticks++
eventBox.Wait(func(events *util.Events) {
- defer events.Clear()
for evt, value := range *events {
switch evt {
@@ -265,6 +264,7 @@ func Run(opts *Options, revision string) {
}
}
}
+ events.Clear()
})
if delay && reading {
dur := util.DurWithin(
diff --git a/src/util/eventbox.go b/src/util/eventbox.go
index ccdbb99e..b710cf1e 100644
--- a/src/util/eventbox.go
+++ b/src/util/eventbox.go
@@ -26,23 +26,23 @@ func NewEventBox() *EventBox {
// Wait blocks the goroutine until signaled
func (b *EventBox) Wait(callback func(*Events)) {
b.cond.L.Lock()
- defer b.cond.L.Unlock()
if len(b.events) == 0 {
b.cond.Wait()
}
callback(&b.events)
+ b.cond.L.Unlock()
}
// Set turns on the event type on the box
func (b *EventBox) Set(event EventType, value interface{}) {
b.cond.L.Lock()
- defer b.cond.L.Unlock()
b.events[event] = value
if _, found := b.ignore[event]; !found {
b.cond.Broadcast()
}
+ b.cond.L.Unlock()
}
// Clear clears the events
@@ -56,27 +56,27 @@ func (events *Events) Clear() {
// Peek peeks at the event box if the given event is set
func (b *EventBox) Peek(event EventType) bool {
b.cond.L.Lock()
- defer b.cond.L.Unlock()
_, ok := b.events[event]
+ b.cond.L.Unlock()
return ok
}
// Watch deletes the events from the ignore list
func (b *EventBox) Watch(events ...EventType) {
b.cond.L.Lock()
- defer b.cond.L.Unlock()
for _, event := range events {
delete(b.ignore, event)
}
+ b.cond.L.Unlock()
}
// Unwatch adds the events to the ignore list
func (b *EventBox) Unwatch(events ...EventType) {
b.cond.L.Lock()
- defer b.cond.L.Unlock()
for _, event := range events {
b.ignore[event] = true
}
+ b.cond.L.Unlock()
}
// WaitFor blocks the execution until the event is received