top | item 22389239

Show HN: A first project in Rust – in-memory order book

91 points| cstein2 | 6 years ago |github.com | reply

33 comments

order
[+] jansc|6 years ago|reply
Heh, I also released my first project in Rust yesterday. An ncurses gopher client. I probably missed many patterns and idioms as well. Starting with Rust is quite hard, even as an experienced developer. I spent hours "fighting" the compiler. Code available at https://github.com/jansc/ncgopher/
[+] kenshi|6 years ago|reply
Could you talk a bit about the kinds of things you had to fight the compiler on?

Can you identify any idioms or approaches that seem normal or natural in other languages, but which are problematic in Rust?

[+] cstein2|6 years ago|reply
Feedback welcome! I'm sure I'm missing loads of Rust patterns and idioms...
[+] ShorsHammer|6 years ago|reply
Some comments:

Async/await syntax is poorly documented across the ecosystem but has landed now and is useful, without it codebases will look outdated soon enough, hyper itself has some of the best docs

Lazystatic is popular way to use globals like that yet don't really bother with it anymore. The paradigms are a bit different for a language, you are discouraged from such things for a reason, there's certainly ways around using globals.

You don't need extern crate with 2018 edition and macros are automatically imported

[+] Misdicorl|6 years ago|reply
If this is intended as anything more than a toy, you don't want to store the full OpenLimitOrder in the book data structure.

The symbol should be allocated once on the book. The side is allocated twice in each set of vecs. The price is defined in the vec itself. The queue should be a vec of IDs and open size. The real struct should only be constructed lazily as absolutely needed.

[+] cstein2|6 years ago|reply
I suspect it will remain a toy, but agreed on those optimizations
[+] asplake|6 years ago|reply
I don’t do Rust but that was nicely readable. One comment: using Enums for stock symbols presumably forces a recompile on each new symbol. What would be the idiomatic representation in Rust for things like that? Some kind of interned string?
[+] roblabla|6 years ago|reply
A string works. If a numerical/in-place representation is wanted for efficiency, you could rely on the fact that stock symbols are always less than 8 characters, so we can parse it into an array of 8 bytes.
[+] aloukissas|6 years ago|reply
Great stuff! Since we're working on things like that on the daily, you have inspired me to do something like that in elixir :)
[+] evdubs|6 years ago|reply
Why use a Vec instead of a B-Tree?
[+] cstein2|6 years ago|reply
I was imagining inserts at an unoccupied price level would be relatively rare in normal order book operation, so a Vec just seemed simpler
[+] evdubs|6 years ago|reply
Also, why use Uuids instead of a pair of user_id: u64, order_id: u64?
[+] logicchains|6 years ago|reply
Not the OP, but presumably to minimise the amount of jumping around in memory. Best orderbook is a small matrix, if you can get away with it (for trading you often don't need the whole orderbook, or at least don't need the whole thing on the hot path, but for an exchange obviously there's less scope for approximations).
[+] globular-toast|6 years ago|reply
Why a B-tree if it's supposed to be in-memory? A heap would be easier to implement and more efficient.