Skip to content

Commit 234a1ea

Browse files
authored
hotpath optimization for AddRecord (#4415)
Small optimization in the AddRecord hotpath: A quick [10s benchmark test](https://github.com/PeerDB-io/peerdb/blob/47539dc086d58d190e4a101010b60bc153d3c288/flow/model/cdc_stream_bench_test.go) shows: ``` BenchmarkAddRecordOld 1549653 767.9 ns/op 1074 B/op 23 allocs/op BenchmarkAddRecordNew 47112669 25.49 ns/op 0 B/op 0 allocs/op ``` The columns represent number of iterations / average wallclock time per iteration / average bytes of heap memory allocation per iteration / average number of distinct heap allocations per operation, respectively. This is probably not a bottleneck on CDC throughput today as we are much more likely to be bottlenecked on _processing_ cdc message, but given this code is in the hot path, removing the heap allocation and GC introduced here in the common case is still beneficial.
1 parent 089a1a4 commit 234a1ea

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

flow/model/cdc_stream.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ func (r *CDCStream[T]) AddRecord(ctx context.Context, record Record[T]) error {
6767
}
6868
}
6969

70+
// hot-path optimization: avoid setting up logger/ticker unless channel is actually full
71+
select {
72+
case r.records <- record:
73+
return nil
74+
default:
75+
}
76+
7077
logger := internal.LoggerFromCtx(ctx)
7178
ticker := time.NewTicker(10 * time.Second)
7279
defer ticker.Stop()

0 commit comments

Comments
 (0)