Boost C++ Libraries: Ticket #2359: Static initialization problems with fast_pool_allocator https://svn.boost.org/trac10/ticket/2359 <h2 class="section" id="DetailedanalysisfromJoaquínMLópezMuñoz:">Detailed analysis from Joaquín M López Muñoz:</h2> <p> Hello, I did look hard into pool/detail/singleton.hpp in the past because it deals with a problem that I also had to face in the implementation of Boost.Flyweight. These are my conclusions wrt to pool/detail/singleton.hpp and with respect to the particular problem you're now studying: </p> <ul><li>pool/detail/singleton.hpp claims that singleton_default&lt;T&gt;::instance() is automatically called before main() if no user code does it explicitly. Strictly speaking, the implementation does not guarantee this, but only that singleton_default&lt;T&gt;::instance() will be called during the so-called dynamic initialization phase of the program startup sequence. For all practical purposes, however, this is equivalent to the orignal claim, so the problem does not lie here. </li></ul><ul><li>Where the problem lies can be found by looking at the usage of singleton_pool by fast_pool_allocator: notice that singleton_pool is used *only* when fast_pool_allocator::allocate or ::deallocate are invoked. So, it is perfectly possible to construct a fast_pool_allocator without that forcing the compiler to construct the underying singleton_pool *yet*. And this is what's happening indeed, the sequence of objects construction we're having is this: </li></ul><ol><li>owned_base::pool_ begins construction </li></ol><blockquote> <p> 1.1 call of new pool_lii() inside pool::pool </p> </blockquote> <blockquote> <p> 1.1.1 a fast_pool_allocator is cted inside pool_lii ctor. </p> </blockquote> <blockquote> <p> 1.2 pool_ii ends construction </p> </blockquote> <ol start="2"><li>pool_ ends construction </li></ol><ol start="3"><li>the singleton instance associated to singleton_pool&lt;...&gt;is </li></ol><blockquote> <p> cted before main() because fast_pool_allocator uses it later </p> </blockquote> <blockquote> <p> (singleton guarantee). </p> </blockquote> <p> This sequence is consistent with the guarantees provided by singleton_default&lt;T&gt;. The problem lies in the design of </p> <p> fast_pool_allocator: as it stands, the construction of a fast_pool_allocator does *not* force the prior construction of the internal singleton instance, and this is a mistake. </p> <p> If my analysis is correct, to fix the problem one need only ensure that his construction order is preserved for instance by explicitly using singleton_pool&lt;...&gt; inside fast_pool_allocator ctors like this: </p> <pre class="wiki"> fast_pool_allocator() { singleton_pool&lt;fast_pool_allocator_tag, sizeof(T), UserAllocator, Mutex, NextSize&gt;::is_from(0); } template &lt;typename U&gt; fast_pool_allocator( const fast_pool_allocator&lt;U, UserAllocator, Mutex, NextSize&gt; &amp;) { singleton_pool&lt;fast_pool_allocator_tag, sizeof(T), UserAllocator, Mutex, NextSize&gt;::is_from(0); } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2359 Trac 1.4.3 Chris Newbold Mon, 22 Sep 2008 21:02:18 GMT <link>https://svn.boost.org/trac10/ticket/2359#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2359#comment:1</guid> <description> <p> I'm not sure I buy this explanation entirely. </p> <p> owned_base::pool_ is a global object. As described above, so is object_creator. </p> <p> The test case consists of a single translation unit, and inside that translation unit, the declaration of object_creator precedes that of owned_base::pool_ (because object_creator is declared by a Boost.Pool header which is included before the application-level code which declares owned_base). </p> <p> Following the fairly clear rules about initialization order for namespace-scoped global objects, I would have expected the compiler to construct object_creator first (and therefore the pool) before constructing owned_base::pool_. </p> <p> I'm not at all convinced that singleton_default or fast_pool_allocator is actually correct, but I do think this situation is a bit more complex... </p> <h2 class="section" id="MorefromJoaquín">More from Joaquín</h2> <p> These precedence rules do not apply to class template static data: quoting the standard (latest draft at <a class="ext-link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2691.pdf"><span class="icon">​</span>http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2691.pdf</a>), section 3.6.2: </p> <blockquote> <p> "Dynamic initialization of a non-local object with static storage duration is either ordered or unordered. Definitions of explicitly specialized class template static data members have ordered initialization. Other class template static data members (i.e., implicitly or explicitly instantiated specializations) have unordered initialization. Other objects defined in namespace scope have ordered initialization. Objects with ordered initialization defined within a single translation unit shall be initialized in theorder of their definitions in the translation unit." </p> </blockquote> <p> That is, only *ordered* file-scope objects are subject to the precedence rule you're referring to. Class template static data are not ordered by definition (except if belonging to a full specialization). So, the sequence I proposed above is consistent AFAICS. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Chris Newbold</dc:creator> <pubDate>Mon, 22 Sep 2008 21:06:57 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2359#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2359#comment:2</guid> <description> <p> Ah! This is just the sort of explanation I was looking for, but could not find--I guess my copy of the C++ standard has gone stale. </p> <p> Based on this, it certainly would appear that GCC's behavior is conformant and that the combination of fast_pool_allocator and singleton_default presumes behavior which is unspecified. </p> <p> My only concern is whether referencing singleton_default from the constructor for fast_pool_singleton will guarantee proper initialization ordering. </p> <p> 3.6.2 doesn't really shed any light on how ordered and unordered initialization may be coupled. There isn't, for example, any expressed guarantee that non-locals will be initialized prior to first reference. </p> <h2 class="section" id="MorefromJoaquín:">More from Joaquín:</h2> <p> There is no such guarantee indeed, but the fix does NOT rely on initialization rules for non-local objects, but on the initialization rules for *local* objects with static storage duration (6.7/4): the singleton is a local static object inside the function singleton_default&lt;T&gt;::instance(), which is explicitly invoked inside fast_pool_allocator ctors via singleton_pool&lt;...&gt;is_from() (if you apply the fix, that is). </p> <p> The non-local object involved in singleton_default&lt;T&gt;, namely singleton_default&lt;T&gt;::create_object is there to guarantee that singleton_default&lt;T&gt;::instance() is invoked, and thus the singleton created in dynamic initialization time *if no one has done it before*. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Chris Newbold</dc:creator> <pubDate>Mon, 22 Sep 2008 21:09:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2359#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2359#comment:3</guid> <description> <p> <em>And finally...</em> </p> <p> Quite right. I now agree your suggested fix is completely sound. </p> <p> And, in fact, making your suggested fix re-orders the construction of the pool and allocator objects as expected. I tried the experiment with GCC 4.1.2. </p> <p> The next step is for me to audit the remainder of the pool library and see if there are other clients of singleton_pool which are subject to the same potential failure mode. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Chris Newbold</dc:creator> <pubDate>Mon, 22 Sep 2008 21:14:47 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/2359#comment:4 https://svn.boost.org/trac10/ticket/2359#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket Chris Newbold Mon, 29 Sep 2008 18:45:28 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2359#comment:5 https://svn.boost.org/trac10/ticket/2359#comment:5 <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">fixed</span> </li> </ul> <p> Fixed on trunk by revision 49025. </p> Ticket anonymous Thu, 13 Jan 2011 22:19:09 GMT <link>https://svn.boost.org/trac10/ticket/2359#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2359#comment:6</guid> <description> <p> singletons are evil. This is my mind... </p> </description> <category>Ticket</category> </item> <item> <author>sehmann@…</author> <pubDate>Fri, 22 Jun 2012 18:07:27 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2359#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2359#comment:7</guid> <description> <p> Can you please address this performance issue? We are seeing a 35% slowdown on our code when using fast_pool_allocator as a result of this bug fix. <a class="ext-link" href="http://lists.boost.org/Archives/boost/2009/07/154257.php"><span class="icon">​</span>http://lists.boost.org/Archives/boost/2009/07/154257.php</a> </p> </description> <category>Ticket</category> </item> </channel> </rss>