Browse Source

backend: reuse timer in run().

Benchmarks:

```
import (
	"testing"
	"time"
)

func BenchmarkTimeAfter(b *testing.B) {
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		select {
		case <- time.After(1 * time.Millisecond):
		}
	}
}

func BenchmarkTimerReset(b *testing.B) {
	b.ReportAllocs()
	t := time.NewTimer(1 * time.Millisecond)
	for n := 0; n < b.N; n++ {
		select {
		case <- t.C:
		}
		t.Reset(1 * time.Millisecond)
	}
}
```

Running reveals that each loop results in 3 allocs:

```
BenchmarkTimeAfter-4 	    2000	   1112134 ns/op	     192 B/op	       3 allocs/op
BenchmarkTimerReset-4	    2000	   1109774 ns/op	       0 B/op	       0 allocs/op
```
Nikita Vetoshkin 9 years ago
parent
commit
dbc7c2cf4e
1 changed files with 4 additions and 2 deletions
  1. 4 2
      mvcc/backend/backend.go

+ 4 - 2
mvcc/backend/backend.go

@@ -179,15 +179,17 @@ func (b *backend) Size() int64 {
 
 
 func (b *backend) run() {
 func (b *backend) run() {
 	defer close(b.donec)
 	defer close(b.donec)
-
+	t := time.NewTimer(b.batchInterval)
+	defer t.Stop()
 	for {
 	for {
 		select {
 		select {
-		case <-time.After(b.batchInterval):
+		case <-t.C:
 		case <-b.stopc:
 		case <-b.stopc:
 			b.batchTx.CommitAndStop()
 			b.batchTx.CommitAndStop()
 			return
 			return
 		}
 		}
 		b.batchTx.Commit()
 		b.batchTx.Commit()
+		t.Reset(b.batchInterval)
 	}
 	}
 }
 }