From 669c8daf1855e434a64cebfd94feb92033eb5807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Semenov=20Herman=20=28=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=93=D0=B5=D1=80=D0=BC=D0=B0=D0=BD=29?= Date: Wed, 1 Jan 2025 13:21:55 +0300 Subject: [PATCH] refactor: using optimized funcs find,strchr with char param, constexpr calc len out-of-loop --- .../offline_compiler/source/decoder/binary_decoder.cpp | 4 ++-- shared/offline_compiler/source/multi_command.cpp | 2 +- shared/offline_compiler/source/ocloc_fatbinary.cpp | 2 +- shared/offline_compiler/source/offline_compiler.cpp | 8 ++++---- .../device_binary_format/zebin/zeinfo_decoder.cpp | 2 +- shared/source/helpers/product_config_helper.cpp | 10 +++++----- shared/source/os_interface/linux/drm_neo.cpp | 6 ++++-- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/shared/offline_compiler/source/decoder/binary_decoder.cpp b/shared/offline_compiler/source/decoder/binary_decoder.cpp index ae74b978a2ad6..e8aeb29a820a8 100644 --- a/shared/offline_compiler/source/decoder/binary_decoder.cpp +++ b/shared/offline_compiler/source/decoder/binary_decoder.cpp @@ -237,7 +237,7 @@ void BinaryDecoder::parseTokens() { break; } else if (patchList[i].find("PATCH_TOKEN") == std::string::npos) { continue; - } else if (patchList[i].find("@") == std::string::npos) { + } else if (patchList[i].find('@') == std::string::npos) { continue; } @@ -254,7 +254,7 @@ void BinaryDecoder::parseTokens() { nameEndPos = patchList[i].find(',', nameStartPos); patchTokenPtr->name = patchList[i].substr(nameStartPos, nameEndPos - nameStartPos); - nameStartPos = patchList[i].find("@"); + nameStartPos = patchList[i].find('@'); nameEndPos = patchList[i].find('@', nameStartPos + 1); if (nameEndPos == std::string::npos) { continue; diff --git a/shared/offline_compiler/source/multi_command.cpp b/shared/offline_compiler/source/multi_command.cpp index c90c9b8919bb4..54158c4b8e6a7 100644 --- a/shared/offline_compiler/source/multi_command.cpp +++ b/shared/offline_compiler/source/multi_command.cpp @@ -189,7 +189,7 @@ int MultiCommand::splitLineInSeparateArgs(std::vector &qargs, const continue; } else { start = i; - end = commandsLine.find(" ", start); + end = commandsLine.find(' ', start); end = (end == std::string::npos) ? commandsLine.length() : end; } if (end == std::string::npos) { diff --git a/shared/offline_compiler/source/ocloc_fatbinary.cpp b/shared/offline_compiler/source/ocloc_fatbinary.cpp index 0ce7bdd73b1d4..d67b27c9ec5bc 100644 --- a/shared/offline_compiler/source/ocloc_fatbinary.cpp +++ b/shared/offline_compiler/source/ocloc_fatbinary.cpp @@ -306,7 +306,7 @@ int buildFatBinaryForTarget(int retVal, const std::vector &argsCopy } std::string entryName(""); - if (product.find(".") != std::string::npos) { + if (product.find('.') != std::string::npos) { entryName = product; } else { auto productConfig = argHelper->productConfigHelper->getProductConfigFromDeviceName(product); diff --git a/shared/offline_compiler/source/offline_compiler.cpp b/shared/offline_compiler/source/offline_compiler.cpp index 870db00f2f082..d99393fbe9745 100644 --- a/shared/offline_compiler/source/offline_compiler.cpp +++ b/shared/offline_compiler/source/offline_compiler.cpp @@ -247,7 +247,7 @@ std::string formatNameVersionString(std::vector extensions, boo for (const auto &ext : extensions) { formatedExtensions.push_back({}); auto it = formatedExtensions.rbegin(); - bool needsQuoutes = (nullptr != strstr(ext.name, " ")); + bool needsQuoutes = (nullptr != strchr(ext.name, ' ')); it->reserve(strnlen_s(ext.name, sizeof(ext.name)) + (needsQuoutes ? 2 : 0) + (needVersions ? 16 : 0)); if (needsQuoutes) { it->append("\""); @@ -888,7 +888,7 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector & if (options.empty()) { // try to read options from file if not provided by commandline - size_t extStart = inputFile.find_last_of("."); + size_t extStart = inputFile.find_last_of('.'); if (extStart != std::string::npos) { std::string oclocOptionsFileName = inputFile.substr(0, extStart); oclocOptionsFileName.append("_ocloc_options.txt"); @@ -1236,7 +1236,7 @@ std::string OfflineCompiler::parseBinAsCharArray(uint8_t *binary, size_t size, s std::string OfflineCompiler::getFileNameTrunk(std::string &filePath) { size_t slashPos = filePath.find_last_of("\\/", filePath.size()) + 1; - size_t extPos = filePath.find_last_of(".", filePath.size()); + size_t extPos = filePath.find_last_of('.', filePath.size()); if (extPos == std::string::npos) { extPos = filePath.size(); } @@ -1576,7 +1576,7 @@ void OfflineCompiler::writeOutAllFiles() { if (outputFile.empty()) { elfOutputFile = generateFilePath(outputDirectory, fileBase, ".bin"); } else { - size_t extPos = fileBase.find_last_of(".", fileBase.size()); + size_t extPos = fileBase.find_last_of('.', fileBase.size()); std::string fileExt = ".bin"; if (extPos != std::string::npos) { auto existingExt = fileBase.substr(extPos, fileBase.size()); diff --git a/shared/source/device_binary_format/zebin/zeinfo_decoder.cpp b/shared/source/device_binary_format/zebin/zeinfo_decoder.cpp index 8dcbfbb85d0f4..b83fdce36acf5 100644 --- a/shared/source/device_binary_format/zebin/zeinfo_decoder.cpp +++ b/shared/source/device_binary_format/zebin/zeinfo_decoder.cpp @@ -354,7 +354,7 @@ void populateKernelMiscInfo(KernelDescriptor &dst, KernelMiscArgInfos &kernelMis dstTypeTraits.typeQualifiers = KernelArgMetadata::parseTypeQualifiers(dstMetadata.typeQualifiers); dst.payloadMappings.explicitArgs.at(srcMetadata.index).getTraits() = std::move(dstTypeTraits); - dstMetadata.type = dstMetadata.type.substr(0U, dstMetadata.type.find(";")); + dstMetadata.type = dstMetadata.type.substr(0U, dstMetadata.type.find(';')); dst.explicitArgsExtendedMetadata.at(srcMetadata.index) = std::move(dstMetadata); } } diff --git a/shared/source/helpers/product_config_helper.cpp b/shared/source/helpers/product_config_helper.cpp index acb27d4a1bc0e..3df0564716e56 100644 --- a/shared/source/helpers/product_config_helper.cpp +++ b/shared/source/helpers/product_config_helper.cpp @@ -47,7 +47,7 @@ void ProductConfigHelper::adjustDeviceName(std::string &device) { device = device.substr(0, findCore); } - auto findUnderscore = device.find("_"); + auto findUnderscore = device.find('_'); if (findUnderscore != std::string::npos) { device.erase(std::remove(device.begin(), device.end(), '_'), device.end()); } @@ -146,7 +146,7 @@ bool ProductConfigHelper::isSupportedProductConfig(uint32_t config) const { AOT::PRODUCT_CONFIG ProductConfigHelper::getProductConfigFromDeviceName(const std::string &device) const { uint32_t config = AOT::UNKNOWN_ISA; - if (device.find(".") != std::string::npos) { + if (device.find('.') != std::string::npos) { config = getProductConfigFromVersionValue(device); } else if (std::all_of(device.begin(), device.end(), (::isdigit))) { config = static_cast(std::stoul(device)); @@ -212,7 +212,7 @@ std::vector ProductConfigHelper::getAllProductAcronyms() { PRODUCT_FAMILY ProductConfigHelper::getProductFamilyFromDeviceName(const std::string &device) const { std::vector::const_iterator it; - if (device.find(".") != std::string::npos) { + if (device.find('.') != std::string::npos) { it = std::find_if(deviceAotInfo.begin(), deviceAotInfo.end(), findProductConfig(getProductConfigFromVersionValue(device))); } else { it = std::find_if(deviceAotInfo.begin(), deviceAotInfo.end(), findAcronym(device)); @@ -271,13 +271,13 @@ int ProductConfigHelper::parseProductConfigFromString(const std::string &device, } uint32_t ProductConfigHelper::getProductConfigFromVersionValue(const std::string &device) { - auto majorPos = device.find("."); + auto majorPos = device.find('.'); auto major = parseProductConfigFromString(device, 0, majorPos); if (major == ConfigStatus::MismatchedValue || majorPos == std::string::npos) { return AOT::UNKNOWN_ISA; } - auto minorPos = device.find(".", ++majorPos); + auto minorPos = device.find('.', ++majorPos); auto minor = parseProductConfigFromString(device, majorPos, minorPos); if (minor == ConfigStatus::MismatchedValue || minorPos == std::string::npos) { diff --git a/shared/source/os_interface/linux/drm_neo.cpp b/shared/source/os_interface/linux/drm_neo.cpp index 90a9960951884..0c684956b4643 100644 --- a/shared/source/os_interface/linux/drm_neo.cpp +++ b/shared/source/os_interface/linux/drm_neo.cpp @@ -659,15 +659,17 @@ std::vector> Drm::discoverDevices(ExecutionEnvironme do { const char *renderDeviceSuffix = "-render"; + constexpr size_t renderDevSufSize = std::char_traits::length(renderDeviceSuffix); + constexpr size_t pciDevDirLen = std::char_traits::length(Os::pciDevicesDirectory); for (std::vector::iterator file = files.begin(); file != files.end(); ++file) { std::string_view devicePathView(file->c_str(), file->size()); - devicePathView = devicePathView.substr(strlen(Os::pciDevicesDirectory)); + devicePathView = devicePathView.substr(pciDevDirLen); auto rdsPos = devicePathView.rfind(renderDeviceSuffix); if (rdsPos == std::string::npos) { continue; } - if (rdsPos < devicePathView.size() - strlen(renderDeviceSuffix)) { + if (rdsPos < devicePathView.size() - renderDevSufSize) { continue; } // at least 'pci-0000:00:00.0' -> 16