Skip to content

Commit 4bd189c

Browse files
authored
Refactorization: Faster calculation of CRC32 by avoiding to create a temporary string. Use proper types in checksum functions. (#4180)
Merged from LCppC
1 parent 898a3a2 commit 4bd189c

4 files changed

Lines changed: 16 additions & 18 deletions

File tree

lib/preprocessor.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,30 +1017,28 @@ static const std::uint32_t crc32Table[] = {
10171017
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
10181018
};
10191019

1020-
static std::uint32_t crc32(const std::string &data)
1020+
static void crc32(const std::string &data, uint32_t& crc)
10211021
{
1022-
std::uint32_t crc = ~0U;
10231022
for (char c : data) {
10241023
crc = crc32Table[(crc ^ (unsigned char)c) & 0xFF] ^ (crc >> 8);
10251024
}
1026-
return crc ^ ~0U;
10271025
}
10281026

1029-
unsigned int Preprocessor::calculateChecksum(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const
1027+
uint32_t Preprocessor::calculateChecksum(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const
10301028
{
1031-
std::ostringstream ostr;
1032-
ostr << toolinfo << '\n';
1029+
std::uint32_t crc = ~0U;
1030+
crc32(toolinfo, crc);
10331031
for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) {
10341032
if (!tok->comment)
1035-
ostr << tok->str();
1033+
crc32(tok->str(), crc);
10361034
}
10371035
for (std::map<std::string, simplecpp::TokenList *>::const_iterator it = mTokenLists.begin(); it != mTokenLists.end(); ++it) {
10381036
for (const simplecpp::Token *tok = it->second->cfront(); tok; tok = tok->next) {
10391037
if (!tok->comment)
1040-
ostr << tok->str();
1038+
crc32(tok->str(), crc);
10411039
}
10421040
}
1043-
return crc32(ostr.str());
1041+
return crc ^ ~0U;
10441042
}
10451043

10461044
void Preprocessor::simplifyPragmaAsm(simplecpp::TokenList *tokenList) const

lib/preprocessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class CPPCHECKLIB Preprocessor {
170170
* @param toolinfo Arbitrary extra toolinfo
171171
* @return CRC32 checksum
172172
*/
173-
unsigned int calculateChecksum(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const;
173+
uint32_t calculateChecksum(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const;
174174

175175
void simplifyPragmaAsm(simplecpp::TokenList *tokenList) const;
176176

lib/tokenlist.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -460,20 +460,20 @@ void TokenList::createTokens(simplecpp::TokenList&& tokenList)
460460

461461
//---------------------------------------------------------------------------
462462

463-
unsigned long long TokenList::calculateChecksum() const
463+
uint64_t TokenList::calculateChecksum() const
464464
{
465-
unsigned long long checksum = 0;
465+
uint64_t checksum = 0;
466466
for (const Token* tok = front(); tok; tok = tok->next()) {
467-
const unsigned int subchecksum1 = tok->flags() + tok->varId() + tok->tokType();
468-
unsigned int subchecksum2 = 0;
467+
const uint32_t subchecksum1 = tok->flags() + tok->varId() + tok->tokType();
468+
uint32_t subchecksum2 = 0;
469469
for (char i : tok->str())
470-
subchecksum2 += (unsigned int)i;
470+
subchecksum2 += (uint32_t)i;
471471
if (!tok->originalName().empty()) {
472472
for (char i : tok->originalName())
473-
subchecksum2 += (unsigned int) i;
473+
subchecksum2 += (uint32_t)i;
474474
}
475475

476-
checksum ^= ((static_cast<unsigned long long>(subchecksum1) << 32) | subchecksum2);
476+
checksum ^= ((static_cast<uint64_t>(subchecksum1) << 32) | subchecksum2);
477477

478478
const bool bit1 = (checksum & 1) != 0;
479479
checksum >>= 1;

lib/tokenlist.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class CPPCHECKLIB TokenList {
156156
* Calculates a 64-bit checksum of the token list used to compare
157157
* multiple token lists with each other as quickly as possible.
158158
*/
159-
unsigned long long calculateChecksum() const;
159+
uint64_t calculateChecksum() const;
160160

161161
/**
162162
* Create abstract syntax tree.

0 commit comments

Comments
 (0)