Leadaxe 在 PR 中重做 AWG2 接收缓冲区优化
Leadaxe 表示,原 PR 基于未用于发行的 lx 分支,因此在发行所用的 lx-awg2-v007 分支另建 PR #6。新方案允许约 128 个读槽位随包进入队列,超过预算才复制到紧凑缓冲区,并按元素记录缓冲区归属。其 macOS 内存绑定测试中,满积压时堆增长为 11.9 MiB,原实现为 66.2 MiB,始终复制方案为 4.2 MiB;仍希望用路由器实测评估取舍。核验时 PR #6 尚未合并。
作者原文@LeadaxeThank you for this, and for the write-up: the diagnosis is right, and it is a problem in code this fork inherited and never looked at from that side.
I can't merge this PR as it is, and I can't fix it up in place either:
- The base.
lxis the default branch of this repository, but it is not what releases are built from. sing-box-lx pins3ed0471, the tip oflx-awg2-v007, and the two lines have diverged by about two hundred commits — you ran into that yourself when the release would not compile against this branch. Merging here would change nothing in a release. That the default branch points at the wrong line is my fault, and I will sort it out.- Editing your branch. The head repository belongs to an organization, and GitHub does not offer "allow edits from maintainers" for organization-owned forks, so I have no way to push to
fix/lx-rx-packet-sized-buffers. Retargeting the PR would not help either: the commit is built onlx, so a new base would pull in the whole divergence.So I rebuilt the change on the release line in #6, with you as co-author. It takes your idea and changes how it is applied:
- A packet is copied only once a device already has a read batch (128 slots) waiting in its queues. Below that it crosses as before, with no copy. In my stand the always-copy variant costs +7% per packet for plain WireGuard and +16% with
s4; the budgeted one +3% and +2%.- Each queued element owns its buffer and returns the one it holds, instead of a device-wide mode consulted at every call site. That is what makes the mix possible, and it removes the iOS special case.
- The compact buffers come from a pool with the same accounting as the read slots, so Android gets the benefit too instead of being left on full slots.
- The guard for batched and GRO TUNs from this PR is kept. Note that your port on the release base (
392d8d3, the one KeenProxy pins) does not have it: with a system TUN that writes in batches, the TUN would get buffers too small to merge into. Nothing breaks, the merge is just skipped, but it is worth knowing.The price is memory: a full backlog in the stand takes 11.9 MiB with the budget against 4.2 MiB with your port (66.2 MiB before), because 128 slots still travel uncopied.
What I do not have is a real path: my numbers come from an in-memory bind on macOS. If you can run your router measurement against the new branch, that would settle whether the budget is worth its 8 MiB.
I'm leaving this PR open until that is decided.