Skip to content

Commit

Permalink
utils: Cache get_real_path to improve performance, make API more const
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKagstrom committed Jun 7, 2014
1 parent d4ca613 commit 4f4a051
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/include/utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ std::list<std::string> split_string(const std::string &s, const char *delims);

std::string trim_string(const std::string &strIn);

std::string get_real_path(const std::string &path);
const std::string &get_real_path(const std::string &path);

bool string_is_integer(const std::string &str, unsigned base = 0);

Expand Down
13 changes: 9 additions & 4 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,19 +412,24 @@ std::string trim_string(const std::string &strIn)
return str;
}

std::string get_real_path(const std::string &path)
// Cache for ::realpath - it's apparently one of the reasons why kcov is slow
static std::unordered_map<std::string, std::string> realPathCache;
const std::string &get_real_path(const std::string &path)
{
const auto &it = realPathCache.find(path);
if (it != realPathCache.end())
return it->second;

char *rp = NULL;

rp = ::realpath(path.c_str(), nullptr);

if (!rp)
return path;

std::string out(rp);
free(rp);
realPathCache[path] = rp;

return out;
return realPathCache[path];
}


Expand Down

0 comments on commit 4f4a051

Please sign in to comment.