rhg: more efficient `HgPath::join`
authorArseniy Alekseyev <aalekseyev@janestreet.com>
Tue, 26 Oct 2021 19:47:30 +0100
changeset 48311 6d69e83e6b6e
parent 48310 229f5ee1a08a
child 48315 a44bb185f6bd
child 48318 557cfa049a7b
rhg: more efficient `HgPath::join` 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 `HgPathBuf::push` is chosen by analogy to `PathBuf::push`. Differential Revision: https://phab.mercurial-scm.org/D11721
rust/hg-core/src/filepatterns.rs
rust/hg-core/src/matchers.rs
rust/hg-core/src/utils/hg_path.rs
--- a/rust/hg-core/src/filepatterns.rs	Tue Nov 09 15:25:38 2021 +0100
+++ b/rust/hg-core/src/filepatterns.rs	Tue Oct 26 19:47:30 2021 +0100
@@ -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)
             })?,
--- a/rust/hg-core/src/matchers.rs	Tue Nov 09 15:25:38 2021 +0100
+++ b/rust/hg-core/src/matchers.rs	Tue Oct 26 19:47:30 2021 +0100
@@ -391,8 +391,7 @@
         } = ignore_pattern;
         match syntax {
             PatternSyntax::RootGlob | PatternSyntax::Glob => {
-                let mut root = vec![];
-
+                let mut root = HgPathBuf::new();
                 for p in pattern.split(|c| *c == b'/') {
                     if p.iter().any(|c| match *c {
                         b'[' | b'{' | b'*' | b'?' => true,
@@ -400,11 +399,9 @@
                     }) {
                         break;
                     }
-                    root.push(HgPathBuf::from_bytes(p));
+                    root.push(HgPathBuf::from_bytes(p).as_ref());
                 }
-                let buf =
-                    root.iter().fold(HgPathBuf::new(), |acc, r| acc.join(r));
-                roots.push(buf);
+                roots.push(root);
             }
             PatternSyntax::Path | PatternSyntax::RelPath => {
                 let pat = HgPath::new(if pattern == b"." {
--- a/rust/hg-core/src/utils/hg_path.rs	Tue Nov 09 15:25:38 2021 +0100
+++ b/rust/hg-core/src/utils/hg_path.rs	Tue Oct 26 19:47:30 2021 +0100
@@ -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 {