https://dominictobias.medium.com/go-is-about-to-get-a-whole-lot-faster-a50c1e7d60b9

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 balanced binary trees 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

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

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 for a language already known to be unusually both fast and easy, that low level and often performance critical data collections can get such a performance boost out of generics.

P.S. You can find the updated fork over here.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.