3

I'm coding an intrusive data structure and wondering whether to use base or member hooks. As the code will be called many times, my question regards performance and to what extent the compilers are able to inline such code.

Base hooks are based on inheritance while member hooks use pointers-to-members via template parameters. My design choice would be to use member hooks, but my experience says pointers are much harder to optimize than static code. On the other hand, all those pointers are known at compile time and perhaps the compiler can do some magic to analyze what's happening.

Does anyone has some experience with this? Any data, hints or references are welcome.

dtldarek
  • 949
  • 1
  • 13
  • 17
  • Could you provide code sample? – ArmanHunanyan Feb 11 '14 at 13:26
  • @ArmanHunanyan What kind of an example do you have in mind? There is a good description at [boost intrusive](http://www.boost.org/doc/libs/1_55_0/doc/html/intrusive/usage.html). As for my own code, it would be too large and out of context to put it here. – dtldarek Feb 11 '14 at 13:32
  • Note: you are forgetting another alternative => an attribute `template class IntrusiveListHook` that the class should publicly expose, and which itself does not expose anything publicly (but which has a `template friend class IntrusiveList`) – Matthieu M. Feb 11 '14 at 16:13

2 Answers2

0

As for most "X vs Y, what is faster?" questions there is only one proper answer for this one:

Ask your profiler.

Experience is vague. Human guesswork can not take into account all the nitty gritty details and pitfalls of compiler optimizations. Compilers differ in what they can optimize and how good they do it. Sometimes even between different versions of the same compiler. The only thing that can tell you well how your implementations can be optimized by your specific compiler(s) on your specific platform(s) is a proper measurement of performance with typical problem sizes.

Even if there is someone who tells you he knows what is faster and gives you some pretty graphs: can you trust him enough to not make those measurements? Does he know what your specific environment looks like? Does he and his graphs take into account the special corner cases of your problems? Most probably not.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
  • I would trust tests like [these](http://www.boost.org/doc/libs/1_55_0/doc/html/intrusive/performance.html), and even if I don't expect such a comprehensible answer, data I don't trust completely could still be useful (e.g. to asses if potential gains are worth the trouble). – dtldarek Feb 11 '14 at 13:54
-1

Since data and hooks are in a "has a" Relationship I'd also prefer member hooks from a design point of view. I also don't think there is a difference in optimization between putting the hooks in a base class than putting them into a class directly.

There is also some consideration about these different approaches in Boost intrusive.

TNA
  • 2,595
  • 1
  • 14
  • 19
  • There might be very well a difference in optimization, since maybe the optimizer cannot see through the layers of inheritance, maybe virtual functions are used etc. You can't tell wothout knowing the optimizer's capabilities and the candidate implementations at hand. – Arne Mertz Feb 11 '14 at 13:35