This is an automated email from the ASF dual-hosted git repository.

guangmingchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 6ef4158c use standard alg to optimze comparator (#2959)
6ef4158c is described below

commit 6ef4158cc6b36bf6a831416d265e081f1e995051
Author: none <zhangqion...@users.noreply.github.com>
AuthorDate: Sun Apr 27 10:01:26 2025 +0800

    use standard alg to optimze comparator (#2959)
    
    Co-authored-by: qiongyu.zhang <qiongyu.zh...@momenta.ai>
---
 src/brpc/restful.cpp | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

diff --git a/src/brpc/restful.cpp b/src/brpc/restful.cpp
index 32554e82..36b31e9d 100644
--- a/src/brpc/restful.cpp
+++ b/src/brpc/restful.cpp
@@ -314,29 +314,22 @@ void RestfulMap::ClearMethods() {
 struct CompareItemInPathList {
     bool operator()(const RestfulMethodProperty* e1,
                     const RestfulMethodProperty* e2) const {
-        const int rc1 = e1->path.prefix.compare(e2->path.prefix);
+        const RestfulMethodPath& path1 = e1->path;
+        const RestfulMethodPath& path2 = e2->path;
+        const int rc1 = path1.prefix.compare(path2.prefix);
         if (rc1 != 0) {
             return rc1 < 0;
         }
         // /A/*/B is put before /A/B so that we try exact patterns first
         // (the matching is in reversed order)
-        if (e1->path.has_wildcard != e2->path.has_wildcard) {
-            return e1->path.has_wildcard > e2->path.has_wildcard;
+        if (path1.has_wildcard != path2.has_wildcard) {
+            return path1.has_wildcard > path2.has_wildcard;
         }
         // Compare postfix from back to front.
-        // TODO: Optimize this.
-        std::string::const_reverse_iterator it1 = e1->path.postfix.rbegin();
-        std::string::const_reverse_iterator it2 = e2->path.postfix.rbegin();
-        while (it1 != e1->path.postfix.rend() &&
-               it2 != e2->path.postfix.rend()) {
-            if (*it1 != *it2) {
-                return (*it1 < *it2);
-            }
-            ++it1;
-            ++it2;
-        }
-        return (it1 == e1->path.postfix.rend())
-            > (it2 == e2->path.postfix.rend());
+        const bool postfix_result = std::lexicographical_compare(
+            path1.postfix.rbegin(), path1.postfix.rend(),
+            path2.postfix.rbegin(), path2.postfix.rend());
+        return postfix_result;
     }
 };
 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to