Writes to stateful variables are expensive.
diff --git a/example/main.go b/example/main.go
index 3501919..8e4f1d1 100644
--- a/example/main.go
+++ b/example/main.go
@@ -28,52 +28,38 @@
reader.Stop()
finished := time.Now()
fmt.Println(Iterations, finished.Sub(started))
-
- time.Sleep(time.Millisecond * 10)
}
func publish(written, read *disruptor.Cursor) {
+ sequence := disruptor.InitialSequenceValue
+ writer := disruptor.NewWriter(written, read, BufferSize)
- // sequence := disruptor.InitialSequenceValue
- // writer := &disruptor.Writer2{}
-
- // // writer := disruptor.NewWriter(written, read, BufferSize)
- // for sequence < Iterations {
- // sequence = writer.Reserve()
- // }
-
- // sequence := disruptor.InitialSequenceValue
- // writer := disruptor.NewWriter(written, read, BufferSize)
-
- // for sequence <= Iterations {
- // sequence = writer.Reserve()
- // ringBuffer[sequence&BufferMask] = sequence
- // written.Sequence = sequence
- // // writer.Commit(sequence)
- // }
-
- // fmt.Println(writer.Gating())
-
- gating := 0
-
- previous := disruptor.InitialSequenceValue
- gate := disruptor.InitialSequenceValue
-
- for previous <= Iterations {
- next := previous + 1
- wrap := next - BufferSize
-
- for wrap > gate {
- gate = read.Sequence
- gating++
- }
-
- ringBuffer[next&BufferMask] = next
- written.Sequence = next
- previous = next
+ for sequence <= Iterations {
+ sequence = writer.Reserve()
+ ringBuffer[sequence&BufferMask] = sequence
+ writer.Commit(sequence)
}
- fmt.Println("Gating", gating)
+ // gating := 0
+
+ // previous := disruptor.InitialSequenceValue
+ // gate := disruptor.InitialSequenceValue
+
+ // for previous <= Iterations {
+ // next := previous + 1
+ // wrap := next - BufferSize
+
+ // for wrap > gate {
+ // gate = read.Sequence
+ // gating++
+ // }
+
+ // ringBuffer[next&BufferMask] = next
+ // written.Sequence = next
+ // previous = next
+ // }
+
+ // fmt.Println("Gating", gating)
}
type SampleConsumer struct{}
diff --git a/writer.go b/writer.go
index c68ad33..d58ce77 100644
--- a/writer.go
+++ b/writer.go
@@ -6,7 +6,9 @@
capacity int64
previous int64
gate int64
-} // TODO: padding?
+ pad1 int64
+ pad2 int64
+}
func NewWriter(written *Cursor, upstream Barrier, capacity int64) *Writer {
assertPowerOfTwo(capacity)
@@ -31,11 +33,16 @@
next := this.previous + 1
wrap := next - this.capacity
- for wrap > this.gate {
- this.gate = this.upstream.Read(next)
+ 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
}
- this.previous = next
+ this.previous = next // update stateful variable: 1.20ns per operation
return next
}