Skip to content

Commit 9ffab11

Browse files
ttsugriyfacebook-github-bot
authored andcommitted
Avoid vector overhead in isSupportedScheme.
Summary: 1) using vector is inefficient since it's heap-allocated and has indirection 2) using string is inefficient for similar reasons 3) using static for supported schemes is inefficient because it involves atomics or mutex to ensure thread safety 4) using folly::StringPiece provides more ergonomic APIs for `contains` and `starts_with` Reviewed By: hanidamlaj Differential Revision: D74557531 fbshipit-source-id: 2364bdbe9cd5aa6b292ad66a4d2ab461f6f890b8
1 parent b749e04 commit 9ffab11

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

proxygen/lib/utils/ParseURL.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,19 @@ static bool validateScheme(folly::StringPiece url) {
5252
scheme.begin(), scheme.end(), [](auto _) { return std::isalpha(_); });
5353
}
5454

55-
bool ParseURL::isSupportedScheme(const std::string& location) {
56-
static const std::vector<std::string> supportedSchemes = {"http", "https"};
57-
std::size_t containsScheme = location.find("://");
58-
if (containsScheme == std::string::npos) {
55+
bool ParseURL::isSupportedScheme(folly::StringPiece location) {
56+
static constexpr std::array<folly::StringPiece, 2> kSupportedSchemes{"http",
57+
"https"};
58+
if (!location.contains("://")) {
5959
// Location doesn't contain a scheme, so use the one from the original URL
6060
return true;
6161
}
6262

63-
for (const std::string& scheme : supportedSchemes) {
64-
// Check to see whether or not it is a supported scheme
65-
if (location.compare(0, scheme.length(), scheme) == 0) {
66-
return true;
67-
}
68-
}
69-
return false;
63+
return std::any_of(kSupportedSchemes.begin(),
64+
kSupportedSchemes.end(),
65+
[location](folly::StringPiece scheme) {
66+
return location.starts_with(scheme);
67+
});
7068
}
7169

7270
folly::Optional<std::string> ParseURL::getRedirectDestination(

proxygen/lib/utils/ParseURL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ParseURL {
4747
return ParseURL(urlVal, strict);
4848
}
4949

50-
static bool isSupportedScheme(const std::string& location);
50+
static bool isSupportedScheme(folly::StringPiece location);
5151

5252
static folly::Optional<std::string> getRedirectDestination(
5353
folly::StringPiece url,

0 commit comments

Comments
 (0)