Stack vs Heap Memory

Keep in mind my examples are all coming from quant finance use cases. I'm sure sir Avi and Prof Duffy can highlight other cases.

Large Data Structures: Use heap memory for order books, price matrices, and data structures that handle historical price data, as these can easily grow large with high-frequency trading data or long backtesting periods. Learned this lesson all too painfully!


Dynamic Sizing: In finance applications since we often deal with datasets of unknown or varying size (e.g., market data, trades, or order flow), the heap is useful when data structures need to expand dynamically.


Persistence Across Trades and Positions: If an object, like a position or a trade, needs to persist beyond a single function or class, allocate it on the heap to ensure it remains accessible across modules or parts of your application.


Parallel Processing: For multi-threaded operations, like parallelized Monte Carlo simulations or batch processing of trades, heap allocation allows shared data structures to be accessed safely by multiple threads. This one is a life saver to remember.


Optimization with Custom Allocators: In performance-sensitive areas, custom allocators (often used with heap-allocated data) can optimize latency and memory layout, crucial for low-latency trading systems or real-time risk calculations. Sir Avi might have specific ideas from FX high frequency land.


Avoiding Stack Overflow: For deeply nested calculations or recursive functions, especially in algorithmic trading, use the heap to avoid stack overflow risks when handling complex data structures.


Library and API Interactions: Many finance libraries (like QuantLib) use heap allocation for compatibility and extensibility, particularly for structures that require frequent updates, such as option pricing trees or volatility surfaces. Quantlib is a great lib to play with also btw.

By no means a complete list but hopefully tickles the brain a bit to get you thinking about these type of things.
 
Finance use cases is exactly my target problem.

Large Data Structures: Use heap memory for order books, price matrices, and data structures that handle historical price data, as these can easily grow large with high-frequency trading data or long backtesting periods. Learned this lesson all too painfully!
Using heap memory in these cases makes sense --- I agree with this completely

Dynamic Sizing: In finance applications since we often deal with datasets of unknown or varying size (e.g., market data, trades, or order flow), the heap is useful when data structures need to expand dynamically.
Not exactly sure what you are getting at here. A quote or a trade coming in from a downstream venue is just an event...unless, of course, you're referring to how it impacts the order book.

Persistence Across Trades and Positions: If an object, like a position or a trade, needs to persist beyond a single function or class, allocate it on the heap to ensure it remains accessible across modules or parts of your application.
Do you really allocate heap memory for new position info or trades? I hear that in true HFT systems, there's very little heap allocation.

Avoiding Stack Overflow: For deeply nested calculations or recursive functions, especially in algorithmic trading, use the heap to avoid stack overflow risks when handling complex data structures.
What are some examples of these deeply nested calculations? I am primarily focused on true HFT systems...so think CitSec, Optiver, HRT, etc. They don't seem to have crazy complex calcs due to the nature of being fast.
 
Back
Top Bottom