Skip to content

Commit

Permalink
Replace LookupWriterRec's linear search on RecList with an unordered_…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
pnorbert committed Oct 29, 2023
1 parent e999646 commit 2e8bcaf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
29 changes: 12 additions & 17 deletions source/adios2/toolkit/format/bp5/BP5Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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<BP5WriterRec>(&(it->second));
}

return NULL;
}

Expand Down Expand Up @@ -442,9 +438,8 @@ BP5Serializer::CreateWriterRec(void *Variable, const char *Name, DataType Type,
size_t ElemSize, size_t DimCount)
{
core::VariableBase *VB = static_cast<core::VariableBase *>(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;
Expand Down Expand Up @@ -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 =
Expand Down
4 changes: 3 additions & 1 deletion source/adios2/toolkit/format/bp5/BP5Serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#pragma warning(disable : 4250)
#endif

#include <unordered_map>

namespace adios2
{
namespace format
Expand Down Expand Up @@ -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;
Expand All @@ -170,6 +171,7 @@ class BP5Serializer : virtual public BP5Base
FMFormat AttributeFormat = NULL;
void *AttributeData = NULL;
int AttributeSize = 0;
std::unordered_map<void *, _BP5WriterRec> RecMap;
};

FMFormat GenericAttributeFormat = NULL;
Expand Down

0 comments on commit 2e8bcaf

Please sign in to comment.