0

Say I have an object that takes about 30 seconds to construct on each program run. This object is constructed with the same piece of data each and every time the program is run. What solution(s) exist for this problem?

EDIT:

I did some research tailored to a more specific version of my question. In particular, I wanted to populate a hash table with some values. I found http://www.gnu.org/software/gperf/ which addresses this exact issue. gperf generates a perfect hash function and the associated table to go along.

flumpb
  • 1,707
  • 3
  • 16
  • 33
  • Possible duplicate of: http://stackoverflow.com/questions/12453623/can-i-get-a-c-compiler-to-instantiate-objects-at-compile-time – Borgleader Sep 21 '12 at 14:44
  • Is your object during construction do anything with the outer world? I mean opens sockets, files, creating semaphores, create and show windows... ? – PiotrNycz Sep 21 '12 at 14:45
  • @PiotrNycz - that was my first thought - how do you make a ctor take 30 seconds? – Martin James Sep 21 '12 at 16:16

3 Answers3

2

C++11 introduces an extended notion of "constant expression", which you can confer on objects by means of the constexpr keyword:

constexpr int a = 12;      // constant expression

constexpr int f(int n) { return a * n; }

constexpr int b = f(a);    // also constant expression

Objects may have constructors declared as constexpr as well. The rules for functions to qualify as constant expressions are very restrictive, but if you are able to build such an object, then global objects which are constant expressions may indeed be computed and stored at compile time. They count as being "statically initialized" (basically, initialized before program start-up).

Prior to C++11, only the simplest primitive types were eligible for such treatment.

Anything that requires dynamic allocation can never be a constant expression, so there's no hope of having anything like a global std::map statically initialized.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I was thinking about a map. I guess I'll have to implement something crafty then. – flumpb Sep 21 '12 at 20:52
  • @kisplit: You can of course make your own constexpr binary search tree... since it's constant, you won't need any rebalancing algorithms, so this is actually very simple... – Kerrek SB Sep 21 '12 at 21:41
  • This sounds extremely interesting, I'll give it a try. Thanks for the info! – flumpb Sep 24 '12 at 20:22
  • @kisplit: No problem. For the search, be sure to use constexpr functions and ternary conditionals, like in the second part of [this answer](http://stackoverflow.com/a/12566646/596781), to get compile-time lookup, too. – Kerrek SB Sep 24 '12 at 20:24
1

There is no generic solution to your problem. You will need to analyze your object to determine what is being created during that 30 seconds, and find some way to represent the specific data without needing intensive computation.

Once you understand how to reproduce the specific data without computation, you can easily serialize it out to whatever storage you like (including temporary storage, to later be added to your compiled object). From there, deserializing it back into your object should not be a difficult task.

This assumes, of course, that there are no dynamic resources used by your object (open files, sockets, memory allocations, etc.). You can't serialize that.

mah
  • 39,056
  • 9
  • 76
  • 93
1

consider how the object can be represented another way. for example, if it's a multilayered image you generate, you might just load a fully composited image instead of generating it each time you run.

justin
  • 104,054
  • 14
  • 179
  • 226