I have a class that uses boost::variant to store a double or a string, like this :
class value
{
boost::variant<double, std::string> val;
};
It's supposed to be an immutable value type for a toy interpreter I'm playing with. At first it seemed like a good idea to pass it around by const reference and return by value, and have it allocated on stack always, since I wanted it treated as a primitive. However, then I saw that it's size is 40 bytes (due to sizeof std::string, mostly), and I was a bit worried. I know I shouldn't allocate large chunks of memory on stack, but how large is too large?
Also, copying 40 bytes every time I return, especially since the value is immutable and doesn't even need to be copied, seems like a bit of a waste.
The option regular heap allocation doesn't seem too attractive, as I could have thousands of these allocations/deallocations per second.
The final option I came up with is to have a boost::pool to allocate these objects when needed, and use a boost::shared_ptr to manage their lifetime. However, because the interpreter is in charge of memory allocation (the type of memory allocation will be a policy passed to the interpreter as a template argument), this would mean that the value class would have to know about the interpreter, which slightly complicates things.
So these are the questions :
- What should I do in this case and why?
- How big is "too big" to allocate on stack? I'm sure that depends on how often it's allocated and how often it has to be copied, too.
Thanks.