Skip to content

Commit 67a573e

Browse files
authored
Issue 42 (#44)
Replace `malloc` and `sprintf` with a static `std::string`. This should improve the performance a little bit, as the memory is not reallocated for each enter and exit.
1 parent 596be48 commit 67a573e

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

src/scorep.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct region_handle
2020
static std::unordered_map<std::string, region_handle> regions;
2121
static std::unordered_map<std::string, region_handle> rewind_regions;
2222

23-
void region_begin(std::string region_name, std::string module, std::string file_name,
23+
void region_begin(const std::string& region_name, std::string module, std::string file_name,
2424
std::uint64_t line_number)
2525
{
2626
auto pair = regions.emplace(make_pair(region_name, region_handle()));
@@ -36,7 +36,7 @@ void region_begin(std::string region_name, std::string module, std::string file_
3636
SCOREP_User_RegionEnter(handle.value);
3737
}
3838

39-
void region_end(std::string region_name)
39+
void region_end(const std::string& region_name)
4040
{
4141
auto& handle = regions[region_name];
4242
SCOREP_User_RegionEnd(handle.value);
@@ -122,6 +122,9 @@ extern "C"
122122
return Py_None;
123123
}
124124

125+
/** This code is not thread save. However, this does not matter as the python GIL is not
126+
* released.
127+
*/
125128
static PyObject* region_begin(PyObject* self, PyObject* args)
126129
{
127130
const char* module;
@@ -134,16 +137,20 @@ extern "C"
134137

135138
if (scorep_python::filter_modules.find(module) == scorep_python::filter_modules.end())
136139
{
137-
char* region = (char*)malloc(strlen(module) + strlen(region_name) + 2);
138-
sprintf(region, "%s:%s", module, region_name);
140+
static std::string region = "";
141+
region = module;
142+
region += ":";
143+
region += region_name;
139144
scorep::region_begin(region, module, file_name, line_number);
140-
free(region);
141145
}
142146

143147
Py_INCREF(Py_None);
144148
return Py_None;
145149
}
146150

151+
/** This code is not thread save. However, this does not matter as the python GIL is not
152+
* released.
153+
*/
147154
static PyObject* region_end(PyObject* self, PyObject* args)
148155
{
149156
const char* module;
@@ -154,10 +161,11 @@ extern "C"
154161

155162
if (scorep_python::filter_modules.find(module) == scorep_python::filter_modules.end())
156163
{
157-
char* region = (char*)malloc(strlen(module) + strlen(region_name) + 2);
158-
sprintf(region, "%s:%s", module, region_name);
164+
static std::string region = "";
165+
region = module;
166+
region += ":";
167+
region += region_name;
159168
scorep::region_end(region);
160-
free(region);
161169
}
162170

163171
Py_INCREF(Py_None);

0 commit comments

Comments
 (0)