Skip to content

Avoid memory allocations #67

Description

@Flamefire

As the tracer should keep its own impact as low as possible, keeping an eye out for unnecessary memory allocations is vital.

Example:

void region_begin(const std::string& region_name, std::string module, std::string file_name,
called from
scorep::region_begin(region, module, file_name, line_number);

  • module and file_name are initially NULL-terminated C-Strings
  • For calling the function they are converted to std::strings very likely allocation heap memory and copying the contents
  • both are only used in the first region enter, so in most cases the above cost is wasted
  • file_name is then never used as a std::string but converted back to a C-String:
    file_name.c_str(), line_number);
    So all work is wasted completely
  • From module only a substring (prefix) is used (BTW: using substr 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 cheaper

I'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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions