Go is about to go a whole lot faster

Credit: https://unsplash.com/photos/CpkOjOcXdUY

I’ve been implementing the same (or as similar as possible) trading orderbook algorithm in various languages recently and one thing I noticed with Go is that it has fewer or simpler algorithms in the standard library, for example no deques and maps cannot be ordered like the std::map/BTreeMap in C++/Rust).

I was reading about others discussing this fact somewhere and a line piqued my interest that went something like “because of the runtime cost of interface{} (aka “any”), some algorithms are implemented more simplistically”. That seemed a shame for my performance critical orderbook and so I decided to do a little experiment:

What if I take the deque library I’m using and convert it to use generics? The results are quite promising…

Benchmarks without generics

go1.18beta1 darwin/amd64
cpu: VirtualApple @ 2.50GHz
BenchmarkPushFront-10 10000 25.81 ns/opBenchmarkPushBack-10 10000 40.87 ns/opBenchmarkSerial-10 10000 40.05 ns/opBenchmarkSerialReverse-10 10000 30.43 ns/opBenchmarkRotate-10 10000 30122 ns/opBenchmarkInsert-10 10000 29192 ns/opBenchmarkRemove-10 10000 13927 ns/opBenchmarkYoyo-10 10000 1801284 ns/opBenchmarkYoyoFixed-10 10000 1136005 ns/op

Benchmarks with generics

go1.18beta1 darwin/amd64
cpu: VirtualApple @ 2.50GHz
BenchmarkPushFront-10 10000 9.846 ns/opBenchmarkPushBack-10 10000 10.04 ns/opBenchmarkSerial-10 10000 11.08 ns/opBenchmarkSerialReverse-10 10000 26.22 ns/opBenchmarkRotate-10 10000 11047 ns/opBenchmarkInsert-10 10000 15644 ns/opBenchmarkRemove-10 10000 5203 ns/opBenchmarkYoyo-10 10000 561729 ns/opBenchmarkYoyoFixed-10 10000 393723 ns/op

Most of the benchmarks are 2–3x faster!

This is encouraging news that performance critical data collections can get such a boost out of generics, without having to write specialized containers per type.

P.S. You can find the updated fork here and here’s the git diff.

HackerNews discussion.
Reddit discussion.

--

--

--

Interested in programming and fintech: JS, WASM, Rust, Go, blockchain, L1/2, DApps, trading

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Intro to Flutter: a practical guide to cross-platform development

Exploring IOTA #ICT-2, Reverse engineering the code Part-1

Develop your first Spring Boot app in 5 mins

Beyond the Bootcamp: How does software look at memory?

My journey to CCIE RS — EIGRP 3

{UPDATE} Policía rápido piloto de motos 3d - subiendo la colina juego de carreras Hack Free…

What’s new in Rails 5.1?

So you think you can code…now what?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dominic Tobias

Dominic Tobias

Interested in programming and fintech: JS, WASM, Rust, Go, blockchain, L1/2, DApps, trading

More from Medium

Golang URL Shortener

Injecting Interfaces — Golang Dependency Injection

A good summary of Go lang basic syntax

TicTacGo — An Intro to Golang