Skip to content

Commit a69c912

Browse files
authored
perf(ini): Decrease cost of INI::isDeclarationOfType by around 10% (#1498)
1 parent 0a77180 commit a69c912

File tree

2 files changed

+42
-86
lines changed
  • GeneralsMD/Code/GameEngine/Source/Common/INI
  • Generals/Code/GameEngine/Source/Common/INI

2 files changed

+42
-86
lines changed

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

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,61 +1867,39 @@ void INI::parseDeathTypeFlags(INI* ini, void* /*instance*/, void* store, const v
18671867
// both blockType and blockName are case insensitive
18681868
Bool INI::isDeclarationOfType( AsciiString blockType, AsciiString blockName, char *bufferToCheck )
18691869
{
1870-
Bool retVal = true;
1871-
if (!bufferToCheck || blockType.isEmpty() || blockName.isEmpty()) {
1870+
if (!bufferToCheck || blockType.isEmpty() || blockName.isEmpty())
18721871
return false;
1873-
}
1874-
// DO NOT RETURN EARLY FROM THIS FUNCTION. (beyond this point)
1875-
// we have to restore the bufferToCheck to its previous state before returning, so
1876-
// it is important to get through all the checks.
18771872

1878-
char restoreChar;
1879-
char *tempBuff = bufferToCheck;
1880-
int blockTypeLength = blockType.getLength();
1881-
int blockNameLength = blockName.getLength();
1873+
const char* tempBuff = bufferToCheck;
18821874

1883-
while (isspace(*tempBuff)) {
1875+
while (isspace(*tempBuff))
18841876
++tempBuff;
1885-
}
18861877

1887-
if (strlen(tempBuff) > blockTypeLength) {
1888-
restoreChar = tempBuff[blockTypeLength];
1889-
tempBuff[blockTypeLength] = 0;
1878+
const int blockTypeLength = blockType.getLength();
1879+
if (strnicmp(tempBuff, blockType.str(), blockTypeLength) != 0)
1880+
return false;
18901881

1891-
if (stricmp(blockType.str(), tempBuff) != 0) {
1892-
retVal = false;
1893-
}
1882+
tempBuff += blockTypeLength;
18941883

1895-
tempBuff[blockTypeLength] = restoreChar;
1896-
tempBuff = tempBuff + blockTypeLength;
1897-
} else {
1898-
retVal = false;
1899-
}
1884+
if (!isspace(*tempBuff++))
1885+
return false;
19001886

1901-
while (isspace(*tempBuff)) {
1887+
while (isspace(*tempBuff))
19021888
++tempBuff;
1903-
}
1904-
1905-
if (strlen(tempBuff) > blockNameLength) {
1906-
restoreChar = tempBuff[blockNameLength];
1907-
tempBuff[blockNameLength] = 0;
19081889

1909-
if (stricmp(blockName.str(), tempBuff) != 0) {
1910-
retVal = false;
1911-
}
1890+
const int blockNameLength = blockName.getLength();
1891+
if (strnicmp(tempBuff, blockName.str(), blockNameLength) != 0)
1892+
return false;
19121893

1913-
tempBuff[blockNameLength] = restoreChar;
1914-
tempBuff = tempBuff + blockNameLength;
1915-
} else {
1916-
retVal = false;
1917-
}
1894+
tempBuff += blockNameLength;
19181895

1919-
while (strlen(tempBuff)) {
1920-
retVal = retVal && isspace(tempBuff[0]);
1896+
while (isspace(*tempBuff))
19211897
++tempBuff;
1922-
}
19231898

1924-
return retVal;
1899+
if (*tempBuff != '\0')
1900+
return false;
1901+
1902+
return true;
19251903
}
19261904

19271905
//-------------------------------------------------------------------------------------------------
@@ -1962,8 +1940,8 @@ Bool INI::isEndOfBlock( char *bufferToCheck )
19621940
retVal = false;
19631941
}
19641942

1965-
while (strlen(tempBuff)) {
1966-
retVal = retVal && isspace(tempBuff[0]);
1943+
while (*tempBuff && retVal) {
1944+
retVal = isspace(*tempBuff);
19671945
++tempBuff;
19681946
}
19691947

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

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,61 +1872,39 @@ void INI::parseDeathTypeFlags(INI* ini, void* /*instance*/, void* store, const v
18721872
// both blockType and blockName are case insensitive
18731873
Bool INI::isDeclarationOfType( AsciiString blockType, AsciiString blockName, char *bufferToCheck )
18741874
{
1875-
Bool retVal = true;
1876-
if (!bufferToCheck || blockType.isEmpty() || blockName.isEmpty()) {
1875+
if (!bufferToCheck || blockType.isEmpty() || blockName.isEmpty())
18771876
return false;
1878-
}
1879-
// DO NOT RETURN EARLY FROM THIS FUNCTION. (beyond this point)
1880-
// we have to restore the bufferToCheck to its previous state before returning, so
1881-
// it is important to get through all the checks.
18821877

1883-
char restoreChar;
1884-
char *tempBuff = bufferToCheck;
1885-
int blockTypeLength = blockType.getLength();
1886-
int blockNameLength = blockName.getLength();
1878+
const char* tempBuff = bufferToCheck;
18871879

1888-
while (isspace(*tempBuff)) {
1880+
while (isspace(*tempBuff))
18891881
++tempBuff;
1890-
}
18911882

1892-
if (strlen(tempBuff) > blockTypeLength) {
1893-
restoreChar = tempBuff[blockTypeLength];
1894-
tempBuff[blockTypeLength] = 0;
1883+
const int blockTypeLength = blockType.getLength();
1884+
if (strnicmp(tempBuff, blockType.str(), blockTypeLength) != 0)
1885+
return false;
18951886

1896-
if (stricmp(blockType.str(), tempBuff) != 0) {
1897-
retVal = false;
1898-
}
1887+
tempBuff += blockTypeLength;
18991888

1900-
tempBuff[blockTypeLength] = restoreChar;
1901-
tempBuff = tempBuff + blockTypeLength;
1902-
} else {
1903-
retVal = false;
1904-
}
1889+
if (!isspace(*tempBuff++))
1890+
return false;
19051891

1906-
while (isspace(*tempBuff)) {
1892+
while (isspace(*tempBuff))
19071893
++tempBuff;
1908-
}
1909-
1910-
if (strlen(tempBuff) > blockNameLength) {
1911-
restoreChar = tempBuff[blockNameLength];
1912-
tempBuff[blockNameLength] = 0;
19131894

1914-
if (stricmp(blockName.str(), tempBuff) != 0) {
1915-
retVal = false;
1916-
}
1895+
const int blockNameLength = blockName.getLength();
1896+
if (strnicmp(tempBuff, blockName.str(), blockNameLength) != 0)
1897+
return false;
19171898

1918-
tempBuff[blockNameLength] = restoreChar;
1919-
tempBuff = tempBuff + blockNameLength;
1920-
} else {
1921-
retVal = false;
1922-
}
1899+
tempBuff += blockNameLength;
19231900

1924-
while (strlen(tempBuff)) {
1925-
retVal = retVal && isspace(tempBuff[0]);
1901+
while (isspace(*tempBuff))
19261902
++tempBuff;
1927-
}
19281903

1929-
return retVal;
1904+
if (*tempBuff != '\0')
1905+
return false;
1906+
1907+
return true;
19301908
}
19311909

19321910
//-------------------------------------------------------------------------------------------------
@@ -1967,8 +1945,8 @@ Bool INI::isEndOfBlock( char *bufferToCheck )
19671945
retVal = false;
19681946
}
19691947

1970-
while (strlen(tempBuff)) {
1971-
retVal = retVal && isspace(tempBuff[0]);
1948+
while (*tempBuff && retVal) {
1949+
retVal = isspace(*tempBuff);
19721950
++tempBuff;
19731951
}
19741952

0 commit comments

Comments
 (0)