comparison rust/hg-core/src/utils.rs @ 52316:f4aede0f01af

rust-manifest: use `memchr` crate for all byte-finding needs While writing a very dumb manifest diffing algorithm for a proof-of-concept I saw that `Manifest::find_by_path` was much slower than I was expecting. It turns out that the Rust stdlib uses slow (all is relative) code when searching for byte positions for reasons ranging from portability, SIMD API stability, nobody doing the work, etc. `memch` is much faster for these purposes, so let's use it. I was measuring ~670ms of profile time in `find_by_path`, after this patch it went down to ~230ms.
author Rapha?l Gom?s <rgomes@octobus.net>
date Tue, 12 Nov 2024 23:20:04 +0100
parents e6a44bc91bc2
children 36d39726c0af
comparison
equal deleted inserted replaced
52315:fad30cb98579 52316:f4aede0f01af
132 None 132 None
133 } 133 }
134 } 134 }
135 135
136 fn split_2(&self, separator: u8) -> Option<(&[u8], &[u8])> { 136 fn split_2(&self, separator: u8) -> Option<(&[u8], &[u8])> {
137 let mut iter = self.splitn(2, |&byte| byte == separator); 137 let pos = memchr::memchr(separator, self)?;
138 let a = iter.next()?; 138 Some((&self[..pos], &self[pos + 1..]))
139 let b = iter.next()?;
140 Some((a, b))
141 } 139 }
142 140
143 fn split_2_by_slice(&self, separator: &[u8]) -> Option<(&[u8], &[u8])> { 141 fn split_2_by_slice(&self, separator: &[u8]) -> Option<(&[u8], &[u8])> {
144 find_slice_in_slice(self, separator) 142 find_slice_in_slice(self, separator)
145 .map(|pos| (&self[..pos], &self[pos + separator.len()..])) 143 .map(|pos| (&self[..pos], &self[pos + separator.len()..]))