Skip to content

[SyntaxModel] Improve the performance of searching URLs in comments #5214

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

Merged
merged 4 commits into from
Oct 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions lib/IDE/SyntaxModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class ModelASTWalker : public ASTWalker {
unsigned BufferID;
std::vector<StructureElement> SubStructureStack;
SourceLoc LastLoc;
static const std::regex &getURLRegex(unsigned Index);
static const std::regex &getURLRegex(StringRef Protocol);
static const std::regex &getDocCommentRegex(unsigned Index);

Optional<SyntaxNode> parseFieldNode(StringRef Text, StringRef OrigText,
Expand Down Expand Up @@ -366,13 +366,27 @@ class ModelASTWalker : public ASTWalker {
}
};

const std::regex &ModelASTWalker::getURLRegex(unsigned Index) {
const std::regex &ModelASTWalker::getURLRegex(StringRef Pro) {
static const std::regex Regexes[3] = {
std::regex{ RegexStrURL, std::regex::ECMAScript | std::regex::nosubs },
std::regex{ RegexStrMailURL, std::regex::ECMAScript | std::regex::nosubs },
std::regex{ RegexStrRadarURL, std::regex::ECMAScript | std::regex::nosubs }
};
return Regexes[Index];

static const auto MailToPosition = std::find(URLProtocols.begin(),
URLProtocols.end(),
"mailto");
static const auto RadarPosition = std::find(URLProtocols.begin(),
URLProtocols.end(),
"radar");
auto Found = std::find(URLProtocols.begin(), URLProtocols.end(), Pro);
assert(Found != URLProtocols.end() && "bad protocol name");
if (Found < MailToPosition)
return Regexes[0];
else if (Found < RadarPosition)
return Regexes[1];
else
return Regexes[2];
}

const std::regex &ModelASTWalker::getDocCommentRegex(unsigned Index) {
Expand Down Expand Up @@ -1369,24 +1383,32 @@ bool ModelASTWalker::findUrlStartingLoc(StringRef Text,
static const auto MailToPosition = std::find(URLProtocols.begin(),
URLProtocols.end(),
"mailto");
static const auto RadarPosition = std::find(URLProtocols.begin(),
URLProtocols.end(),
"radar");
auto Index = Text.find(":");
if (Index == StringRef::npos)
return false;

for (auto It = URLProtocols.begin(); It != URLProtocols.end(); ++ It) {
if (Index >= It->size() &&
Text.substr(Index - It->size(), It->size()) == *It) {
Start = Index - It->size();
if (It < MailToPosition)
Regex = getURLRegex(0);
else if (It < RadarPosition)
Regex = getURLRegex(1);
else
Regex = getURLRegex(2);
return true;
auto Lookback = [Text](unsigned Index, StringRef Name) {
return Index >= Name.size() &&
Text.substr(Index - Name.size(), Name.size()) == Name;
};

auto HasSlash = Text.substr(Index).startswith("://");

if (HasSlash) {
for (auto It = URLProtocols.begin(); It < URLProtocols.end(); ++ It) {
if (Lookback(Index, *It)) {
Regex = getURLRegex(*It);
Start = Index - It->size();
return true;
}
}
} else {
for (auto It = MailToPosition; It < URLProtocols.end(); ++ It) {
if (Lookback(Index, *It)) {
Regex = getURLRegex(*It);
Start = Index - It->size();
return true;
}
}
}
#endif
Expand Down