3

In my new workplace, the code has a heavy use of Pimpl idiom and the reason is to reduce the compile time. But I have a basic query - Doesn't pimpl require dynamic allocation of memory? So, effectively we are allocating more memory in heap than needed. And if it is used a lot, you end up using more memory. Is it a good idea to use it then?

gsamaras
  • 66,800
  • 33
  • 152
  • 256
Rajeev Mehta
  • 599
  • 8
  • 17
  • 3
    The only extra memory overhead is the actual pointer variable itself, which will be four or eight bytes (depending if you're targeting a 32- or 64-bit system). The data for the structure have to be stored somewhere anyway. Maybe a couple of bytes for padding. Generally nothing you need to worry about, unless you start to run into low-memory situations (where you probably should be looking somewhere else first) – Some programmer dude Jun 11 '17 at 12:05
  • 2
    @Someprogrammerdude - Using the standard global allocator incurs the overhead of the the bookkeeping. The cost/reward analysis really depends on the size of the pimpl itself. A custom small object allocator may be in order. – StoryTeller - Unslander Monica Jun 11 '17 at 12:06
  • @StoryTeller True, but I still thinks it's like shooting flies with an elephant gun, in most situations. – Some programmer dude Jun 11 '17 at 12:11
  • @Rajeev Can you please elaborate on *why* you are worried about this? Do you have problems with high memory usage? Are you targeting a memory-constrained system? Micro-optimizations like that usually results in code that is harder to read, understand and maintain. If memory consumption is a problem, my first suggestion would be to look somewhere else first, perhaps using a memory debugger or allocation tracker to try and find memory leaks elsewhere. – Some programmer dude Jun 11 '17 at 12:13
  • 3
    @Someprogrammerdude - Without profiling it is indeed premature optimization with a bazooka. Still, it's an interesting question, and a venue for optimization if one considers all the variables. – StoryTeller - Unslander Monica Jun 11 '17 at 12:15
  • 1
    If you are not allocating millions of these objects I wouldn't worry – M.M Jun 11 '17 at 12:22

1 Answers1

2

...the reason is to reduce the compile time.

You meant to say the recompilation time I guess, as suggested in Is the pImpl idiom really used in practice? ("recompilation time is really decreased, since only the source file needs to be rebuilt, but not the header, and every file that includes it").

Doesn't pimpl require dynamic allocation of memory?

Not really, it needs pointers, but pointers can be set to point to anything, whether this is static or not. Read more in Pimpl idiom without using dynamic memory allocation.

And if it is used a lot, you end up using more memory.

Well, the overhead is due to the pointers (4 or 8 bytes). The data have to be stored somewhere in any case, and whether this "somewhere" is static or not, the memory is pretty much the same.

I said pretty much, because if the memory is dynamically allocated, the system has to do some housekeeping, which incurs in an overhead.

However, it is extremely unlikely to run out of memory because you used the Pimpl Idiom. If you do, then the problem somewhere else, and you would go out of memory without that idiom too.


PS: As juanchopanza stated, Memory Fragmentation("when most of your memory is allocated in a large number of non-contiguous blocks, or chunks - leaving a good percentage of your total memory unallocated, but unusable for most typical scenarios") should be taken into account too

gsamaras
  • 66,800
  • 33
  • 152
  • 256