From 1034b17bc7ae3240ad4bc5b5f5ac2ffc3e9a2695 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 27 Jul 2021 15:44:03 -0700 Subject: [PATCH] net/tstun: buffer outbound channel The handoff between tstun.Wrap's Read and poll methods is one of the per-packet hotspots. It shows up in pprof. Making outbound buffered increases throughput. It is hard to measure exactly how much, because the numbers are highly variable, but I'd estimate it at about 1%, using the best observed max throughput across three runs. Signed-off-by: Josh Bleecher Snyder --- net/tstun/wrap.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/tstun/wrap.go b/net/tstun/wrap.go index 5297b4da7..431f33850 100644 --- a/net/tstun/wrap.go +++ b/net/tstun/wrap.go @@ -153,7 +153,7 @@ func Wrap(logf logger.Logf, tdev tun.Device) *Wrapper { // a goroutine should not block when setting it, even with no listeners. bufferConsumed: make(chan struct{}, 1), closed: make(chan struct{}), - // outbound is buffered as an optimization. + // outbound can be unbuffered; the buffer is an optimization. outbound: make(chan tunReadResult, 1), eventsUpDown: make(chan tun.Event), eventsOther: make(chan tun.Event),