-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid memory allocations #67
Comments
Even worse: Score-P copys the C-String again 😉 . Moreover, if you are worried about the performance, #5 should be addressed first. Approx 85% of the code are written in python. Each function enter or exit causes a lot of python action, see
I think I'll reconsider this issue once #5 is fixed. However, I'll leave it open in the meantime. If you think it is necessary to fix this earlier, you are welcome to do a PR respecting the above-stated concerns. Best, Andreas |
I see nothing wrong with using what the language offers. C-Strings are part of C++ and there are C++ standard library functions for dealing with them i.e. the entire header (which happen to be the same available in C but in
Sure. I've just seen this and wanted to document it so it doesn't get forgotten. |
The actual solution seems to be string_view: https://en.cppreference.com/w/cpp/string/basic_string_view together with a However, this would require C++17. Best, Andreas |
Not really:
So you'll at least pay for iterating over the string once to determine the size. So only keeping the C-String until you need a Edit: Additionally there is no |
but a |
... which is not guaranteed to contain a NULL terminated string. |
Which is never guaranteed with an array. But as it is only a view to a NUL terminated string, thats not an issue. BTW more fun is this:
then you'll get the length for free. Moreover, it's more flexible. Best, |
Er what? A
So you want to store your C-String in a struct storing a pointer and size, then pass that to a function, in that function expect the pointer to be a C-String and ignore the size using that struct effectively as a convoluted way of transferring a C-String. Before this gets more spammy I'll stop. Important info is in first post and #67 (comment). |
Technical remark:The code for
And the code for
So |
The same applies to filenames on UNIX, (see https://unix.stackexchange.com/questions/38055/utf-8-filenames ). So we could avoid this as well. |
My proposal would be to use a simple, slim wrapper around a C-String:
This is very similar to This then allows accepting the Python values with known size and passing them to functions (like region_begin) that currently take If you agree with this design I'll create a PR with an implementation of this proposal |
Sounds reasonable. Please go ahead. Best, Andreas |
For some numbers: Replacing the
Top is with C-Strings for 100k, 300k and 500k iterations of the simplefunc benchmark (min, max, mean and median over 51 invocations) and bottom is current master using So there is some effect although not much. The small case is even slower for some reason but I think this is an artifact from the small runtime. I'd expect the effect to be more visible when memory pressure is higher especially as the benchmark uses only 1 function hence the memory allocator might return always the last used chunk which is faster than "real-world" use cases of random length functions |
After merging #105 you might want to try again, and use |
As the tracer should keep its own impact as low as possible, keeping an eye out for unnecessary memory allocations is vital.
Example:
scorep_binding_python/src/scorep.cpp
Line 23 in f9fbd7d
scorep_binding_python/src/scorep.cpp
Line 167 in f9fbd7d
module
andfile_name
are initially NULL-terminated C-Stringsstd::string
s very likely allocation heap memory and copying the contentsfile_name
is then never used as astd::string
but converted back to a C-String:scorep_binding_python/src/scorep.cpp
Line 33 in f9fbd7d
module
only a substring (prefix) is used (BTW: usingsubstr
instead of the ctor would likely read easier) Using resize instead would avoid an allocation and copy and using the find and substr on the original C-String would make it even cheaperI'm aware this is a Python tracer so relative performance impact is not as large as for e.g. C++ programs but especially allocations can have a noticeable impact. So as un-C++ it is, I'd recommend using raw C-Strings for those 2 and similar for other functions
The text was updated successfully, but these errors were encountered: