Skip to content

Commit 7ec85af

Browse files
committed
Pass and move some strings as std::string arround
1 parent d5d3132 commit 7ec85af

7 files changed

Lines changed: 53 additions & 54 deletions

File tree

src/methods.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,20 @@ extern "C"
6969
return NULL;
7070
}
7171

72-
std::string_view module(module_cstr, module_len);
72+
std::string module(module_cstr, module_len);
7373
std::string_view function_name(function_name_cstr, function_name_len);
74-
std::string_view file_name(file_name_cstr, file_name_len);
75-
76-
std::string file_name_abs = scorepy::abspath(file_name);
74+
auto const file_name_abs =
75+
scorepy::abspath(std::string_view(file_name_cstr, file_name_len));
7776

7877
if (identifier == nullptr or identifier == Py_None)
7978
{
80-
scorepy::region_begin(function_name, module, file_name_abs, line_number);
79+
scorepy::region_begin(function_name, std::move(module), std::move(file_name_abs),
80+
line_number);
8181
}
8282
else
8383
{
84-
scorepy::region_begin(function_name, module, file_name_abs, line_number,
85-
reinterpret_cast<PyCodeObject*>(identifier));
84+
scorepy::region_begin(function_name, std::move(module), std::move(file_name_abs),
85+
line_number, reinterpret_cast<PyCodeObject*>(identifier));
8686
}
8787

8888
Py_RETURN_NONE;
@@ -124,16 +124,17 @@ extern "C"
124124
return NULL;
125125
}
126126

127-
std::string_view module(module_cstr, module_len);
127+
std::string module(module_cstr, module_len);
128128
std::string_view function_name(function_name_cstr, function_name_len);
129129

130130
if (identifier == nullptr or identifier == Py_None)
131131
{
132-
scorepy::region_end(function_name, module);
132+
scorepy::region_end(function_name, std::move(module));
133133
}
134134
else
135135
{
136-
scorepy::region_end(function_name, module, reinterpret_cast<PyCodeObject*>(identifier));
136+
scorepy::region_end(function_name, std::move(module),
137+
reinterpret_cast<PyCodeObject*>(identifier));
137138
}
138139

139140
Py_RETURN_NONE;

src/scorepy/cInstrumenter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ bool CInstrumenter::on_event(PyFrameObject& frame, int what, PyObject*)
124124
if (name.compare("_unsetprofile") != 0 && module_name.compare(0, 6, "scorep") != 0)
125125
{
126126
const int line_number = code->co_firstlineno;
127-
const std::string file_name = get_file_name(frame);
128-
region_begin(name, module_name, file_name, line_number, code);
127+
const auto file_name = get_file_name(frame);
128+
region_begin(name, std::move(module_name), std::move(file_name), line_number, code);
129129
}
130130
}
131131
Py_DECREF(code);
@@ -142,7 +142,7 @@ bool CInstrumenter::on_event(PyFrameObject& frame, int what, PyObject*)
142142
// TODO: Use string_view/CString comparison?
143143
if (name.compare("_unsetprofile") != 0 && module_name.compare(0, 6, "scorep") != 0)
144144
{
145-
region_end(name, module_name, code);
145+
region_end(name, std::move(module_name), code);
146146
}
147147
}
148148
Py_DECREF(code);

src/scorepy/events.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,45 @@ static compat::RegisterCodeDealloc register_dealloc(on_dealloc);
2828

2929
// Used for regions, that have an identifier, aka a code object id. (instrumenter regions and
3030
// some decorated regions)
31-
void region_begin(std::string_view function_name, std::string_view module,
32-
const std::string& file_name, const std::uint64_t line_number,
33-
compat::PyCodeObject* identifier)
31+
void region_begin(std::string_view function_name, std::string module, std::string file_name,
32+
const std::uint64_t line_number, compat::PyCodeObject* identifier)
3433
{
3534
region_handle& region = regions[identifier];
3635

3736
if (region == uninitialised_region_handle)
3837
{
39-
const auto region_name = make_region_name(module, function_name);
38+
const auto region_name = make_region_name(std::move(module), function_name);
4039
SCOREP_User_RegionInit(&region.value, NULL, NULL, region_name.c_str(),
4140
SCOREP_USER_REGION_TYPE_FUNCTION, file_name.c_str(), line_number);
4241

43-
SCOREP_User_RegionSetGroup(region.value, std::string(module, 0, module.find('.')).c_str());
42+
SCOREP_User_RegionSetGroup(region.value,
43+
std::string(region_name, 0, region_name.find('.')).c_str());
4444
}
4545
SCOREP_User_RegionEnter(region.value);
4646
}
4747

4848
// Used for regions, that only have a function name, a module, a file and a line number (user
4949
// regions)
50-
void region_begin(std::string_view function_name, std::string_view module,
51-
const std::string& file_name, const std::uint64_t line_number)
50+
void region_begin(std::string_view function_name, std::string module, std::string file_name,
51+
const std::uint64_t line_number)
5252
{
53-
const auto region_name = make_region_name(module, function_name);
53+
const auto region_name = make_region_name(std::move(module), function_name);
5454
region_handle& region = user_regions[region_name];
5555

5656
if (region == uninitialised_region_handle)
5757
{
5858
SCOREP_User_RegionInit(&region.value, NULL, NULL, region_name.c_str(),
5959
SCOREP_USER_REGION_TYPE_FUNCTION, file_name.c_str(), line_number);
6060

61-
SCOREP_User_RegionSetGroup(region.value, std::string(module, 0, module.find('.')).c_str());
61+
SCOREP_User_RegionSetGroup(region.value,
62+
std::string(region_name, 0, region_name.find('.')).c_str());
6263
}
6364
SCOREP_User_RegionEnter(region.value);
6465
}
6566

6667
// Used for regions, that have an identifier, aka a code object id. (instrumenter regions and
6768
// some decorated regions)
68-
void region_end(std::string_view function_name, std::string_view module,
69+
void region_end(std::string_view function_name, std::string module,
6970
compat::PyCodeObject* identifier)
7071
{
7172
const auto it_region = regions.find(identifier);
@@ -75,27 +76,27 @@ void region_end(std::string_view function_name, std::string_view module,
7576
}
7677
else
7778
{
78-
const auto region_name = make_region_name(module, function_name);
79-
region_end_error_handling(region_name);
79+
const auto region_name = make_region_name(std::move(module), function_name);
80+
region_end_error_handling(std::move(region_name));
8081
}
8182
}
8283

8384
// Used for regions, that only have a function name, a module (user regions)
84-
void region_end(std::string_view function_name, std::string_view module)
85+
void region_end(std::string_view function_name, std::string module)
8586
{
86-
const auto region_name = make_region_name(module, function_name);
87+
const auto region_name = make_region_name(std::move(module), function_name);
8788
auto it_region = user_regions.find(region_name);
8889
if (it_region != user_regions.end())
8990
{
9091
SCOREP_User_RegionEnd(it_region->second.value);
9192
}
9293
else
9394
{
94-
region_end_error_handling(region_name);
95+
region_end_error_handling(std::move(region_name));
9596
}
9697
}
9798

98-
void region_end_error_handling(const std::string& region_name)
99+
void region_end_error_handling(std::string region_name)
99100
{
100101
static region_handle error_region;
101102
static SCOREP_User_ParameterHandle scorep_param = SCOREP_USER_INVALID_PARAMETER;

src/scorepy/events.hpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ struct region_handle
3030
constexpr region_handle uninitialised_region_handle = region_handle();
3131

3232
/// Combine the arguments into a region name
33-
inline std::string make_region_name(std::string_view module_name, std::string_view name)
33+
inline std::string make_region_name(std::string module_name, std::string_view name)
3434
{
35-
std::string region;
36-
region += module_name;
37-
region += ":";
38-
region += name;
39-
return std::move(region);
35+
module_name += ':';
36+
module_name += name;
37+
return std::move(module_name);
4038
}
4139

4240
extern std::unordered_map<compat::PyCodeObject*, region_handle> regions;
@@ -58,11 +56,10 @@ inline bool try_region_begin(compat::PyCodeObject* identifier)
5856
}
5957
}
6058

61-
void region_begin(std::string_view function_name, std::string_view module,
62-
const std::string& file_name, const std::uint64_t line_number,
63-
compat::PyCodeObject* identifier);
64-
void region_begin(std::string_view function_name, std::string_view module,
65-
const std::string& file_name, const std::uint64_t line_number);
59+
void region_begin(std::string_view function_name, std::string module, std::string file_name,
60+
const std::uint64_t line_number, compat::PyCodeObject* identifier);
61+
void region_begin(std::string_view function_name, std::string module, std::string file_name,
62+
const std::uint64_t line_number);
6663

6764
/** tries to end a region. Return true on success
6865
*
@@ -81,11 +78,11 @@ inline bool try_region_end(compat::PyCodeObject* identifier)
8178
}
8279
}
8380

84-
void region_end(std::string_view function_name, std::string_view module,
81+
void region_end(std::string_view function_name, std::string module,
8582
compat::PyCodeObject* identifier);
86-
void region_end(std::string_view function_name, std::string_view module);
83+
void region_end(std::string_view function_name, std::string module);
8784

88-
void region_end_error_handling(const std::string& region_name);
85+
void region_end_error_handling(std::string region_name);
8986

9087
void rewind_begin(std::string region_name, std::string file_name, std::uint64_t line_number);
9188
void rewind_end(std::string region_name, bool value);

src/scorepy/pathUtils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static std::string getcwd()
2525
result.resize(result.find('\0', result.size() - chunk_size));
2626
else
2727
result.clear();
28-
return result;
28+
return std::move(result);
2929
}
3030

3131
void normalize_path(std::string& path)
@@ -80,7 +80,7 @@ std::string abspath(std::string_view input_path)
8080
result = getcwd();
8181
// On error exit
8282
if (result.empty())
83-
return result;
83+
return {};
8484
// Prepend CWD
8585
result.append(1, '/').append(input_path);
8686
}
@@ -89,6 +89,6 @@ std::string abspath(std::string_view input_path)
8989
result = input_path;
9090
}
9191
normalize_path(result);
92-
return result;
92+
return std::move(result);
9393
}
9494
} // namespace scorepy

src/scorepy/pythonHelpers.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77

88
namespace scorepy
99
{
10-
std::string_view get_module_name(PyFrameObject& frame)
10+
std::string get_module_name(PyFrameObject& frame)
1111
{
1212
PyObject* globals = PyFrame_GetGlobals(&frame);
1313
PyObject* module_name = PyDict_GetItemString(globals, "__name__");
1414
Py_DECREF(globals);
1515
if (module_name)
16-
return compat::get_string_as_utf_8(module_name);
16+
return std::move(std::string(compat::get_string_as_utf_8(module_name)));
1717

1818
// this is a NUMPY special situation, see NEP-18, and Score-P issue #63
1919
// TODO: Use string_view/C-String to avoid creating 2 std::strings
2020
PyCodeObject* code = PyFrame_GetCode(&frame);
2121
std::string_view filename = compat::get_string_as_utf_8(code->co_filename);
2222
Py_DECREF(code);
2323
if ((filename.size() > 0) && (filename == "<__array_function__ internals>"))
24-
return std::string_view("numpy.__array_function__");
24+
return std::move(std::string("numpy.__array_function__"));
2525
else
26-
return std::string_view("unkown");
26+
return std::move(std::string("unkown"));
2727
}
2828

2929
std::string get_file_name(PyFrameObject& frame)
@@ -33,9 +33,9 @@ std::string get_file_name(PyFrameObject& frame)
3333
Py_DECREF(code);
3434
if (filename == Py_None)
3535
{
36-
return "None";
36+
return std::move(std::string("None"));
3737
}
38-
const std::string full_file_name = abspath(compat::get_string_as_utf_8(filename));
39-
return !full_file_name.empty() ? full_file_name : "ErrorPath";
38+
const auto full_file_name = abspath(compat::get_string_as_utf_8(filename));
39+
return !full_file_name.empty() ? std::move(full_file_name) : "ErrorPath";
4040
}
4141
} // namespace scorepy

src/scorepy/pythonHelpers.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ auto cast_to_PyFunc(TFunc* func) -> detail::ReplaceArgsToPyObject_t<TFunc>*
7373

7474
/// Return the module name the frame belongs to.
7575
/// The pointer is valid for the lifetime of the frame
76-
std::string_view get_module_name(PyFrameObject& frame);
76+
std::string get_module_name(PyFrameObject& frame);
7777
/// Return the file name the frame belongs to
7878
std::string get_file_name(PyFrameObject& frame);
7979

0 commit comments

Comments
 (0)