Hi folks. New to Go, and very much liking what I see. I value simplicity and think I would feel at home here.
But I’ve run into an early perormance problem with my project and am hoping for advice.
The use-case
This is an event-based financial backtesting app that will be streaming millions to billions of ticks and bars and needs to keep track of hundreds of indicator calculations through computationally intensive optimisations.
So I need a performant moving window of the most recent x indicator results, where x ranges from around 10 to around 1000.
Benchmark in Rust: 290 ms
Playing with Rust, the standard lib offers the VecDeque structure, with push_back, pop_front and index access to the internal values of the queue.
For 100,000,000 iterations on a VecDeque of 10 ints, this runs @ 290 ms.
Benchmark in Go: 1.2 secs
I don’t see a similar data structure in Go, so I benchmarked a naive approach using the slice:
p := []int{1,1,1,1,1,1,1,1,1,1,}
for i := 0; i < 100_000_000; i++ {
p = p[1:]
p = append(p, 10)
}
This runs in 1.2 secs, or a full 4x slower than Rust.
For some reason using a fixed-length slice is slower, @ 1.4 secs.
On the plus side, Go is almost 5x faster than my benchmark in C#!
I tried a couple of specialised deque libraries in Go to get a sense of what is possible, even though they didn’t support indexing to enable me to access internal values.
The 500 star oleiade/lane
library took a woeful 14 seconds.
Another more obscure library that claimed to be highly optimised gave me 1.2 seconds - no better than my slice implementation.
Do I really have to settle for a 4x slowdown?
I realised that with a GC language I’d take a performance hit in return for ease of development. But this seems like a low-allocation operation, so I’m surprised at the width of the performance gap.
Do I really have to settle for a 4x slowdown in my app if I build it in Go?
This is literally the first code I’ve written in the language and my understanding of the internals is still hazy, so I’m hoping I’m overlooking something that would give me a speedup.