4.6ns per operation
diff --git a/example/main.go b/example/main.go
index 066ffb4..6e2674c 100644
--- a/example/main.go
+++ b/example/main.go
@@ -31,33 +31,33 @@
}
// TODO: experiment with: sequence, gate = writer.Reserve(sequence, gate)
-// func publish(written *disruptor.Cursor, upstream disruptor.Barrier) {
-// sequence := disruptor.InitialSequenceValue
-// writer := disruptor.NewWriter(written, upstream, BufferSize)
-// for sequence <= Iterations {
-// sequence = writer.Reserve()
-// ringBuffer[sequence&BufferMask] = sequence
-// writer.Commit(sequence)
-// }
-// }
-
func publish(written *disruptor.Cursor, upstream disruptor.Barrier) {
- previous := disruptor.InitialSequenceValue
- gate := disruptor.InitialSequenceValue
-
- for previous <= Iterations {
- next := previous + 1
-
- for next-BufferSize > gate {
- gate = upstream.Read(next)
- }
-
- ringBuffer[next&BufferMask] = next
- written.Store(next)
- previous = next
+ sequence := disruptor.InitialSequenceValue
+ writer := disruptor.NewWriter(written, upstream, BufferSize)
+ for sequence <= Iterations {
+ sequence = writer.Reserve()
+ ringBuffer[sequence&BufferMask] = sequence
+ writer.Commit(sequence)
}
}
+// func publish(written *disruptor.Cursor, upstream disruptor.Barrier) {
+// previous := disruptor.InitialSequenceValue
+// gate := disruptor.InitialSequenceValue
+
+// for previous <= Iterations {
+// next := previous + 1
+
+// for next-BufferSize > gate {
+// gate = upstream.Read(next)
+// }
+
+// ringBuffer[next&BufferMask] = next
+// written.Store(next)
+// previous = next
+// }
+// }
+
type SampleConsumer struct{}
func (this SampleConsumer) Consume(lower, upper int64) {
diff --git a/writer.go b/writer.go
index fa799b1..a1bd178 100644
--- a/writer.go
+++ b/writer.go
@@ -30,23 +30,14 @@
}
func (this *Writer) Reserve() int64 {
- // next := this.previous + 1
- // wrap := next - this.capacity
- wrap := (this.previous + 1) - this.capacity
+ next := this.previous + 1
- if wrap > this.gate {
- min := this.upstream.Read(0) // interface call: 1.20ns per operation
- for wrap > min {
- min = this.upstream.Read(0)
- }
-
- this.gate = min // update stateful variable: 1.20ns per operation
+ for next-this.capacity > this.gate {
+ this.gate = this.upstream.Read(0)
}
- this.previous++
- // this.previous = next
- return this.previous
- // return next
+ this.previous = next
+ return next
}
func (this *Writer) Commit(sequence int64) {