Skip to content

Commit 0d42b9a

Browse files
authored
Avoid inplace construction if its not needed (#95)
1 parent 532373d commit 0d42b9a

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

src/scorepy/events.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,34 @@ namespace scorepy
99

1010
struct region_handle
1111
{
12-
region_handle() = default;
12+
constexpr region_handle() = default;
1313
~region_handle() = default;
14+
constexpr bool operator==(const region_handle& other)
15+
{
16+
return this->value == other.value;
17+
}
1418
SCOREP_User_RegionHandle value = SCOREP_USER_INVALID_REGION;
1519
};
1620

21+
constexpr region_handle uninitalised_region_handle = region_handle();
22+
1723
static std::unordered_map<std::string, region_handle> regions;
1824
static std::unordered_map<std::string, region_handle> rewind_regions;
1925

2026
void region_begin(const std::string& region_name, std::string module, std::string file_name,
2127
std::uint64_t line_number)
2228
{
23-
auto pair = regions.emplace(make_pair(region_name, region_handle()));
24-
bool inserted_new = pair.second;
25-
auto& handle = pair.first->second;
26-
if (inserted_new)
29+
auto& region_handle = regions[region_name];
30+
31+
if (region_handle == uninitalised_region_handle)
2732
{
28-
SCOREP_User_RegionInit(&handle.value, NULL, &SCOREP_User_LastFileHandle,
33+
SCOREP_User_RegionInit(&region_handle.value, NULL, &SCOREP_User_LastFileHandle,
2934
region_name.c_str(), SCOREP_USER_REGION_TYPE_FUNCTION,
3035
file_name.c_str(), line_number);
31-
SCOREP_User_RegionSetGroup(handle.value, std::string(module, 0, module.find('.')).c_str());
36+
SCOREP_User_RegionSetGroup(region_handle.value,
37+
std::string(module, 0, module.find('.')).c_str());
3238
}
33-
SCOREP_User_RegionEnter(handle.value);
39+
SCOREP_User_RegionEnter(region_handle.value);
3440
}
3541

3642
void region_end(const std::string& region_name)

0 commit comments

Comments
 (0)