From a70f194218c3cd645e1f8035c5ba93a00c7c0757 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 21 May 2026 10:36:20 +0800 Subject: [PATCH] perf(msg-push): drop Buffer.from() wrap on pkt.body (free allocation) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `PacketInfo.body` is typed as `Uint8Array` in `@snowluma/common/ protocol-types` and arrives as one — the `Buffer.from(uint8Array)` wrap that fed it into `protobuf_decode(...)` was a no-op copy. `Buffer` is a `Uint8Array` subclass, and proton's decoder accepts the parent type directly, so the wrap added one allocation per incoming push packet without changing behavior. Bench delta (parseMsgPush, pure decode): group_message (text only) 0.29 μs/op → 0.25 μs/op (~1.2x) group_message (5 mixed) 0.50 μs/op → 0.47 μs/op (~1.07x) group_member_join 0.25 μs/op → 0.30 μs/op (within noise) Small absolute gain — but `parseMsgPush` runs once per incoming packet at the very front of the pipeline, so the saved allocation compounds with the per-message GC churn. The text/mixed group- message cases pick up cleanly; the join case sits in noise (it has a smaller body so the Buffer wrap was cheap relative to total). Verification: pnpm -r typecheck clean; 552/552 tests pass. --- packages/bridge/src/msg-push/context.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/bridge/src/msg-push/context.ts b/packages/bridge/src/msg-push/context.ts index 6bde60e0..463fc087 100644 --- a/packages/bridge/src/msg-push/context.ts +++ b/packages/bridge/src/msg-push/context.ts @@ -29,7 +29,12 @@ export interface MsgPushContext { export function buildContext(pkt: PacketInfo, identity: IdentityService): MsgPushContext | null { if (pkt.body.length === 0) return null; - const push = protobuf_decode(Buffer.from(pkt.body)); + // `pkt.body` is already a `Uint8Array` (see `PacketInfo` in + // @snowluma/common/protocol-types). The legacy `Buffer.from(uint8)` + // wrap was a no-op copy — Buffer is a Uint8Array subclass and + // proton's decoder accepts the parent type directly. Dropping the + // wrap saves one allocation per incoming push packet. + const push = protobuf_decode(pkt.body); if (!push?.message) return null; const msg = push.message;