Skip to content

perf(sdk-go): cache order book price conversions - #66

Open
namtran1812 wants to merge 1 commit into
gemini:mainfrom
namtran1812:namtran/go-orderbook-optimization
Open

namtran1812 wants to merge 1 commit into
gemini:mainfrom
namtran1812:namtran/go-orderbook-optimization

Conversation

@namtran1812

Copy link
Copy Markdown

Summary

Cache the float representation of order book prices alongside their exact decimal representation.

The order book already caches parsed types.Decimal prices, but each cache hit still recomputed Decimal.Float64(). Since Float64() formats the decimal and parses it back to a float, repeated updates at the same price incurred unnecessary conversion work.

This change stores both representations in the existing bounded price cache:

  • exact types.Decimal remains the source of truth for price identity, sorting, and comparisons
  • cached float64 continues to serve float-returning metrics such as spread and mid
  • existing cache bounds and locking behavior are unchanged

Performance

Apple M1, darwin/arm64, Go benchmark comparison (benchstat, 5 runs):

Benchmark Before After Change
ApplyLevel insert 450.4 ns/op 342.9 ns/op -23.87%
ApplyLevel update 423.5 ns/op 315.4 ns/op -25.53%
LiveOrderBook IngestDiff 947.8 ns/op 695.8 ns/op -26.59%

Allocations:

Benchmark Before After
ApplyLevel insert 11 → 9 allocs/op -18.18%
ApplyLevel update 11 → 9 allocs/op -18.18%
LiveOrderBook IngestDiff 27 → 23 allocs/op -14.81%

The latency geomean decreased 25.34% across the measured paths.

Validation

  • go test ./websocket/orderbook -count=10
  • go test -race ./websocket/orderbook -count=1
  • go test ./... -count=1
  • go vet ./...
  • git diff --check

Added regression coverage verifies that cache hits preserve float-returning metrics and exact price-level identity for prices that cannot safely use float64 as their identity.

@nostradamus-bot

Copy link
Copy Markdown

Nostradamus Risk Rating — Low

PR touches only packages/sdk-go/websocket/orderbook/book.go, a pure in-memory market-data caching struct in the Go SDK with no auth, credential, financial-transaction, or trust-boundary involvement; the change is a performance optimization caching Float64() conversions alongside the existing Decimal source of truth, with regression tests explicitly covering float-precision edge cases — no security surface is affected.

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