Skip to content

Commit a0316b9

Browse files
committed
perf(namekey): Remove all superfluous AsciiString allocations for name key lookups
1 parent 0351e5f commit a0316b9

File tree

34 files changed

+189
-93
lines changed

34 files changed

+189
-93
lines changed

GeneralsMD/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

GeneralsMD/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);
@@ -118,6 +118,8 @@ class NameKeyGenerator : public SubsystemInterface
118118
Bool addReservedKey();
119119
#endif
120120

121+
NameKeyType nameToKeyImpl(const AsciiString& name);
122+
NameKeyType nameToLowercaseKeyImpl(const AsciiString& name);
121123
NameKeyType nameToKeyImpl(const char* name);
122124
NameKeyType nameToLowercaseKeyImpl(const char *name);
123125
NameKeyType createNameKey(UnsignedInt hash, const AsciiString& name);

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

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

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

GeneralsMD/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 ///////////////////////////////////////////////////////////////////////////////////////

GeneralsMD/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
{

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,8 @@ class WeaponStore : public SubsystemInterface
839839
/**
840840
Find the WeaponTemplate with the given name. If no such WeaponTemplate exists, return null.
841841
*/
842-
const WeaponTemplate *findWeaponTemplate(AsciiString name) const;
842+
const WeaponTemplate *findWeaponTemplate(const AsciiString& name) const;
843+
const WeaponTemplate *findWeaponTemplate(const char* name) const;
843844
const WeaponTemplate *findWeaponTemplateByNameKey( NameKeyType key ) const { return findWeaponTemplatePrivate( key ); }
844845

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

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

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

274274
//-------------------------------------------------------------------------------------------------
275-
const DamageFX *DamageFXStore::findDamageFX(AsciiString name) const
275+
const DamageFX *DamageFXStore::findDamageFX(NameKeyType namekey) const
276276
{
277-
NameKeyType namekey = TheNameKeyGenerator->nameToKey(name);
278-
DamageFXMap::const_iterator it = m_dfxmap.find(namekey);
279-
if (it == m_dfxmap.end())
277+
DamageFXMap::const_iterator it = m_dfxmap.find(namekey);
278+
if (it == m_dfxmap.end())
280279
{
281280
return NULL;
282281
}
@@ -286,6 +285,18 @@ const DamageFX *DamageFXStore::findDamageFX(AsciiString name) const
286285
}
287286
}
288287

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

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

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

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

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

13821382
typedef const UpgradeTemplate* ConstUpgradeTemplatePtr;

GeneralsMD/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

GeneralsMD/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)