Skip to content

Commit

Permalink
Absolutize paths after remapping (#91)
Browse files Browse the repository at this point in the history
The index store library requires paths being written are absolute. If we
don't absolutize them then it does. Unfortunately its logic for that
computes the PWD for every single path, which can amount to ~5% of the
total time spent in index-import.

Signed-off-by: Keith Smiley <keithbsmiley@gmail.com>
  • Loading branch information
keith authored Jun 27, 2023
1 parent 4c31054 commit 431d4e5
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions index-import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,33 @@ static cl::opt<bool> UndoRulesSwiftRenames(

struct Remapper {
public:
explicit Remapper() { llvm::sys::fs::current_path(this->pwd); }

std::string remap(const llvm::StringRef input) const {
if (input.empty()) {
return input.str();
}

std::string input_str = input.str();
std::optional<StringRef> remapped_str = std::nullopt;
for (const auto &remap : this->_remaps) {
const auto &pattern = remap.first;
const auto &replacement = remap.second;
if (re2::RE2::Replace(&input_str, *pattern, replacement)) {
return path::remove_leading_dotslash(StringRef(input_str)).str();
remapped_str = path::remove_leading_dotslash(StringRef(input_str));
break;
}
}

// No patterns matched, return the input unaltered.
return path::remove_leading_dotslash(input).str();
StringRef output_str =
remapped_str.value_or(path::remove_leading_dotslash(input));
if (path::is_absolute(output_str)) {
return output_str.str();
}

SmallString<128> absolute(output_str);
llvm::sys::fs::make_absolute(this->pwd, absolute);
return absolute.str().str();
}

void addRemap(std::shared_ptr<re2::RE2> &pattern,
Expand All @@ -79,6 +94,9 @@ struct Remapper {
}

std::vector<std::pair<std::shared_ptr<re2::RE2>, std::string>> _remaps;

private:
SmallString<128> pwd;
};

// Helper for working with index::writer::OpaqueModule. Provides the following:
Expand Down

0 comments on commit 431d4e5

Please sign in to comment.