Mercurial > public > mercurial-scm > hg
comparison rust/hg-core/src/utils/hg_path.rs @ 48311:6d69e83e6b6e
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
author | Arseniy Alekseyev <aalekseyev@janestreet.com> |
---|---|
date | Tue, 26 Oct 2021 19:47:30 +0100 |
parents | 1249eb9cc332 |
children | 9dcfd1d05e6e |
comparison
equal
deleted
inserted
replaced
48310:229f5ee1a08a | 48311:6d69e83e6b6e |
---|---|
218 HgPath::new(&self.inner[..*size]), | 218 HgPath::new(&self.inner[..*size]), |
219 HgPath::new(&self.inner[*size + 1..]), | 219 HgPath::new(&self.inner[*size + 1..]), |
220 ), | 220 ), |
221 } | 221 } |
222 } | 222 } |
223 pub fn join<T: ?Sized + AsRef<Self>>(&self, other: &T) -> HgPathBuf { | 223 |
224 let mut inner = self.inner.to_owned(); | 224 pub fn join(&self, path: &HgPath) -> HgPathBuf { |
225 if !inner.is_empty() && inner.last() != Some(&b'/') { | 225 let mut buf = self.to_owned(); |
226 inner.push(b'/'); | 226 buf.push(path); |
227 } | 227 buf |
228 inner.extend(other.as_ref().bytes()); | |
229 HgPathBuf::from_bytes(&inner) | |
230 } | 228 } |
231 | 229 |
232 pub fn components(&self) -> impl Iterator<Item = &HgPath> { | 230 pub fn components(&self) -> impl Iterator<Item = &HgPath> { |
233 self.inner.split(|&byte| byte == b'/').map(HgPath::new) | 231 self.inner.split(|&byte| byte == b'/').map(HgPath::new) |
234 } | 232 } |
403 | 401 |
404 impl HgPathBuf { | 402 impl HgPathBuf { |
405 pub fn new() -> Self { | 403 pub fn new() -> Self { |
406 Default::default() | 404 Default::default() |
407 } | 405 } |
408 pub fn push(&mut self, byte: u8) { | 406 |
407 pub fn push<T: ?Sized + AsRef<HgPath>>(&mut self, other: &T) -> () { | |
408 if !self.inner.is_empty() && self.inner.last() != Some(&b'/') { | |
409 self.inner.push(b'/'); | |
410 } | |
411 self.inner.extend(other.as_ref().bytes()) | |
412 } | |
413 | |
414 pub fn push_byte(&mut self, byte: u8) { | |
409 self.inner.push(byte); | 415 self.inner.push(byte); |
410 } | 416 } |
411 pub fn from_bytes(s: &[u8]) -> HgPathBuf { | 417 pub fn from_bytes(s: &[u8]) -> HgPathBuf { |
412 HgPath::new(s).to_owned() | 418 HgPath::new(s).to_owned() |
413 } | 419 } |