I have read numerous other answers on this topic but they don't quite answer what I'm looking for. Examples:
Class members and explicit stack/heap allocation
When should a class be allocated on the stack instead of the heap
Member function memory allocation stack or heap?
These answers cover the mechanical differences between the two (automatic vs manual memory management, variable lifetimes, etc) but I am more interested in best practices, and how to write code that can scale.
Context
I am writing a class which processes a large stream of data, say 10-100s's of GB. Let's assume that the performance bottleneck is how fast my class can process the data, e.g. the source and destination of the data are both fast.
My class works by splitting the data into chunks of size N
bytes, and processing. The optimal size N
for maximal throughput depends on the processing performed, which is only known at runtime. N
can range from 10's of bytes up to 1000's of bytes. If I did everything in the stack, for say N = 256
, the total sum of member variables in the class is <1MB.
I also tried stack allocating different sizes of arrays for a small set of different N
s, and using only one at a given time. This is so far the fastest implementation. Nevertheless, comparing implementations that use all stack vs heap, performance difference is fairly small to use the heap, so that ends up being simpler.
Questions
If I make the choice to use stack vs. heap now, how does that affect future users of my class? For example, in theory one could write a program that has 100's of instances of this class. If I used all stack, and the user put all my instances on the stack, it would blow up.
How is stack usage factored into the design of a large hierarchical system? I don't see that mentioned when I read online and books. Mostly the stack is mentioned in the context of excessive recursion, trying to outright declare 100MB array, etc.
Generally, is the author of a class (whose underlying workings are abstracted away) supposed to give the end user some information about the stack footprint? Or some direction on when/whether heap allocation is required?