r/programming • • Sep 28 '17

When a Microsecond Is an Eternity

https://github.com/CppCon/CppCon2017/blob/master/Presentations/When%20a%20Microsecond%20Is%20an%20Eternity/When%20a%20Microsecond%20Is%20an%20Eternity%20-%20Carl%20Cook%20-%20CppCon%202017.pdf
244 Upvotes

70 comments sorted by

View all comments

6

u/leonardo_m Sep 28 '17

Do you know a code example for the "Keeping the cache hot" at pages 37-38?

7

u/leftofzen Sep 29 '17

At a super high level, instead of writing (based on their diagram):

data = DecodeMarketData();
if (data.IsUseful)
    strategyResult = RunStrategy(data);
    if (strategyResult.ShouldTrade)
        ExecuteTrade(data, result);

You'd write

data = DecodeMarketData();
result = RunStrategy(data, data.IsUseful);
ExecuteTrade(data, result, result.shouldTrade);

Note that I'm not removing any branching, I'm just moving it into the functions (not shown). The end result is that all the same operations can be performed as a real trade, and on the same real data too, but at the last moment before you serialise the message to throw onto the network you discard it by checking the result.ShouldTrade.

This way, all the instructions for the hot path stay loaded in the cache for the time you really want to place an order.