Boost C++ Libraries: Ticket #2563: [boost][serialization] void_upcast is very slow https://svn.boost.org/trac10/ticket/2563 <p> My preference would be to detect mulitple inheritance at compile time if that's possible. </p> <p> Make a TRAK ticket for this item. </p> <p> Robert Ramey </p> <p> <strong><em>Jacob wrote:</em></strong><em> The original "inline void_upcast" method checks if the pointer to the object is of the right type, by traversing its parents. This is necessary when using multiple inheritance, since in such a case the adress of the pointer to the object can vary depending on which base class it is cast to.</em> </p> <p> <em>While profiling one of my projects I noticed that most of the time was spent in the void_upcast method. I modified the method to return the pointer directly(see below), resulting in a high performance increase. This "solution" is possible if you avoid multiple inheritance all together, which I did. In my particular project I experienced a 75% speed increase implementing this, going from about 10 seconds to 2.5 seconds.</em> </p> <p> <em>Would it be possible to implement some functionality to turn on and off the upcasting for given classes, kind of like the tracking of objects is currently administrated?</em> </p> <p> <em>Modified "inline void_upcast":</em> </p> <pre class="wiki">/inline void * void_upcast( extended_type_info const &amp; derived, extended_type_info const &amp; base, void * const t ){ return const_cast&lt;void*&gt;(t); }/ </pre><p> <em>Yours sincerely,</em> </p> <p> <em>Jacob Holm</em> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2563 Trac 1.4.3 Robert Ramey Thu, 04 Dec 2008 20:29:20 GMT status changed https://svn.boost.org/trac10/ticket/2563#comment:1 https://svn.boost.org/trac10/ticket/2563#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> I am interested in making the library faster. </p> <p> But, I don't see your solution working execept in special cases. void_upcast also "shifts" the pointer much like </p> <p> static_cast&lt;A *&gt;(b) </p> <p> would. Your solution would work only when the derived class has not member variables </p> <p> Robert Ramey </p> Ticket jacobholm@… Fri, 05 Dec 2008 10:03:02 GMT attachment set https://svn.boost.org/trac10/ticket/2563 https://svn.boost.org/trac10/ticket/2563 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">Main.cpp</span> </li> </ul> <p> static_cast test </p> Ticket jacobholm@… Fri, 05 Dec 2008 10:03:59 GMT <link>https://svn.boost.org/trac10/ticket/2563#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2563#comment:2</guid> <description> <p> The project I am using contains derived classes with member variables. If possible, I think that the best way would be to modify void_upcast to use static_cast&lt;T&gt; with templates. That way one would be able to use multiple inheritance as well. Another solution could be to have an option to disable upcast alltogether, letting the user keep track of inheritance between classes. If used correctly this last solution will work with multiple inheritance as well. </p> <p> I have attached an example cpp file to the ticket which uses static_cast on derived objects, together with multiple inheritance. </p> <p> Yours sincerely, </p> <p> Jacob Holm </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Thu, 11 Dec 2008 16:28:01 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2563#comment:3 https://svn.boost.org/trac10/ticket/2563#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> I've looked at the code and your test. I've concluded the following: </p> <p> a) I won't dispute that void_upcast is slower than a static cast. It may be possible to make it faster. b) I double checked the serialization code to verify that void_upcast was only being invoked when it was absolutely necessary. I'm satisfied that it is. c) So I've concluded that your proposed change will cause the serialization library to fail except in some cases. d) I took a look at your test. It's not obvious to me what it is actually meant to show and how it would relate to the replacement of void_upcast with a static cast. </p> <p> Robert Ramey </p> Ticket jacobholm@… Fri, 12 Dec 2008 08:41:06 GMT <link>https://svn.boost.org/trac10/ticket/2563#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2563#comment:4</guid> <description> <pre class="wiki">/inline void * void_upcast( extended_type_info const &amp; derived, extended_type_info const &amp; base, void * const t ){ return const_cast&lt;void*&gt;(t); }/ </pre><p> The code above was the first to be suggested. This code will not work with multiple inheritance. Then there would be need for a method that detects if there is multiple inheritance or not. But if we were to modify it to use templates and static_cast it would work with multiple inheritance as well: </p> <pre class="wiki">template &lt;typename A&gt; class cast { public: template&lt;typename B&gt; static A* void_upcast( B* const t ) { return static_cast&lt;A*&gt;(const_cast&lt;B*&gt;(t)); } }; </pre><p> Example using the above code: </p> <pre class="wiki">const B *t; cast&lt;A*&gt;::void_upcast(t); </pre><p> This solution is based on that we know the class type so that we can call the template class. </p> <p> I also have some comments your conclusions: </p> <p> c) Why will the serialization fail with static_cast? Perhaps you could provide a code example? </p> <p> d) It is meant to show that static_cast may change the pointer when casting with multiple inheritance, and never change the pointer when using single inheritance. </p> <p> Yours sincerely, </p> <p> Jacob Holm </p> </description> <category>Ticket</category> </item> </channel> </rss>