Patchwork D11721: rhg: more efficient `HgPath::join`

login
register
mail settings
Submitter phabricator
Date Oct. 26, 2021, 6:49 p.m.
Message ID <differential-rev-PHID-DREV-t4motl7litoss45zil3o-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50046/
State Superseded
Headers show

Comments

phabricator - Oct. 26, 2021, 6:49 p.m.
aalekseyev created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This commit makes `HgPath::join` slightly more efficient
  by avoiding one copy.
  
  It also avoids a particularly inefficient (quadratic) use of
  `HgPath::join` by using a new mutating function `HgPathBuf::push` instead.
  
  The name for `HgPath::push` is chosen by analogy to `PathBuf::push`.

REPOSITORY
  rHG Mercurial

BRANCH
  stable

REVISION DETAIL
  https://phab.mercurial-scm.org/D11721

AFFECTED FILES
  rust/hg-core/src/filepatterns.rs
  rust/hg-core/src/matchers.rs
  rust/hg-core/src/utils/hg_path.rs

CHANGE DETAILS




To: aalekseyev, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/hg-core/src/utils/hg_path.rs b/rust/hg-core/src/utils/hg_path.rs
--- a/rust/hg-core/src/utils/hg_path.rs
+++ b/rust/hg-core/src/utils/hg_path.rs
@@ -220,13 +220,11 @@ 
             ),
         }
     }
-    pub fn join<T: ?Sized + AsRef<Self>>(&self, other: &T) -> HgPathBuf {
-        let mut inner = self.inner.to_owned();
-        if !inner.is_empty() && inner.last() != Some(&b'/') {
-            inner.push(b'/');
-        }
-        inner.extend(other.as_ref().bytes());
-        HgPathBuf::from_bytes(&inner)
+
+    pub fn join(&self, path: &HgPath) -> HgPathBuf {
+        let mut buf = self.to_owned();
+        buf.push(path);
+        buf
     }
 
     pub fn components(&self) -> impl Iterator<Item = &HgPath> {
@@ -405,7 +403,15 @@ 
     pub fn new() -> Self {
         Default::default()
     }
-    pub fn push(&mut self, byte: u8) {
+
+    pub fn push<T: ?Sized + AsRef<HgPath>>(&mut self, other: &T) -> () {
+        if !self.inner.is_empty() && self.inner.last() != Some(&b'/') {
+            self.inner.push(b'/');
+        }
+        self.inner.extend(other.as_ref().bytes())
+    }
+
+    pub fn push_byte(&mut self, byte: u8) {
         self.inner.push(byte);
     }
     pub fn from_bytes(s: &[u8]) -> HgPathBuf {
diff --git a/rust/hg-core/src/matchers.rs b/rust/hg-core/src/matchers.rs
--- a/rust/hg-core/src/matchers.rs
+++ b/rust/hg-core/src/matchers.rs
@@ -402,8 +402,8 @@ 
                     }
                     root.push(HgPathBuf::from_bytes(p));
                 }
-                let buf =
-                    root.iter().fold(HgPathBuf::new(), |acc, r| acc.join(r));
+                let mut buf = HgPathBuf::new();
+                root.iter().fold((), |(), r| buf.push(r));
                 roots.push(buf);
             }
             PatternSyntax::Path | PatternSyntax::RelPath => {
diff --git a/rust/hg-core/src/filepatterns.rs b/rust/hg-core/src/filepatterns.rs
--- a/rust/hg-core/src/filepatterns.rs
+++ b/rust/hg-core/src/filepatterns.rs
@@ -536,7 +536,7 @@ 
         Ok(Self {
             prefix: path_to_hg_path_buf(prefix).and_then(|mut p| {
                 if !p.is_empty() {
-                    p.push(b'/');
+                    p.push_byte(b'/');
                 }
                 Ok(p)
             })?,