From 2e8bcaf558c1d96cd1bd88a612f1cbc119aee495 Mon Sep 17 00:00:00 2001 From: Norbert Podhorszki Date: Thu, 26 Oct 2023 08:28:10 -0400 Subject: [PATCH] Replace LookupWriterRec's linear search on RecList with an unordered_map. For 250k variables, time goes from 21sec to ~1sec in WSL. The order of entries in RecList was not necessary for the serializer to work correctly. --- .../toolkit/format/bp5/BP5Serializer.cpp | 29 ++++++++----------- .../adios2/toolkit/format/bp5/BP5Serializer.h | 4 ++- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/source/adios2/toolkit/format/bp5/BP5Serializer.cpp b/source/adios2/toolkit/format/bp5/BP5Serializer.cpp index 06d2176856..b88535c099 100644 --- a/source/adios2/toolkit/format/bp5/BP5Serializer.cpp +++ b/source/adios2/toolkit/format/bp5/BP5Serializer.cpp @@ -31,14 +31,14 @@ namespace format BP5Serializer::BP5Serializer() { Init(); } BP5Serializer::~BP5Serializer() { - if (Info.RecList) + if (!Info.RecMap.empty()) { - for (int i = 0; i < Info.RecCount; i++) + for (auto &rec : Info.RecMap) { - if (Info.RecList[i].OperatorType) - free(Info.RecList[i].OperatorType); + if (rec.second.OperatorType) + free(rec.second.OperatorType); } - free(Info.RecList); + Info.RecMap.clear(); } if (Info.MetaFieldCount) free_FMfield_list(Info.MetaFields); @@ -61,7 +61,6 @@ void BP5Serializer::Init() // Re-init Info to zero Info = FFSWriterMarshalBase(); Info.RecCount = 0; - Info.RecList = (BP5Serializer::BP5WriterRec)malloc(sizeof(Info.RecList[0])); Info.MetaFieldCount = 0; Info.MetaFields = NULL; Info.LocalFMContext = create_local_FMcontext(); @@ -80,14 +79,11 @@ void BP5Serializer::Init() } BP5Serializer::BP5WriterRec BP5Serializer::LookupWriterRec(void *Key) { - for (int i = 0; i < Info.RecCount; i++) + auto it = Info.RecMap.find(Key); + if (it != Info.RecMap.end()) { - if (Info.RecList[i].Key == Key) - { - return &Info.RecList[i]; - } + return const_cast(&(it->second)); } - return NULL; } @@ -442,9 +438,8 @@ BP5Serializer::CreateWriterRec(void *Variable, const char *Name, DataType Type, size_t ElemSize, size_t DimCount) { core::VariableBase *VB = static_cast(Variable); - Info.RecList = (BP5WriterRec)realloc( - Info.RecList, (Info.RecCount + 1) * sizeof(Info.RecList[0])); - BP5WriterRec Rec = &Info.RecList[Info.RecCount]; + auto obj = Info.RecMap.insert(std::make_pair(Variable, _BP5WriterRec())); + BP5WriterRec Rec = &obj.first->second; if (Type == DataType::String) ElemSize = sizeof(char *); Rec->Key = Variable; @@ -1134,9 +1129,9 @@ BufferV *BP5Serializer::ReinitStepData(BufferV *DataBuffer, void BP5Serializer::CollectFinalShapeValues() { - for (int i = 0; i < Info.RecCount; i++) + for (auto it : Info.RecMap) { - BP5WriterRec Rec = &Info.RecList[i]; + BP5WriterRec Rec = &it.second; if (Rec->Shape == ShapeID::GlobalArray) { core::VariableBase *VB = diff --git a/source/adios2/toolkit/format/bp5/BP5Serializer.h b/source/adios2/toolkit/format/bp5/BP5Serializer.h index 71ec480e88..fc4f43c2ed 100644 --- a/source/adios2/toolkit/format/bp5/BP5Serializer.h +++ b/source/adios2/toolkit/format/bp5/BP5Serializer.h @@ -21,6 +21,8 @@ #pragma warning(disable : 4250) #endif +#include + namespace adios2 { namespace format @@ -160,7 +162,6 @@ class BP5Serializer : virtual public BP5Base struct FFSWriterMarshalBase { int RecCount = 0; - BP5WriterRec RecList = NULL; FMContext LocalFMContext = {0}; int MetaFieldCount = 0; FMFieldList MetaFields = NULL; @@ -170,6 +171,7 @@ class BP5Serializer : virtual public BP5Base FMFormat AttributeFormat = NULL; void *AttributeData = NULL; int AttributeSize = 0; + std::unordered_map RecMap; }; FMFormat GenericAttributeFormat = NULL;