Skip to content

Commit 9501885

Browse files
Prevent unnecessary allocations in test util
We can use string_view's to prevent allocating a string every time get_folder_for_given_path is called.
1 parent 344e4e6 commit 9501885

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

tests/framework/util/folder_manager.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,18 @@ FileSystemManager::FileSystemManager()
187187
fs::Folder& FileSystemManager::get_folder(ManifestLocation location) noexcept { return folders.at(location); }
188188
fs::Folder const& FileSystemManager::get_folder(ManifestLocation location) const noexcept { return folders.at(location); }
189189

190-
Folder* FileSystemManager::get_folder_for_given_path(std::filesystem::path const& given_path) noexcept {
191-
auto normalized_path = given_path.lexically_normal();
190+
Folder* FileSystemManager::get_folder_for_given_path(std::string_view given_path) noexcept {
192191
for (auto const& [manifest_location, folder] : folders) {
193-
if (folder.location() == normalized_path) {
192+
if (folder.location() == given_path) {
194193
return &folders.at(manifest_location);
195194
}
196195
}
197-
if (redirected_paths.count(given_path.string()) > 0) {
198-
return &folders.at(redirected_paths.at(normalized_path.string()));
199-
}
200-
for (auto const& [redirected_path_str, redirected_location] : redirected_paths) {
201-
std::filesystem::path redirected_path = redirected_path_str;
196+
for (auto const& [redirected_path, redirected_location] : redirected_paths) {
197+
if (redirected_path == given_path) {
198+
return &folders.at(redirected_location);
199+
}
202200
const auto mismatch_pair =
203-
std::mismatch(normalized_path.begin(), normalized_path.end(), redirected_path.begin(), redirected_path.end());
201+
std::mismatch(given_path.begin(), given_path.end(), redirected_path.begin(), redirected_path.end());
204202
if (mismatch_pair.second == redirected_path.end()) return &folders.at(redirected_location);
205203
}
206204
return nullptr;

tests/framework/util/folder_manager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <filesystem>
3030
#include <string>
31+
#include <string_view>
3132
#include <unordered_map>
3233
#include <vector>
3334

@@ -87,7 +88,7 @@ class FileSystemManager {
8788
Folder const& get_folder(ManifestLocation location) const noexcept;
8889

8990
// Gets a pointer to the folder that given_path points to. This includes redirected paths as well as the exact path of folders
90-
Folder* get_folder_for_given_path(std::filesystem::path const& given_path) noexcept;
91+
Folder* get_folder_for_given_path(std::string_view given_path) noexcept;
9192

9293
bool is_folder_path(std::filesystem::path const& path) const noexcept;
9394

0 commit comments

Comments
 (0)