Skip to content

zstring destructor inlining

Symptom

Functions that destroy a zstring (IOI's typedef over STLport 4.6.1's basic_string<char, string_char_traits<char>, allocator<char> >) compiled to a different inlining depth from the original. Three shapes showed up:

  • Destructors of classes holding a zstring member, at the third or fourth level of inlining, call ~basic_string (_ZNSbIcSt18string_char_traitsIcESaIcEED2Ev, 0x0043cf8c) out of line in the original. Ours inlined the whole chain down to allocator<char>::deallocate. Examples: ZActionMenu, ZPickupMenu and ZDropDisplay D1/D0 in ingamemenus.cpp, and the second ZNavigator member in the discarded temporary at the top of ZHM3InventoryMenu::ZHM3InventoryMenu, which calls _String_base::~_String_base (0x0044e018) out of line.
  • ZXMLGUISystem::~ZXMLGUISystem calls ~basic_string out of line for one member and ~_String_base for the next.
  • Ordinary functions with a local or temporary zstring, such as ZActionMenu::SetPlaceRetrieveOneItemText, call allocator<char>::deallocate (0x00438678) out of line, where ours inlined it to beqz plus operator delete.

Diagnosis

Two separate GCC 3.2 behaviours are involved.

1. The destructor chain was one statement short

GCC 3.2's tree inliner (-finline-limit, default 600, counted as 10 per statement) stops inlining a callee once the statements already inlined into the current top-level call pass the limit. Our header had no user-declared ~basic_string and no ~allocator, so the chain counted fewer statements than the original's STLport headers.

Evidence:

  • ZHM3InventoryMenu::ZHM3InventoryMenu is exact at -finline-limit 570 to 599 and wrong at 600. Values in one band of ten give the same result, which means our chain was exactly one statement short.
  • analysis/recovered-types.h lists __base_dtor/__comp_dtor for allocator<char>, which matches STLport's ~allocator() _STLP_NOTHROW {}. With a user-declared destructor, _STLP_alloc_proxy and _String_base get non-trivial member cleanup, and that adds the statement.
  • The debug line table names the original headers (libraries/STLport-4.6.1/stlport/stl/_string.h, _alloc.h, _new.h), so the original used STLport's own basic_string. The out-of-line ~basic_string covers _string.h lines 124 and 305: an explicit destructor that inlines the base destructor.

2. allocator<char>::deallocate is inlined only at its first use per unit

GCC 3.2 inlines this template member only at the first call site in a translation unit; every later call goes to the out-of-line copy. A two-function probe shows it:

void First(const char *p) { zstring s(p); Use(s.c_str()); }
void Second(const char *p) { zstring s(p); Use(s.c_str()); }

First ends in jal _ZdlPv and Second ends in jal _ZNSaIcE10deallocateEPcj. An inline function that is never used does not take the first use.

The original works the same way. In ingamemenus.cpp the only string operator delete is at 0x003acce8 in ZActionMenuBase::PostInit2, the first zstring temporary in the unit. Every later string destruction calls deallocate. With PostInit2 still assembly, SetPlaceRetrieveOneItemText became the first use in our build and inlined the delete (score 37). In xmlguisystem.cpp the original's first use is the out-of-line ~_String_base copy itself (0x0044e018 inlines operator delete).

Three units had worked around this with a declaration of an explicit specialization, template <> void std::allocator<char>::deallocate(char *, unsigned int);, which makes every call in the unit out of line. That breaks the unit whose original first use is in C code (xmlguisystem.cpp), and it is not needed where the order already comes out right (guixmlreader2.cpp).

The change

src/common/zstl/zstlstring.h, three additions in STLport's form:

inline void _Destroy(char *, char *) {}
    ~allocator() {}
    ~basic_string() {
        _Destroy(this->m_pStart, this->m_pFinish + 1);
    }

The _Destroy call is needed: an empty ~basic_string() {} leaves the six menu destructors and the inventory constructor different. Both destructors are needed: without ~allocator, ~ZXMLGUISystem, ~ZHitman3, __destroy_aux and the inventory constructor differ, and without ~basic_string, the menu destructors, ~ZXMLGUISystem and the inventory constructor differ. Two more STLport layers were tried and make no difference in any affected unit, so they are left out: _STLP_alloc_proxy::deallocate and __new_alloc::deallocate/__stl_delete.

Source changes that go with it:

  • xmlguisystem.cpp: removed the allocator<char>::deallocate specialization declaration.
  • guixmlreader2.cpp: removed the same declaration. The unit is exact with or without it.
  • zhitman3.cpp: moved template ZArray<Animation::Header *>::~ZArray(); above ZHitman3::~ZHitman3. Once ~ZHitman3 is C, it inlines ~ZArray first, and an explicit instantiation placed after that no longer emits the D0/D1 copies the original has at 0x004627e4 and 0x00462820. The declaration is still present in hm3savebase.cpp and levellinking.cpp. Their original first use is in functions that are still assembly, and removing the declaration makes ZTextSliderGeneric::readParams and ZUpgradeSelector::SWeaponUpgrade::SetNumber differ.

Functions now matching

19 functions, 3,264 bytes. The full build went from 16,359 to 16,378 exact.

Needed the header change:

Function Address Bytes
ZActionMenu::~ZActionMenu D1, D0 0x00469d58, 0x00469da0 72, 88
ZPickupMenu::~ZPickupMenu D1, D0 0x00469df8, 0x00469e40 72, 88
ZDropDisplay::~ZDropDisplay D1, D0 0x00469e98, 0x00469ee0 72, 88
ZHM3InventoryMenu::ZHM3InventoryMenu 0x003c9158 236
ZXMLGUISystem::~ZXMLGUISystem D1, D0 0x0044dec4, 0x0044df58 148, 164
_String_base<char>::~_String_base 0x0044e018 40
ZHitman3::~ZHitman3 D1, D0 0x00313c30, 0x00313d44 276, 280
__destroy_aux<SProfileInfo *> 0x0046b51c 68

Emitted as a side effect of the menu destructors (they match with either header): CBaseEvent<ZWINDOW>::~CBaseEvent D2 (0x00469ff0, 36) and CWinEvent<ZWINDOW>::~CWinEvent D0, D1 (0x0046a4a0 and 0x0046a4e0, 64 and 36).

Needed the first-use order:

  • ZActionMenuBase::PostInit2 (0x003acc5c, 808), newly written. It is the original first use of deallocate in ingamemenus.cpp.
  • ZActionMenu::SetPlaceRetrieveOneItemText (0x003ae474, 516), newly written, and exact once PostInit2 is C.

Already exact at the base commit, promoted from a draft: ReadText(zstring &, const char *, const char **) (0x00267864, 112).