From f5e982cc07e2cbd3361462a2aa1e02eb7dbedfb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20MARTINEZ=20CAAMA=C3=91O?= Date: Fri, 21 Oct 2022 14:29:53 +0000 Subject: [PATCH] SWDEV-286150 - [NFC] Avoid copying string when calling HashString Change-Id: Ic2f4d469ca319f7d31814b3955430dbbfdad5bb2 --- rocclr/compiler/lib/utils/options.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/rocclr/compiler/lib/utils/options.cpp b/rocclr/compiler/lib/utils/options.cpp index 381a9bea37..c9620cd668 100644 --- a/rocclr/compiler/lib/utils/options.cpp +++ b/rocclr/compiler/lib/utils/options.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -1445,12 +1446,12 @@ Options::setPerBuildInfo( // HashString - Hash function for strings. // This is the Bernstein hash function. -static inline unsigned HashString(std::string Str, unsigned Result = 0) +static inline unsigned HashString(std::string::const_iterator it, std::string::const_iterator end) { - for (size_t i = 0, e = Str.size(); i != e; ++i) { - Result = Result * 33 + (unsigned char)Str[i]; - } - return Result; + auto Bernstein = [](unsigned Result, unsigned char c) { + return Result * 33 + c; + }; + return std::accumulate(it, end, 0U, Bernstein); } static inline std::string to_string(unsigned number) @@ -1526,13 +1527,11 @@ static std::string getValidDumpBaseName(const std::string &path, const std::stri return file; } - std::string truncName; // HashString returns unsigned, and the max lengh of unsighed is // 10 digits, anything over origDigits will be replaced with its hash value size_t origDigits = basename_max - 10 - ext.size(); - truncName = file.substr(0, origDigits); - std::string remainStr = file.substr(origDigits); - unsigned hashVal = HashString(remainStr); + std::string truncName = file.substr(0, origDigits); + unsigned hashVal = HashString(file.begin() + origDigits, file.end()); return truncName + to_string(hashVal); }