How to create objects and allocate data only once in C++ to improve speed with octave .oct files?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/406373

  •  08-03-2021
  •  | 
  •  

Question

I have been coding some octave .oct files lately (C++), and for my purposes speed is of the essence.

It seems to me that creating C++ objects (in general) can take some time. I was wondering if there is some way to circumvent this? Maybe to somehow create a statically allocated object the first time the function is called and then to return it every time. Although I don't know how it could be possible to do this memory-safely.

It is safe to assume I want to return a sparse matrix object, and I know an upper bound on the number of nonzero elements, so that all the information required for creating the object once-and-for-all exists at first function call.

Just to create the object seems to take some 0.05 seconds but the overhead of calling oct file is only on magnitude 5e-5 seconds. That is a factor of a thousand which can be very impactful if the rest of the code is heavily optimized computational C-code which often runs faster than 1e-5 seconds.

Was it helpful?

Solution

How many objects do you have to create?

If it is just one object, then what @candied_orange said is the way to go.

If it is multiple objects, then I would suggest an object pool, which would be filled in a separate thread once it gets under a certain limit.

If it is a single threaded application and you need multiple objects, then I would also suggest an object pool, but you would allocate memory for multiple objects at once, and once it is consumed, allocate it again.

OTHER TIPS

There are two patterns that cover this problem: Singleton and Pure Dependency Injection.

Singletons are widely maligned because they effectively become globals and thus have all the associated complications if you try to refactor or reuse code. But they still work as well as they ever did.

Pure Dependency Injection has you create your one shot objects in main (or the closest to it that your framework will let you get). The fancy people call that your composition root. Since main is called only once anything you build there is only built once. If you need something in different places you pass a reference (or pointer) into those different places. Done this way your one shots only get built once but can be passed anywhere. All without silly globals. Only problem is now you have to actually allow that to be wired up. It's not free. It's work. But now things are explicit about what they need. And you only take the hit for constructing these objects once at the start of the program.

If you want to pay for it later, as needed, well you could go back to the Singleton pattern. It can cache the object. But if that global thing bugs you, well you can always inject factories that also know how to cache.

Licensed under: CC-BY-SA with attribution
scroll top