[PR #131] [MERGED] Experimental bench runtime opt #158

Closed
opened 2026-10-13 21:04:08 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/amnezia-vpn/amneziawg-go/pull/131
Author: @FrogRocky
Created: 4/2/2026
Status: ✅ Merged
Merged: 4/3/2026
Merged by: @ygurov

Base: experimental ← Head: experimental-bench-runtime-opt


📝 Commits (5)

  • 3075cbd test(benchmarks): add conn and conceal benchmark suites
  • 837dbe8 perf(conceal): optimize record and prelude hot paths
  • 5d257a5 fixes
  • 0895b3c fixes
  • 58ddc66 Merge branch 'experimental-bench' into experimental-bench-runtime-opt

📊 Changes

12 files changed (+3923 additions, -56 deletions)

View changed files

➕ conceal/benchmark_helpers_test.go (+581 -0)
➕ conceal/benchmark_tcp_test.go (+901 -0)
➕ conceal/benchmark_udp_test.go (+1253 -0)
📝 conceal/buffer.go (+2 -2)
📝 conceal/conn_framed.go (+12 -2)
📝 conceal/conn_masquarade.go (+43 -26)
➕ conceal/conn_optimizations_test.go (+408 -0)
📝 conceal/conn_prelude.go (+50 -20)
📝 conceal/rule.go (+2 -2)
➕ conceal/slice_buffer.go (+42 -0)
➕ conn/benchmark_conn_test.go (+622 -0)
📝 conn/bind_std.go (+7 -4)

📄 Description

Summary

The main goals are:

  • make the relevant conceal and conn runtime paths measurable in a repeatable way
  • reduce CPU and allocation overhead in the highest-impact conceal record/prelude paths
  • keep protocol-visible behavior unchanged while tightening a couple of batch/runtime hot paths

What Changed

Benchmark coverage

Added dedicated benchmark suites for the paths this work is targeting:

  • TCP/UDP/batch conceal benchmarks
  • conn endpoint, coalesce/split, and StdNetBind send/receive benchmarks
  • deterministic benchmark helpers and fake transport fixtures so measurements are stable and don’t depend on live sockets

Conceal hot-path optimizations

Optimized the core record/prelude write/read plumbing by:

  • replacing hot-path bytes.NewBuffer usage with narrow slice-backed helpers
  • switching hot callers to value-style FlexBuffer / context construction where possible
  • removing loop-local defer from targeted write paths
  • returning pooled buffers explicitly after downstream writes complete
  • fixing write-side bookkeeping to emit the actual encoded bytes rather than relying on writeContext.Len()

Follow-up runtime fixes

Applied two small local runtime optimizations discovered during regression follow-up:

  • FramedBatchConn.WriteBatch: explicit pooled-buffer cleanup instead of loop-local defer
  • non-Linux StdNetBind.receiveIP: initialize only the single message slot actually used by the fallback ReadMsgUDP path

Regression coverage

Added focused conceal-side regression tests for:

  • stream / UDP / batch masquerade writes
  • framed batch read/write behavior
  • batch prelude write ordering

Benchmark Results

Environment:

  • goos: linux
  • goarch: amd64
  • cpu: Intel(R) Xeon(R) Silver 4210R CPU @ 2.40GHz
  • old.txt = experimental
  • new.txt = this branch
  • n=10

Headline results

  • conceal geomean sec/op: 518.0ns -> 446.1ns (-13.88%)

  • conceal geomean B/op: -17.53%

  • conceal geomean allocs/op: -16.38%

  • conceal geomean throughput: +16.11%

  • conn geomean sec/op: 427.5ns -> 411.1ns (-3.83%)

  • conn geomean B/op: flat

  • conn geomean allocs/op: flat

  • conn geomean throughput: +3.90%

Notable wins

  • TCPRecordMasquerade/WriteRecord/mixed-4: -21.10%
  • TCPPrelude/Write/initiation_mixed-4: -25.71%
  • TCPPipeline/Write/full_pipeline/mixed: -15.24%
  • UDPMasquerade/Write/mixed-4: -15.39%
  • UDPPrelude/Write/initiation_mixed-4: -23.77%
  • UDPBatchMasquerade/Write/batch64/mixed_transport: -28.92%
  • UDPBatchFramed/Write/batch64/compat_off/transport_small: -30.30%
  • UDPBatchPrelude/Write/batch64/mixed_initiation_presence: -29.33%
  • UDPBatchPipeline/Write/batch64/full_pipeline/mixed_initiation_presence: -34.12%
  • ConnStdNetBindReceive/batch: -5.48%
  • ConnStdNetBindReceive/batch_rx_offload: -7.00%

Regressions

The attached Linux run shows one meaningful sec/op regression:

  • UDPBatchFramed/Read/batch8/compat_on/initiation-4: +3.51%

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/amnezia-vpn/amneziawg-go/pull/131 **Author:** [@FrogRocky](https://github.com/FrogRocky) **Created:** 4/2/2026 **Status:** ✅ Merged **Merged:** 4/3/2026 **Merged by:** [@ygurov](https://github.com/ygurov) **Base:** `experimental` ← **Head:** `experimental-bench-runtime-opt` --- ### 📝 Commits (5) - [`3075cbd`](https://github.com/amnezia-vpn/amneziawg-go/commit/3075cbdd1f9c2a85f54789b3ea36f232c00ad0c0) test(benchmarks): add conn and conceal benchmark suites - [`837dbe8`](https://github.com/amnezia-vpn/amneziawg-go/commit/837dbe87d324f83935277cfc68cb466365083ffa) perf(conceal): optimize record and prelude hot paths - [`5d257a5`](https://github.com/amnezia-vpn/amneziawg-go/commit/5d257a57818061e0a1d8ae57b40b1eafde2b6ba5) fixes - [`0895b3c`](https://github.com/amnezia-vpn/amneziawg-go/commit/0895b3c31a67dad4db64a12a99747b24a0d7c477) fixes - [`58ddc66`](https://github.com/amnezia-vpn/amneziawg-go/commit/58ddc66e119846b674617efe914edb840ede8a9c) Merge branch 'experimental-bench' into experimental-bench-runtime-opt ### 📊 Changes **12 files changed** (+3923 additions, -56 deletions) <details> <summary>View changed files</summary> ➕ `conceal/benchmark_helpers_test.go` (+581 -0) ➕ `conceal/benchmark_tcp_test.go` (+901 -0) ➕ `conceal/benchmark_udp_test.go` (+1253 -0) 📝 `conceal/buffer.go` (+2 -2) 📝 `conceal/conn_framed.go` (+12 -2) 📝 `conceal/conn_masquarade.go` (+43 -26) ➕ `conceal/conn_optimizations_test.go` (+408 -0) 📝 `conceal/conn_prelude.go` (+50 -20) 📝 `conceal/rule.go` (+2 -2) ➕ `conceal/slice_buffer.go` (+42 -0) ➕ `conn/benchmark_conn_test.go` (+622 -0) 📝 `conn/bind_std.go` (+7 -4) </details> ### 📄 Description ## Summary The main goals are: - make the relevant `conceal` and `conn` runtime paths measurable in a repeatable way - reduce CPU and allocation overhead in the highest-impact conceal record/prelude paths - keep protocol-visible behavior unchanged while tightening a couple of batch/runtime hot paths ## What Changed ### Benchmark coverage Added dedicated benchmark suites for the paths this work is targeting: - TCP/UDP/batch conceal benchmarks - conn endpoint, coalesce/split, and `StdNetBind` send/receive benchmarks - deterministic benchmark helpers and fake transport fixtures so measurements are stable and don’t depend on live sockets ### Conceal hot-path optimizations Optimized the core record/prelude write/read plumbing by: - replacing hot-path `bytes.NewBuffer` usage with narrow slice-backed helpers - switching hot callers to value-style `FlexBuffer` / context construction where possible - removing loop-local `defer` from targeted write paths - returning pooled buffers explicitly after downstream writes complete - fixing write-side bookkeeping to emit the actual encoded bytes rather than relying on `writeContext.Len()` ### Follow-up runtime fixes Applied two small local runtime optimizations discovered during regression follow-up: - `FramedBatchConn.WriteBatch`: explicit pooled-buffer cleanup instead of loop-local `defer` - non-Linux `StdNetBind.receiveIP`: initialize only the single message slot actually used by the fallback `ReadMsgUDP` path ### Regression coverage Added focused conceal-side regression tests for: - stream / UDP / batch masquerade writes - framed batch read/write behavior - batch prelude write ordering ## Benchmark Results Environment: - `goos: linux` - `goarch: amd64` - `cpu: Intel(R) Xeon(R) Silver 4210R CPU @ 2.40GHz` - `old.txt` = `experimental` - `new.txt` = this branch - `n=10` ### Headline results - `conceal` geomean `sec/op`: `518.0ns -> 446.1ns` (`-13.88%`) - `conceal` geomean `B/op`: `-17.53%` - `conceal` geomean `allocs/op`: `-16.38%` - `conceal` geomean throughput: `+16.11%` - `conn` geomean `sec/op`: `427.5ns -> 411.1ns` (`-3.83%`) - `conn` geomean `B/op`: flat - `conn` geomean `allocs/op`: flat - `conn` geomean throughput: `+3.90%` ### Notable wins - `TCPRecordMasquerade/WriteRecord/mixed-4`: `-21.10%` - `TCPPrelude/Write/initiation_mixed-4`: `-25.71%` - `TCPPipeline/Write/full_pipeline/mixed`: `-15.24%` - `UDPMasquerade/Write/mixed-4`: `-15.39%` - `UDPPrelude/Write/initiation_mixed-4`: `-23.77%` - `UDPBatchMasquerade/Write/batch64/mixed_transport`: `-28.92%` - `UDPBatchFramed/Write/batch64/compat_off/transport_small`: `-30.30%` - `UDPBatchPrelude/Write/batch64/mixed_initiation_presence`: `-29.33%` - `UDPBatchPipeline/Write/batch64/full_pipeline/mixed_initiation_presence`: `-34.12%` - `ConnStdNetBindReceive/batch`: `-5.48%` - `ConnStdNetBindReceive/batch_rx_offload`: `-7.00%` ### Regressions The attached Linux run shows one meaningful `sec/op` regression: - `UDPBatchFramed/Read/batch8/compat_on/initiation-4`: `+3.51%` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-13 21:04:08 +00:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
amnezia-vpn/amneziawg-go#158
No description provided.