Menu

From 11 allocations to 2: speeding up a FIX engine's price encoder in Go
📰

From 11 allocations to 2: speeding up a FIX engine's price encoder in Go

DEV Community: go·Rohan Singh Rajput·1 day ago
#1d2gLz9n
#devtogo#devto#dev#decimal#scale
Reading 0:00
15s threshold

FIX is the protocol that moves orders and quotes between brokers, exchanges and trading firms. QuickFIX/Go is a widely used Go implementation of it. Every price and quantity on every outgoing message passes through one small function, so I went looking at what it costs.

Status: the change is open as quickfixgo/quickfix#787 , discussed in #786 . I'll update this post if it changes in review.

The function

FIXDecimal.Write turns a decimal into the bytes that go on the wire:

func ( d FIXDecimal ) Write () [] byte { return [] byte ( d . Decimal . StringFixed ( d . Scale )) }

Enter fullscreen mode

Exit fullscreen mode

It looks harmless. The benchmark says otherwise:

BenchmarkFIXDecimalWrite 155 ns/op 208 B/op 11 allocs/op

Enter fullscreen mode

Exit fullscreen mode

Eleven heap allocations to format -124.3456 . StringFixed rounds through big.Int arithmetic, builds intermediate strings, and then the result is copied again into a []byte .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.