Skip to content

Commit 0c993a9

Browse files
committed
perf(namekey): Remove all superfluous AsciiString allocations for name key lookups (#1959)
1 parent d809648 commit 0c993a9

File tree

67 files changed

+375
-183
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+375
-183
lines changed

Generals/Code/GameEngine/Include/Common/DamageFX.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ class DamageFXStore : public SubsystemInterface
147147
/**
148148
Find the DamageFX with the given name. If no such DamageFX exists, return null.
149149
*/
150-
const DamageFX *findDamageFX( AsciiString name ) const;
150+
const DamageFX *findDamageFX( NameKeyType namekey ) const;
151+
const DamageFX *findDamageFX( const AsciiString& name ) const;
152+
const DamageFX *findDamageFX( const char* name ) const;
151153

152154
static void parseDamageFXDefinition(INI* ini);
153155

Generals/Code/GameEngine/Include/Common/NameKeyGenerator.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ class NameKeyGenerator : public SubsystemInterface
9090
virtual void update() { }
9191

9292
/// Given a string, convert into a unique integer key.
93-
NameKeyType nameToKey(const AsciiString& name) { return nameToKey(name.str()); }
94-
NameKeyType nameToLowercaseKey(const AsciiString& name) { return nameToLowercaseKey(name.str()); }
93+
NameKeyType nameToKey(const AsciiString& name);
94+
NameKeyType nameToLowercaseKey(const AsciiString& name);
9595

9696
/// Given a string, convert into a unique integer key.
9797
NameKeyType nameToKey(const char* name);
@@ -120,6 +120,8 @@ class NameKeyGenerator : public SubsystemInterface
120120
Bool addReservedKey();
121121
#endif
122122

123+
NameKeyType nameToKeyImpl(const AsciiString& name);
124+
NameKeyType nameToLowercaseKeyImpl(const AsciiString& name);
123125
NameKeyType nameToKeyImpl(const char* name);
124126
NameKeyType nameToLowercaseKeyImpl(const char *name);
125127
NameKeyType createNameKey(UnsignedInt hash, const AsciiString& name);

Generals/Code/GameEngine/Include/Common/Upgrade.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ class UpgradeCenter : public SubsystemInterface
235235
UpgradeTemplate *firstUpgradeTemplate( void ); ///< return the first upgrade template
236236
const UpgradeTemplate *findUpgradeByKey( NameKeyType key ) const; ///< find upgrade by name key
237237
const UpgradeTemplate *findUpgrade( const AsciiString& name ) const; ///< find and return upgrade by name
238+
const UpgradeTemplate *findUpgrade( const char* name ) const; ///< find and return upgrade by name
238239
const UpgradeTemplate *findVeterancyUpgrade(VeterancyLevel level) const; ///< find and return upgrade by veterancy level
239240

240241
UpgradeTemplate *newUpgrade( const AsciiString& name ); ///< allocate, link, and return new upgrade

Generals/Code/GameEngine/Include/GameClient/Image.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ friend class ImageCollection;
116116
//-------------------------------------------------------------------------------------------------
117117
class ImageCollection : public SubsystemInterface
118118
{
119+
typedef std::map<NameKeyType, Image *> ImageMap;
119120

120121
public:
121122

@@ -128,22 +129,24 @@ class ImageCollection : public SubsystemInterface
128129

129130
void load( Int textureSize ); ///< load images
130131

132+
const Image *findImage( NameKeyType namekey ) const; ///< find image based on name key
131133
const Image *findImageByName( const AsciiString& name ) const; ///< find image based on name
134+
const Image *findImageByName( const char* name ) const; ///< find image based on name
132135

133136
/// adds the given image to the collection, transfers ownership to this object
134137
void addImage(Image *image);
135138

136139
/// enumerates the list of existing images
137140
Image *Enum(unsigned index)
138141
{
139-
for (std::map<unsigned,Image *>::iterator i=m_imageMap.begin();i!=m_imageMap.end();++i)
142+
for (ImageMap::iterator i=m_imageMap.begin();i!=m_imageMap.end();++i)
140143
if (!index--)
141144
return i->second;
142145
return NULL;
143146
}
144147

145148
protected:
146-
std::map<unsigned,Image *> m_imageMap; ///< maps named keys to images
149+
ImageMap m_imageMap; ///< maps named keys to images
147150
};
148151

149152
// INLINING ///////////////////////////////////////////////////////////////////////////////////////

Generals/Code/GameEngine/Include/GameLogic/Armor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ class ArmorStore : public SubsystemInterface
105105
void reset() { }
106106
void update() { }
107107

108+
const ArmorTemplate* findArmorTemplate(NameKeyType namekey) const;
108109
/**
109110
Find the Armor with the given name. If no such Armor exists, return null.
110111
*/
111-
const ArmorTemplate* findArmorTemplate(AsciiString name) const;
112+
const ArmorTemplate* findArmorTemplate(const AsciiString& name) const;
113+
const ArmorTemplate* findArmorTemplate(const char* name) const;
112114

113115
inline Armor makeArmor(const ArmorTemplate *tmpl) const
114116
{

Generals/Code/GameEngine/Include/GameLogic/Weapon.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,8 @@ class WeaponStore : public SubsystemInterface
814814
/**
815815
Find the WeaponTemplate with the given name. If no such WeaponTemplate exists, return null.
816816
*/
817-
const WeaponTemplate *findWeaponTemplate(AsciiString name) const;
817+
const WeaponTemplate *findWeaponTemplate(const AsciiString& name) const;
818+
const WeaponTemplate *findWeaponTemplate(const char* name) const;
818819
const WeaponTemplate *findWeaponTemplateByNameKey( NameKeyType key ) const { return findWeaponTemplatePrivate( key ); }
819820

820821
// this dynamically allocates a new Weapon, which is owned (and must be freed!) by the caller.

Generals/Code/GameEngine/Source/Common/DamageFX.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,10 @@ DamageFXStore::~DamageFXStore()
274274
}
275275

276276
//-------------------------------------------------------------------------------------------------
277-
const DamageFX *DamageFXStore::findDamageFX(AsciiString name) const
277+
const DamageFX *DamageFXStore::findDamageFX(NameKeyType namekey) const
278278
{
279-
NameKeyType namekey = TheNameKeyGenerator->nameToKey(name);
280-
DamageFXMap::const_iterator it = m_dfxmap.find(namekey);
281-
if (it == m_dfxmap.end())
279+
DamageFXMap::const_iterator it = m_dfxmap.find(namekey);
280+
if (it == m_dfxmap.end())
282281
{
283282
return NULL;
284283
}
@@ -288,6 +287,18 @@ const DamageFX *DamageFXStore::findDamageFX(AsciiString name) const
288287
}
289288
}
290289

290+
//-------------------------------------------------------------------------------------------------
291+
const DamageFX *DamageFXStore::findDamageFX(const AsciiString& name) const
292+
{
293+
return findDamageFX(TheNameKeyGenerator->nameToKey(name));
294+
}
295+
296+
//-------------------------------------------------------------------------------------------------
297+
const DamageFX *DamageFXStore::findDamageFX(const char* name) const
298+
{
299+
return findDamageFX(TheNameKeyGenerator->nameToKey(name));
300+
}
301+
291302
//-------------------------------------------------------------------------------------------------
292303
void DamageFXStore::init()
293304
{

Generals/Code/GameEngine/Source/Common/INI/INI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ void INI::parseMappedImage( INI *ini, void * /*instance*/, void *store, const vo
872872
if( TheMappedImageCollection )
873873
{
874874
typedef const Image* ConstImagePtr;
875-
*(ConstImagePtr*)store = TheMappedImageCollection->findImageByName( AsciiString( token ) );
875+
*(ConstImagePtr*)store = TheMappedImageCollection->findImageByName( token );
876876
}
877877

878878
//KM: If we are in the worldbuilder, we want to parse commandbuttons for informational purposes,
@@ -1377,7 +1377,7 @@ void INI::parseUpgradeTemplate( INI* ini, void * /*instance*/, void *store, cons
13771377
throw ERROR_BUG;
13781378
}
13791379

1380-
const UpgradeTemplate *uu = TheUpgradeCenter->findUpgrade( AsciiString( token ) );
1380+
const UpgradeTemplate *uu = TheUpgradeCenter->findUpgrade( token );
13811381
DEBUG_ASSERTCRASH( uu || stricmp( token, "None" ) == 0, ("Upgrade %s not found!",token) );
13821382

13831383
typedef const UpgradeTemplate* ConstUpgradeTemplatePtr;

Generals/Code/GameEngine/Source/Common/INI/INIMappedImage.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@
4242
//-------------------------------------------------------------------------------------------------
4343
void INI::parseMappedImageDefinition( INI* ini )
4444
{
45-
AsciiString name;
46-
4745
// read the name
48-
const char* c = ini->getNextToken();
49-
name.set( c );
46+
const char* name = ini->getNextToken();
5047

5148
//
5249
// find existing item if present, note that we do not support overrides
@@ -66,11 +63,10 @@ void INI::parseMappedImageDefinition( INI* ini )
6663
{
6764

6865
// image not found, create a new one
69-
image = newInstance(Image);
66+
image = newInstance(Image);
7067
image->setName( name );
7168
TheMappedImageCollection->addImage(image);
72-
DEBUG_ASSERTCRASH( image, ("parseMappedImage: unable to allocate image for '%s'",
73-
name.str()) );
69+
DEBUG_ASSERTCRASH( image, ("parseMappedImage: unable to allocate image for '%s'", name) );
7470

7571
}
7672

Generals/Code/GameEngine/Source/Common/NameKeyGenerator.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ Bool NameKeyGenerator::addReservedKey()
139139
}
140140
#endif
141141

142+
//-------------------------------------------------------------------------------------------------
143+
NameKeyType NameKeyGenerator::nameToKey(const AsciiString& name)
144+
{
145+
const NameKeyType key = nameToKeyImpl(name);
146+
147+
#if RTS_ZEROHOUR && RETAIL_COMPATIBLE_CRC
148+
while (addReservedKey());
149+
#endif
150+
151+
return key;
152+
}
153+
154+
//-------------------------------------------------------------------------------------------------
155+
NameKeyType NameKeyGenerator::nameToLowercaseKey(const AsciiString& name)
156+
{
157+
const NameKeyType key = nameToLowercaseKeyImpl(name);
158+
159+
#if RTS_ZEROHOUR && RETAIL_COMPATIBLE_CRC
160+
while (addReservedKey());
161+
#endif
162+
163+
return key;
164+
}
165+
142166
//-------------------------------------------------------------------------------------------------
143167
NameKeyType NameKeyGenerator::nameToKey(const char* name)
144168
{
@@ -163,6 +187,40 @@ NameKeyType NameKeyGenerator::nameToLowercaseKey(const char *name)
163187
return key;
164188
}
165189

190+
//-------------------------------------------------------------------------------------------------
191+
NameKeyType NameKeyGenerator::nameToKeyImpl(const AsciiString& name)
192+
{
193+
const UnsignedInt hash = calcHashForString(name.str()) % SOCKET_COUNT;
194+
195+
// do we have it already?
196+
const Bucket *b;
197+
for (b = m_sockets[hash]; b; b = b->m_nextInSocket)
198+
{
199+
if (name.compare(b->m_nameString) == 0)
200+
return b->m_key;
201+
}
202+
203+
// nope, guess not. let's allocate it.
204+
return createNameKey(hash, name);
205+
}
206+
207+
//-------------------------------------------------------------------------------------------------
208+
NameKeyType NameKeyGenerator::nameToLowercaseKeyImpl(const AsciiString& name)
209+
{
210+
const UnsignedInt hash = calcHashForLowercaseString(name.str()) % SOCKET_COUNT;
211+
212+
// do we have it already?
213+
const Bucket *b;
214+
for (b = m_sockets[hash]; b; b = b->m_nextInSocket)
215+
{
216+
if (name.compareNoCase(b->m_nameString) == 0)
217+
return b->m_key;
218+
}
219+
220+
// nope, guess not. let's allocate it.
221+
return createNameKey(hash, name);
222+
}
223+
166224
//-------------------------------------------------------------------------------------------------
167225
NameKeyType NameKeyGenerator::nameToKeyImpl(const char* name)
168226
{

0 commit comments

Comments
 (0)