From cef6b6997a9dec81a43d37201987de5b5f2458f0 Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Tue, 12 Nov 2024 22:36:19 -0500 Subject: [PATCH] apply R suggestions to pypkg and cpp code --- pypkg/redatamlib/readers/FuzzyVariableParser.cpp | 14 ++++++++++++-- src/readers/FuzzyVariableParser.cpp | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pypkg/redatamlib/readers/FuzzyVariableParser.cpp b/pypkg/redatamlib/readers/FuzzyVariableParser.cpp index 1ecf902..812a152 100644 --- a/pypkg/redatamlib/readers/FuzzyVariableParser.cpp +++ b/pypkg/redatamlib/readers/FuzzyVariableParser.cpp @@ -17,8 +17,14 @@ FuzzyVariableParser::FuzzyVariableParser(ByteArrayReader reader, void FuzzyVariableParser::ParseAllVariables(vector &entities) { vector> searchBounds = GetSearchBounds(entities); - size_t numThreads = std::thread::hardware_concurrency(); - numThreads = std::min(entities.size(), numThreads); + // R-devel suggestion: Default to using all available hardware concurrency + size_t maxThreads = std::thread::hardware_concurrency(); + + size_t numThreads = std::min(entities.size(), maxThreads); + + if (numThreads == 0) { + numThreads = 1; + } size_t chunkSize = entities.size() / numThreads; @@ -41,6 +47,10 @@ vector> FuzzyVariableParser::GetSearchBounds(vector entities) { vector> ret; + if (entities.empty()) { + return ret; + } + for (size_t i = 0; i < entities.size() - 1; ++i) { ret.push_back( {entities[i].GetBounds().second, entities[i + 1].GetBounds().first}); diff --git a/src/readers/FuzzyVariableParser.cpp b/src/readers/FuzzyVariableParser.cpp index 09efdf3..b693b8f 100644 --- a/src/readers/FuzzyVariableParser.cpp +++ b/src/readers/FuzzyVariableParser.cpp @@ -17,8 +17,14 @@ FuzzyVariableParser::FuzzyVariableParser(ByteArrayReader reader, void FuzzyVariableParser::ParseAllVariables(vector &entities) { vector> searchBounds = GetSearchBounds(entities); - size_t numThreads = std::thread::hardware_concurrency(); - numThreads = std::min(entities.size(), numThreads); + // R-devel suggestion: Default to using all available hardware concurrency + size_t maxThreads = std::thread::hardware_concurrency(); + + size_t numThreads = std::min(entities.size(), maxThreads); + + if (numThreads == 0) { + numThreads = 1; + } size_t chunkSize = entities.size() / numThreads; @@ -41,6 +47,10 @@ vector> FuzzyVariableParser::GetSearchBounds(vector entities) { vector> ret; + if (entities.empty()) { + return ret; + } + for (size_t i = 0; i < entities.size() - 1; ++i) { ret.push_back( {entities[i].GetBounds().second, entities[i + 1].GetBounds().first});