From 23afb8fbd89453b1b18c597744fd8f1e3edb7c7f Mon Sep 17 00:00:00 2001 From: Osyotr Date: Fri, 13 Sep 2024 02:18:59 +0300 Subject: [PATCH 1/2] Don't recreate stream and locale in loop Using `std::locale::classic()` instead of creating a new `std::locale` object on each iteration of the loop gives a massive performance boost. Moving `std::stringstream` out of the loop further improves performance. --- IfcPlusPlus/src/ifcpp/writer/WriterSTEP.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/IfcPlusPlus/src/ifcpp/writer/WriterSTEP.cpp b/IfcPlusPlus/src/ifcpp/writer/WriterSTEP.cpp index dca2d8472..ab33db9af 100644 --- a/IfcPlusPlus/src/ifcpp/writer/WriterSTEP.cpp +++ b/IfcPlusPlus/src/ifcpp/writer/WriterSTEP.cpp @@ -39,7 +39,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OU void WriterSTEP::writeModelToStream(std::stringstream& stream, shared_ptr model) { //imbue C locale to always use dots as decimal separator - stream.imbue(std::locale("C")); + stream.imbue(std::locale::classic()); const std::string& file_header_str = model->getFileHeader(); if(file_header_str.size() == 0) @@ -70,6 +70,8 @@ void WriterSTEP::writeModelToStream(std::stringstream& stream, shared_ptr counter = 0; size_t numEntities = entityDataStrings.size(); + std::stringstream tmpStream; + tmpStream.imbue(std::locale::classic()); FOR_EACH_LOOP entityDataStrings.begin(), entityDataStrings.end(), [&, this](std::tuple, std::string>& entityDataForOutput) { shared_ptr obj = std::get<1>(entityDataForOutput); if (obj.use_count() < 2) @@ -80,8 +82,6 @@ void WriterSTEP::writeModelToStream(std::stringstream& stream, shared_ptr(entityDataForOutput) = tmpStream.str(); + tmpStream.str(std::string()); counter.fetch_add(1); int currentCount = counter.load(); From 89ac57a74ed617ae76c629948bcf4c5c291ac5f8 Mon Sep 17 00:00:00 2001 From: Osyotr Date: Fri, 13 Sep 2024 13:45:10 +0300 Subject: [PATCH 2/2] Don't move stream out of the loop To prevent race condition --- IfcPlusPlus/src/ifcpp/writer/WriterSTEP.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/IfcPlusPlus/src/ifcpp/writer/WriterSTEP.cpp b/IfcPlusPlus/src/ifcpp/writer/WriterSTEP.cpp index ab33db9af..cc6136b3f 100644 --- a/IfcPlusPlus/src/ifcpp/writer/WriterSTEP.cpp +++ b/IfcPlusPlus/src/ifcpp/writer/WriterSTEP.cpp @@ -70,8 +70,6 @@ void WriterSTEP::writeModelToStream(std::stringstream& stream, shared_ptr counter = 0; size_t numEntities = entityDataStrings.size(); - std::stringstream tmpStream; - tmpStream.imbue(std::locale::classic()); FOR_EACH_LOOP entityDataStrings.begin(), entityDataStrings.end(), [&, this](std::tuple, std::string>& entityDataForOutput) { shared_ptr obj = std::get<1>(entityDataForOutput); if (obj.use_count() < 2) @@ -82,6 +80,8 @@ void WriterSTEP::writeModelToStream(std::stringstream& stream, shared_ptr(entityDataForOutput) = tmpStream.str(); - tmpStream.str(std::string()); counter.fetch_add(1); int currentCount = counter.load();