github-actions[bot] commented on code in PR #62531:
URL: https://github.com/apache/doris/pull/62531#discussion_r3086979082
##########
be/src/util/url_parser.cpp:
##########
@@ -62,73 +62,70 @@ bool UrlParser::parse_url(const StringRef& url, UrlPart
part, StringRef* result)
}
// Positioned to first char after '://'.
- StringRef protocol_end = trimmed_url.substring(protocol_pos +
_s_protocol.size);
+ const char* after_protocol = trimmed_url.data + protocol_pos +
_s_protocol.size;
+ const char* url_end = trimmed_url.data + trimmed_url.size;
switch (part) {
case AUTHORITY: {
- // Find first '/'.
- int32_t end_pos = _s_slash_search.search(&protocol_end);
- *result = protocol_end.substring(0, end_pos);
+ // Authority ends at first '/'.
+ const char* slash =
+ static_cast<const char*>(memchr(after_protocol, '/', url_end -
after_protocol));
+ *result = StringRef(after_protocol, (slash ? slash : url_end) -
after_protocol);
break;
}
case FILE:
case PATH: {
- // Find first '/'.
- int32_t start_pos = _s_slash_search.search(&protocol_end);
-
- if (start_pos < 0) {
+ // Use memchr for fast '/' lookup, then sequential search for
terminators.
+ const char* slash_pos =
+ static_cast<const char*>(memchr(after_protocol, '/', url_end -
after_protocol));
+ if (!slash_pos) {
// Return empty string. This is what Hive does.
return true;
}
-
- StringRef path_start = protocol_end.substring(start_pos);
- int32_t end_pos;
-
+ const size_t remaining = url_end - slash_pos;
if (part == FILE) {
- // End _s_at '#'.
- end_pos = _s_hash_search.search(&path_start);
+ // FILE ends at '#' only.
+ const char* hash_pos = static_cast<const char*>(memchr(slash_pos,
'#', remaining));
+ *result = StringRef(slash_pos, (hash_pos ? hash_pos : url_end) -
slash_pos);
} else {
- // End string _s_at next '?' or '#'.
- end_pos = _s_question_search.search(&path_start);
-
- if (end_pos < 0) {
- // No '?' was found, look for '#'.
- end_pos = _s_hash_search.search(&path_start);
+ // PATH ends at '?' or '#', whichever comes first.
+ const char* q_pos = static_cast<const char*>(memchr(slash_pos,
'?', remaining));
+ if (q_pos) {
+ *result = StringRef(slash_pos, q_pos - slash_pos);
+ } else {
+ const char* h_pos = static_cast<const char*>(memchr(slash_pos,
'#', remaining));
+ *result = StringRef(slash_pos, (h_pos ? h_pos : url_end) -
slash_pos);
}
}
-
- *result = path_start.substring(0, end_pos);
break;
}
case HOST: {
- // Find '@'.
- int32_t start_pos = _s_at_search.search(&protocol_end);
-
- if (start_pos < 0) {
- // No '@' was found, i.e., no user:pass info was given, start
after _s_protocol.
- start_pos = 0;
- } else {
- // Skip '@'.
- start_pos += _s_at.size;
- }
-
- StringRef host_start = protocol_end.substring(start_pos);
- // Find first '?'.
- int32_t query_start_pos = _s_question_search.search(&host_start);
- if (query_start_pos > 0) {
- host_start = host_start.substring(0, query_start_pos);
+ // Single pass: track '@' (userinfo separator), stop at ':' '/' '?'.
+ const char* pos = after_protocol;
+ const char* start_of_host = after_protocol;
+ const char* colon_pos = nullptr;
Review Comment:
This changes Doris' shipped `parse_url()` semantics, not just its speed. The
previous implementation kept scanning past the authority, so inputs like
`parse_url('http://example.com/path:80', 'HOST')` returned `example.com/path`,
`parse_url('http://example.com/path:80', 'PORT')` returned `80`, and
`parse_url('http://example.com/path@foo', 'HOST')` returned `foo`. The new loop
stops at the first `/` or `?`, so those now become `example.com`, `NULL`, and
`example.com`. If that compatibility change is intentional, please call it out
explicitly and add regression coverage for the affected HOST/PORT cases;
otherwise this rewrite needs to preserve the current contract.
##########
be/benchmark/benchmark_parse_url.hpp:
##########
@@ -0,0 +1,345 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <benchmark/benchmark.h>
+
+#include <cstring>
+#include <string>
+#include <vector>
+
+#include "core/string_ref.h"
+#include "util/string_search.hpp"
+#include "util/url_parser.h"
+
+namespace doris {
+
+// ===========================================================================
+// Old implementation (baseline) — copied from the original url_parser.cpp
+// before optimization, used for A/B comparison.
+// ===========================================================================
+class OldUrlParser {
+public:
+ static bool parse_url(const StringRef& url, UrlParser::UrlPart part,
StringRef* result) {
+ result->data = nullptr;
+ result->size = 0;
+ StringRef trimmed_url = url.trim();
Review Comment:
This is no longer a true pre-change baseline: `OldUrlParser::parse_url()`
still calls `StringRef::trim()`, and this PR also optimized `trim()` itself.
That means the A/B benchmark here measures the old parser body against the new
parser body while both already benefit from the new trim fast path, so it
understates or obscures the full before/after delta described in the PR body.
Please either keep an actual old `trim()` in the baseline or benchmark the trim
change separately.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]