Skip to content

Commit

Permalink
refactor: using optimized funcs find,strchr with char param, constexp…
Browse files Browse the repository at this point in the history
…r calc len out-of-loop
  • Loading branch information
GermanAizek committed Jan 1, 2025
1 parent c5d541d commit 669c8da
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions shared/offline_compiler/source/decoder/binary_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion shared/offline_compiler/source/multi_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ int MultiCommand::splitLineInSeparateArgs(std::vector<std::string> &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) {
Expand Down
2 changes: 1 addition & 1 deletion shared/offline_compiler/source/ocloc_fatbinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ int buildFatBinaryForTarget(int retVal, const std::vector<std::string> &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);
Expand Down
8 changes: 4 additions & 4 deletions shared/offline_compiler/source/offline_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ std::string formatNameVersionString(std::vector<NameVersionPair> 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("\"");
Expand Down Expand Up @@ -888,7 +888,7 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector<std::string> &

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");
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
10 changes: 5 additions & 5 deletions shared/source/helpers/product_config_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<uint32_t>(std::stoul(device));
Expand Down Expand Up @@ -212,7 +212,7 @@ std::vector<NEO::ConstStringRef> ProductConfigHelper::getAllProductAcronyms() {

PRODUCT_FAMILY ProductConfigHelper::getProductFamilyFromDeviceName(const std::string &device) const {
std::vector<DeviceAotInfo>::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));
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions shared/source/os_interface/linux/drm_neo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,17 @@ std::vector<std::unique_ptr<HwDeviceId>> Drm::discoverDevices(ExecutionEnvironme

do {
const char *renderDeviceSuffix = "-render";
constexpr size_t renderDevSufSize = std::char_traits<char>::length(renderDeviceSuffix);
constexpr size_t pciDevDirLen = std::char_traits<char>::length(Os::pciDevicesDirectory);
for (std::vector<std::string>::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
Expand Down

0 comments on commit 669c8da

Please sign in to comment.