Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitwise math for speed #11

Merged
merged 1 commit into from
Aug 3, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ The queue implemented here is as fast as it is for an additional reason: it is *
*/
package queue

// minQueueLen is smallest capacity that queue may have.
// Must be power of 2 for bitwise modulus: x % n == x & (n - 1).
const minQueueLen = 16

// Queue represents a single instance of the queue data structure.
Expand All @@ -30,7 +32,7 @@ func (q *Queue) Length() int {
// resizes the queue to fit exactly twice its current contents
// this can result in shrinking if the queue is less than half-full
func (q *Queue) resize() {
newBuf := make([]interface{}, q.count*2)
newBuf := make([]interface{}, q.count<<1)

if q.tail > q.head {
copy(newBuf, q.buf[q.head:q.tail])
Expand All @@ -51,7 +53,8 @@ func (q *Queue) Add(elem interface{}) {
}

q.buf[q.tail] = elem
q.tail = (q.tail + 1) % len(q.buf)
// bitwise modulus
q.tail = (q.tail + 1) & (len(q.buf) - 1)
q.count++
}

Expand All @@ -76,7 +79,8 @@ func (q *Queue) Get(i int) interface{} {
if i < 0 || i >= q.count {
panic("queue: Get() called with index out of range")
}
return q.buf[(q.head+i)%len(q.buf)]
// bitwise modulus
return q.buf[(q.head+i)&(len(q.buf)-1)]
}

// Remove removes the element from the front of the queue. If you actually
Expand All @@ -86,9 +90,11 @@ func (q *Queue) Remove() {
panic("queue: Remove() called on empty queue")
}
q.buf[q.head] = nil
q.head = (q.head + 1) % len(q.buf)
// bitwise modulus
q.head = (q.head + 1) & (len(q.buf) - 1)
q.count--
if len(q.buf) > minQueueLen && q.count*4 == len(q.buf) {
// Resize down if buffer 1/4 full.
if len(q.buf) > minQueueLen && (q.count<<2) == len(q.buf) {
q.resize()
}
}