veyron/runtimes/google/lib/deque: minor optimization in reserve().
OLD:
BenchmarkPushFront 10000000 272 ns/op
BenchmarkPushBack 10000000 204 ns/op
BenchmarkPopFront 100000000 18.7 ns/op
BenchmarkPopBack 100000000 22.4 ns/op
NEW:
BenchmarkPushFront 10000000 236 ns/op
BenchmarkPushBack 10000000 173 ns/op
BenchmarkPopFront 100000000 18.6 ns/op
BenchmarkPopBack 100000000 22.2 ns/op
Change-Id: I014fdbb0f2da6abe6c42cd1cadf0e91a86c7cc01
diff --git a/runtimes/google/lib/deque/deque.go b/runtimes/google/lib/deque/deque.go
index 54ddbfb..a104de2 100644
--- a/runtimes/google/lib/deque/deque.go
+++ b/runtimes/google/lib/deque/deque.go
@@ -105,10 +105,9 @@
q.contents = make([]interface{}, initialQueueSize)
return
}
- contents := make([]interface{}, len(q.contents)*2)
- for i := 0; i != q.size; i++ {
- contents[i] = q.contents[(q.fx+i)%len(q.contents)]
- }
+ contents := make([]interface{}, q.size*2)
+ i := copy(contents[:], q.contents[q.fx:])
+ copy(contents[i:], q.contents[:q.fx])
q.contents = contents
q.fx = 0
q.bx = q.size
diff --git a/runtimes/google/lib/deque/deque_test.go b/runtimes/google/lib/deque/deque_test.go
index 2e34c41..ae0d4d3 100644
--- a/runtimes/google/lib/deque/deque_test.go
+++ b/runtimes/google/lib/deque/deque_test.go
@@ -209,3 +209,41 @@
t.Errorf("Iteration did not stop correctly,expected 6 iterations, got %d", iterations)
}
}
+
+func BenchmarkPushFront(b *testing.B) {
+ var q T
+ for i := 0; i < b.N; i++ {
+ q.PushFront(i)
+ }
+}
+
+func BenchmarkPushBack(b *testing.B) {
+ var q T
+ for i := 0; i < b.N; i++ {
+ q.PushBack(i)
+ }
+}
+
+func BenchmarkPopFront(b *testing.B) {
+ var q T
+ for i := 0; i < b.N; i++ {
+ q.PushBack(i)
+ }
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ q.PopFront()
+ }
+}
+
+func BenchmarkPopBack(b *testing.B) {
+ var q T
+ for i := 0; i < b.N; i++ {
+ q.PushFront(i)
+ }
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ q.PopBack()
+ }
+}