Skip to content

Commit

Permalink
Performance improvement by not sorting every time. (#12)
Browse files Browse the repository at this point in the history
Signed-off-by: Ed Warnicke <[email protected]>
  • Loading branch information
edwarnicke authored Oct 30, 2020
1 parent 2dc42a7 commit f7ad331
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package serialize

import (
"sort"
"sync"
"sync/atomic"
)
Expand Down Expand Up @@ -82,23 +81,20 @@ func (e *Executor) process(jb *job) {
e.buffer = e.buffer[len(e.buffer):]
e.mu.Unlock()
// Sort the buffer
sort.SliceStable(buf, func(i, j int) bool {
return buf[i].ticket < buf[j].ticket
})
for len(buf) > 0 {
// If buf[0].ticket == ticket (the next ticket to process), process it
if buf[0].ticket == ticket {
buf[0].f()
close(buf[0].done)
for i := 0; i < len(buf); {
if buf[i].ticket == ticket {
buf[i].f()
close(buf[i].done)
if atomic.CompareAndSwapUintptr(&e.ticket, ticket, 0) {
return
}
buf[i] = buf[0]
buf = buf[1:]
ticket++
i = 0
continue
}
// If buf[0] != ticket, we have yet to receive the next job and need to drain the buffer again
break
i++
}
}
}

0 comments on commit f7ad331

Please sign in to comment.