Skip to content

perf(sdk-go): reduce websocket order dispatch allocations - #65

Open
namtran1812 wants to merge 1 commit into
gemini:mainfrom
namtran1812:namtran/go-websocket-market-data
Open

namtran1812 wants to merge 1 commit into
gemini:mainfrom
namtran1812:namtran/go-websocket-market-data

Conversation

@namtran1812

Copy link
Copy Markdown

Summary

Reduce allocation overhead in the Go SDK WebSocket order-event dispatch path by replacing the generic map[string]json.RawMessage discriminator decode with a small typed envelope.

The envelope includes both e (event type) and E (event time) so encoding/json resolves the protocol's case-sensitive fields exactly while preserving the existing order-before-trade classification behavior.

Motivation

Private order updates may contain a top-level t trade ID. The dispatcher therefore needs to identify explicit order / orderUpdate events before applying the generic trade heuristic.

Previously, that classification decoded every candidate order frame into a map[string]json.RawMessage and then decoded the e field separately. This adds avoidable allocations on the WebSocket dispatch path.

Changes

  • Replace the map-based order-event envelope with a typed e / E envelope.
  • Preserve explicit order-event classification before trade heuristics.
  • Extend the existing regression test to cover e, numeric E, and top-level t together.
  • Add dispatch benchmarks for depth, trade, ticker, and order frames.

Benchmark

Apple M1, darwin/arm64:

Before After
Order dispatch ~5.19 µs/op ~3.96 µs/op
Allocated bytes 2,560 B/op 1,072 B/op
Allocations 44 allocs/op 20 allocs/op

This is approximately 58% fewer allocated bytes and 55% fewer allocations per order frame in the benchmark. Timing results are environment-specific.

Validation

  • go test ./... -count=1
  • go vet ./...
  • go test -race ./websocket -count=1
  • order/trade dispatch regression: 100 consecutive passes
  • public/private stream separation regression: 50 consecutive passes
  • git diff --check

@nostradamus-bot

Copy link
Copy Markdown

Nostradamus Risk Rating — Low

This PR is a performance-only refactor in the Go SDK WebSocket dispatch path (packages/sdk-go/websocket/client_dispatch.go), replacing a generic map-decode with a typed struct envelope; the change is functionally equivalent and no security-sensitive areas (auth, signing, fund-movement) are touched. The remaining diff is new benchmark tests and a minor regression test extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant