Skip to content

Commit f338e55

Browse files
authored
Clean up serialization mapper to use much less code (sstsimulator#1501)
1 parent ccbd214 commit f338e55

File tree

7 files changed

+46
-122
lines changed

7 files changed

+46
-122
lines changed

src/sst/core/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ sst_core_sources = \
231231
serialization/impl/unpacker.cc \
232232
serialization/impl/ser_shared_ptr_tracker.cc \
233233
serialization/statics.cc \
234-
serialization/impl/mapper.cc \
235234
serialization/impl/serialize_array.cc \
236235
serialization/impl/serialize_trivial.cc \
237236
serialization/impl/serialize_shared_ptr.cc \

src/sst/core/serialization/impl/mapper.cc

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/sst/core/serialization/impl/mapper.h

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,56 @@
1717
"The header file sst/core/serialization/impl/mapper.h should not be directly included as it is not part of the stable public API. The file is included in sst/core/serialization/serializer.h"
1818
#endif
1919

20+
#include "sst/core/serialization/objectMap.h"
21+
2022
#include <cstdint>
21-
#include <map>
23+
#include <deque>
2224
#include <string>
23-
#include <vector>
24-
25-
namespace SST::Core::Serialization {
26-
27-
class ObjectMap;
25+
#include <unordered_map>
2826

29-
namespace pvt {
27+
namespace SST::Core::Serialization::pvt {
3028

3129
class ser_mapper
3230
{
33-
std::map<uintptr_t, uintptr_t> pointer_map;
34-
std::vector<ObjectMap*> obj_;
35-
bool next_item_read_only = false;
36-
int indent = 0;
31+
std::unordered_map<uintptr_t, uintptr_t> pointer_map_;
32+
std::deque<ObjectMap*> obj_;
3733

3834
public:
39-
ser_mapper() = default;
40-
4135
explicit ser_mapper(ObjectMap* object) :
4236
obj_ { object }
4337
{}
4438

45-
void map_primitive(const std::string& name, ObjectMap* map);
46-
void map_container(const std::string& name, ObjectMap* map);
47-
void map_existing_object(const std::string& name, ObjectMap* map);
48-
void map_hierarchy_start(const std::string& name, ObjectMap* map);
49-
void map_hierarchy_end();
50-
void setNextObjectReadOnly() { next_item_read_only = true; }
51-
void report_object_map(ObjectMap* ptr);
52-
ObjectMap* check_pointer_map(uintptr_t ptr);
53-
};
54-
55-
} // namespace pvt
56-
} // namespace SST::Core::Serialization
39+
void map_primitive(const std::string& name, ObjectMap* map) { obj_.back()->addVariable(name, map); }
40+
41+
void map_container(const std::string& name, ObjectMap* map) { obj_.back()->addVariable(name, map); }
42+
43+
void map_existing_object(const std::string& name, ObjectMap* map)
44+
{
45+
map->incRefCount();
46+
obj_.back()->addVariable(name, map);
47+
}
48+
49+
void map_hierarchy_start(const std::string& name, ObjectMap* map)
50+
{
51+
obj_.back()->addVariable(name, map);
52+
obj_.push_back(map);
53+
}
54+
55+
void map_hierarchy_end() { obj_.pop_back(); }
56+
57+
void report_object_map(ObjectMap* ptr)
58+
{
59+
pointer_map_.insert_or_assign(reinterpret_cast<uintptr_t>(ptr->getAddr()), reinterpret_cast<uintptr_t>(ptr));
60+
}
61+
62+
ObjectMap* check_pointer_map(uintptr_t ptr)
63+
{
64+
auto it = pointer_map_.find(ptr);
65+
return it != pointer_map_.end() ? reinterpret_cast<ObjectMap*>(it->second) : nullptr;
66+
}
67+
68+
}; // class ser_wrapper
69+
70+
} // namespace SST::Core::Serialization::pvt
5771

5872
#endif // SST_CORE_SERIALIZATION_IMPL_MAPPER_H

src/sst/core/serialization/impl/serialize_string.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ class serialize_impl<T, std::enable_if_t<std::is_same_v<std::remove_pointer_t<T>
7474
const auto& sPtr = get_ptr(str);
7575
const auto mode = ser.mode();
7676
if ( mode == serializer::MAP ) {
77-
if ( options & SerOption::map_read_only ) {
78-
ser.mapper().setNextObjectReadOnly();
79-
}
80-
ser.mapper().map_primitive(ser.getMapName(), new ObjectMapString(sPtr));
77+
ObjectMap* obj_map = new ObjectMapString(sPtr);
78+
if ( SerOption::is_set(options, SerOption::map_read_only) ) obj_map->setReadOnly();
79+
ser.mapper().map_primitive(ser.getMapName(), obj_map);
8180
}
8281
else {
8382
if constexpr ( std::is_pointer_v<T> ) {

src/sst/core/serialization/impl/serialize_trivial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class serialize_impl<T,
6464
obj_map = new ObjectMapFundamental<std::remove_pointer_t<T>>(t);
6565
else
6666
obj_map = new ObjectMapFundamental<T>(&t);
67-
if ( SerOption::is_set(options, SerOption::map_read_only) ) ser.mapper().setNextObjectReadOnly();
67+
if ( SerOption::is_set(options, SerOption::map_read_only) ) obj_map->setReadOnly();
6868
ser.mapper().map_primitive(ser.getMapName(), obj_map);
6969
}
7070
else {

src/sst/core/timeLord.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,7 @@ serialize_impl<TimeConverter>::operator()(TimeConverter& s, serializer& ser, ser
279279
case serializer::MAP:
280280
{
281281
ObjectMap* obj_map = new ObjectMapFundamental<TimeConverter>(&s);
282-
if ( options & SerOption::map_read_only ) {
283-
ser.mapper().setNextObjectReadOnly();
284-
}
282+
if ( SerOption::is_set(options, SerOption::map_read_only) ) obj_map->setReadOnly();
285283
ser.mapper().map_primitive(ser.getMapName(), obj_map);
286284
break;
287285
}
@@ -322,9 +320,7 @@ serialize_impl<TimeConverter*>::operator()(TimeConverter*& s, serializer& ser, s
322320
case serializer::MAP:
323321
{
324322
ObjectMap* obj_map = new ObjectMapFundamental<TimeConverter*>(&s);
325-
if ( options & SerOption::map_read_only ) {
326-
ser.mapper().setNextObjectReadOnly();
327-
}
323+
if ( SerOption::is_set(options, SerOption::map_read_only) ) obj_map->setReadOnly();
328324
ser.mapper().map_primitive(ser.getMapName(), obj_map);
329325
break;
330326
}

src/sst/core/unitAlgebra.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,7 @@ class serialize_impl<UnitAlgebra>
451451
case serializer::MAP:
452452
{
453453
ObjectMap* obj_map = new ObjectMapFundamental<UnitAlgebra>(&ua);
454-
if ( options & SerOption::map_read_only ) {
455-
ser.mapper().setNextObjectReadOnly();
456-
}
454+
if ( SerOption::is_set(options, SerOption::map_read_only) ) obj_map->setReadOnly();
457455
ser.mapper().map_primitive(ser.getMapName(), obj_map);
458456
break;
459457
}

0 commit comments

Comments
 (0)