Mercurial > hg
comparison rust/hg-core/src/dirstate/parsers.rs @ 48068:bf8837e3d7ce
dirstate: Remove the flat Rust DirstateMap implementation
Before this changeset we had two Rust implementations of `DirstateMap`.
This removes the "flat" DirstateMap so that the "tree" DirstateMap is always
used when Rust enabled. This simplifies the code a lot, and will enable
(in the next changeset) further removal of a trait abstraction.
This is a performance regression when:
* Rust is enabled, and
* The repository uses the legacy dirstate-v1 file format, and
* For `hg status`, unknown files are not listed (such as with `-mard`)
The regression is about 100 milliseconds for `hg status -mard` on a
semi-large repository (mozilla-central), from ~320ms to ~420ms.
We deem this to be small enough to be worth it.
The new dirstate-v2 is still experimental at this point, but we aim to
stabilize it (though not yet enable it by default for new repositories)
in Mercurial 6.0. Eventually, upgrating repositories to dirsate-v2 will
eliminate this regression (and enable other performance improvements).
# Background
The flat DirstateMap was introduced with the first Rust implementation of the
status algorithm. It works similarly to the previous Python + C one, with a
single `HashMap` that associates file paths to a `DirstateEntry` (where Python
has a dict).
We later added the tree DirstateMap where the root of the tree contains nodes
for files and directories that are directly at the root of the repository,
and nodes for directories can contain child nodes representing the files and
directly that *they* contain directly. The shape of this tree mirrors that of
the working directory in the filesystem. This enables the status algorithm to
traverse this tree in tandem with traversing the filesystem tree, which in
turns enables a more efficient algorithm.
Furthermore, the new dirstate-v2 file format is also based on a tree of the
same shape. The tree DirstateMap can access a dirstate-v2 file without parsing
it: binary data in a single large (possibly memory-mapped) bytes buffer is
traversed on demand. This allows `DirstateMap` creation to take `O(1)` time.
(Mutation works by creating new in-memory nodes with copy-on-write semantics,
and serialization is append-mostly.)
The tradeoff is that for "legacy" repositories that use the dirstate-v1 file
format, parsing that file into a tree DirstateMap takes more time. Profiling
shows that this time is dominated by `HashMap`. For a dirstate containing `F`
files with an average `D` directory depth, the flat DirstateMap does parsing
in `O(F)` number of HashMap operations but the tree DirstateMap in `O(F × D)`
operations, since each node has its own HashMap containing its child nodes.
This slower costs ~140ms on an old snapshot of mozilla-central, and ~80ms
on an old snapshot of the Netbeans repository.
The status algorithm is faster, but with `-mard` (when not listing unknown
files) it is typically not faster *enough* to compensate the slower parsing.
Both Rust implementations are always faster than the Python + C implementation
# Benchmark results
All benchmarks are run on changeset 98c0408324e6, with repositories that use
the dirstate-v1 file format, on a server with 4 CPU cores and 4 CPU threads
(no HyperThreading).
`hg status` benchmarks show wall clock times of the entire command as the
average and standard deviation of serveral runs, collected by
https://github.com/sharkdp/hyperfine and reformated.
Parsing benchmarks are wall clock time of the Rust function that converts a
bytes buffer of the dirstate file into the `DirstateMap` data structure as
used by the status algorithm. A single run each, collected by running
`hg status` this environment variable:
RUST_LOG=hg::dirstate::dirstate_map=trace,hg::dirstate_tree::dirstate_map=trace
Benchmark 1: Rust flat DirstateMap → Rust tree DirstateMap
hg status
mozilla-clean 562.3 ms ± 2.0 ms → 462.5 ms ± 0.6 ms 1.22 ± 0.00 times faster
mozilla-dirty 859.6 ms ± 2.2 ms → 719.5 ms ± 3.2 ms 1.19 ± 0.01 times faster
mozilla-ignored 558.2 ms ± 3.0 ms → 457.9 ms ± 2.9 ms 1.22 ± 0.01 times faster
mozilla-unknowns 859.4 ms ± 5.7 ms → 716.0 ms ± 4.7 ms 1.20 ± 0.01 times faster
netbeans-clean 336.5 ms ± 0.9 ms → 339.5 ms ± 0.4 ms 0.99 ± 0.00 times faster
netbeans-dirty 491.4 ms ± 1.6 ms → 475.1 ms ± 1.2 ms 1.03 ± 0.00 times faster
netbeans-ignored 343.7 ms ± 1.0 ms → 347.8 ms ± 0.4 ms 0.99 ± 0.00 times faster
netbeans-unknowns 484.3 ms ± 1.0 ms → 466.0 ms ± 1.2 ms 1.04 ± 0.00 times faster
hg status -mard
mozilla-clean 317.3 ms ± 0.6 ms → 422.5 ms ± 1.2 ms 0.75 ± 0.00 times faster
mozilla-dirty 315.4 ms ± 0.6 ms → 417.7 ms ± 1.1 ms 0.76 ± 0.00 times faster
mozilla-ignored 314.6 ms ± 0.6 ms → 417.4 ms ± 1.0 ms 0.75 ± 0.00 times faster
mozilla-unknowns 312.9 ms ± 0.9 ms → 417.3 ms ± 1.6 ms 0.75 ± 0.00 times faster
netbeans-clean 212.0 ms ± 0.6 ms → 283.6 ms ± 0.8 ms 0.75 ± 0.00 times faster
netbeans-dirty 211.4 ms ± 1.0 ms → 283.4 ms ± 1.6 ms 0.75 ± 0.01 times faster
netbeans-ignored 211.4 ms ± 0.9 ms → 283.9 ms ± 0.8 ms 0.74 ± 0.01 times faster
netbeans-unknowns 211.1 ms ± 0.6 ms → 283.4 ms ± 1.0 ms 0.74 ± 0.00 times faster
Parsing
mozilla-clean 38.4ms → 177.6ms
mozilla-dirty 38.8ms → 177.0ms
mozilla-ignored 38.8ms → 178.0ms
mozilla-unknowns 38.7ms → 176.9ms
netbeans-clean 16.5ms → 97.3ms
netbeans-dirty 16.5ms → 98.4ms
netbeans-ignored 16.9ms → 97.4ms
netbeans-unknowns 16.9ms → 96.3ms
Benchmark 2: Python + C dirstatemap → Rust tree DirstateMap
hg status
mozilla-clean 1261.0 ms ± 3.6 ms → 461.1 ms ± 0.5 ms 2.73 ± 0.00 times faster
mozilla-dirty 2293.4 ms ± 9.1 ms → 719.6 ms ± 3.6 ms 3.19 ± 0.01 times faster
mozilla-ignored 1240.4 ms ± 2.3 ms → 457.7 ms ± 1.9 ms 2.71 ± 0.00 times faster
mozilla-unknowns 2283.3 ms ± 9.0 ms → 719.7 ms ± 3.8 ms 3.17 ± 0.01 times faster
netbeans-clean 879.7 ms ± 3.5 ms → 339.9 ms ± 0.5 ms 2.59 ± 0.00 times faster
netbeans-dirty 1257.3 ms ± 4.7 ms → 474.6 ms ± 1.6 ms 2.65 ± 0.01 times faster
netbeans-ignored 943.9 ms ± 1.9 ms → 347.3 ms ± 1.1 ms 2.72 ± 0.00 times faster
netbeans-unknowns 1188.1 ms ± 5.0 ms → 465.2 ms ± 2.3 ms 2.55 ± 0.01 times faster
hg status -mard
mozilla-clean 903.2 ms ± 3.6 ms → 423.4 ms ± 2.2 ms 2.13 ± 0.01 times faster
mozilla-dirty 884.6 ms ± 4.5 ms → 417.3 ms ± 1.4 ms 2.12 ± 0.01 times faster
mozilla-ignored 881.9 ms ± 1.3 ms → 417.3 ms ± 0.8 ms 2.11 ± 0.00 times faster
mozilla-unknowns 878.5 ms ± 1.9 ms → 416.4 ms ± 0.9 ms 2.11 ± 0.00 times faster
netbeans-clean 434.9 ms ± 1.8 ms → 284.0 ms ± 0.8 ms 1.53 ± 0.01 times faster
netbeans-dirty 434.1 ms ± 0.8 ms → 283.1 ms ± 0.8 ms 1.53 ± 0.00 times faster
netbeans-ignored 431.7 ms ± 1.1 ms → 283.6 ms ± 1.8 ms 1.52 ± 0.01 times faster
netbeans-unknowns 433.0 ms ± 1.3 ms → 283.5 ms ± 0.7 ms 1.53 ± 0.00 times faster
Differential Revision: https://phab.mercurial-scm.org/D11516
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 27 Sep 2021 12:09:15 +0200 |
parents | f2a9db29cb2d |
children | 269ff8978086 |
comparison
equal
deleted
inserted
replaced
48067:d3eb5f50052c | 48068:bf8837e3d7ce |
---|---|
3 // This software may be used and distributed according to the terms of the | 3 // This software may be used and distributed according to the terms of the |
4 // GNU General Public License version 2 or any later version. | 4 // GNU General Public License version 2 or any later version. |
5 | 5 |
6 use crate::errors::HgError; | 6 use crate::errors::HgError; |
7 use crate::utils::hg_path::HgPath; | 7 use crate::utils::hg_path::HgPath; |
8 use crate::{ | 8 use crate::{dirstate::EntryState, DirstateEntry, DirstateParents}; |
9 dirstate::{CopyMap, EntryState, StateMap}, | |
10 DirstateEntry, DirstateParents, | |
11 }; | |
12 use byteorder::{BigEndian, WriteBytesExt}; | 9 use byteorder::{BigEndian, WriteBytesExt}; |
13 use bytes_cast::{unaligned, BytesCast}; | 10 use bytes_cast::{unaligned, BytesCast}; |
14 use micro_timer::timed; | 11 use micro_timer::timed; |
15 use std::convert::{TryFrom, TryInto}; | 12 use std::convert::TryFrom; |
16 | 13 |
17 /// Parents are stored in the dirstate as byte hashes. | 14 /// Parents are stored in the dirstate as byte hashes. |
18 pub const PARENT_SIZE: usize = 20; | 15 pub const PARENT_SIZE: usize = 20; |
19 /// Dirstate entries have a static part of 8 + 32 + 32 + 32 + 32 bits. | 16 /// Dirstate entries have a static part of 8 + 32 + 32 + 32 + 32 bits. |
20 const MIN_ENTRY_SIZE: usize = 17; | 17 const MIN_ENTRY_SIZE: usize = 17; |
139 } | 136 } |
140 } | 137 } |
141 | 138 |
142 /// Seconds since the Unix epoch | 139 /// Seconds since the Unix epoch |
143 pub struct Timestamp(pub i64); | 140 pub struct Timestamp(pub i64); |
144 | |
145 pub fn pack_dirstate( | |
146 state_map: &mut StateMap, | |
147 copy_map: &CopyMap, | |
148 parents: DirstateParents, | |
149 now: Timestamp, | |
150 ) -> Result<Vec<u8>, HgError> { | |
151 // TODO move away from i32 before 2038. | |
152 let now: i32 = now.0.try_into().expect("time overflow"); | |
153 | |
154 let expected_size: usize = state_map | |
155 .iter() | |
156 .map(|(filename, _)| { | |
157 packed_entry_size(filename, copy_map.get(filename).map(|p| &**p)) | |
158 }) | |
159 .sum(); | |
160 let expected_size = expected_size + PARENT_SIZE * 2; | |
161 | |
162 let mut packed = Vec::with_capacity(expected_size); | |
163 | |
164 packed.extend(parents.p1.as_bytes()); | |
165 packed.extend(parents.p2.as_bytes()); | |
166 | |
167 for (filename, entry) in state_map.iter_mut() { | |
168 entry.clear_ambiguous_mtime(now); | |
169 pack_entry( | |
170 filename, | |
171 entry, | |
172 copy_map.get(filename).map(|p| &**p), | |
173 &mut packed, | |
174 ) | |
175 } | |
176 | |
177 if packed.len() != expected_size { | |
178 return Err(HgError::CorruptedRepository(format!( | |
179 "bad dirstate size: {} != {}", | |
180 expected_size, | |
181 packed.len() | |
182 ))); | |
183 } | |
184 | |
185 Ok(packed) | |
186 } | |
187 | |
188 #[cfg(test)] | |
189 mod tests { | |
190 use super::*; | |
191 use crate::{utils::hg_path::HgPathBuf, FastHashMap}; | |
192 use pretty_assertions::assert_eq; | |
193 | |
194 #[test] | |
195 fn test_pack_dirstate_empty() { | |
196 let mut state_map = StateMap::default(); | |
197 let copymap = FastHashMap::default(); | |
198 let parents = DirstateParents { | |
199 p1: b"12345678910111213141".into(), | |
200 p2: b"00000000000000000000".into(), | |
201 }; | |
202 let now = Timestamp(15000000); | |
203 let expected = b"1234567891011121314100000000000000000000".to_vec(); | |
204 | |
205 assert_eq!( | |
206 expected, | |
207 pack_dirstate(&mut state_map, ©map, parents, now).unwrap() | |
208 ); | |
209 | |
210 assert!(state_map.is_empty()) | |
211 } | |
212 #[test] | |
213 fn test_pack_dirstate_one_entry() { | |
214 let expected_state_map: StateMap = [( | |
215 HgPathBuf::from_bytes(b"f1"), | |
216 DirstateEntry::from_v1_data( | |
217 EntryState::Normal, | |
218 0o644, | |
219 0, | |
220 791231220, | |
221 ), | |
222 )] | |
223 .iter() | |
224 .cloned() | |
225 .collect(); | |
226 let mut state_map = expected_state_map.clone(); | |
227 | |
228 let copymap = FastHashMap::default(); | |
229 let parents = DirstateParents { | |
230 p1: b"12345678910111213141".into(), | |
231 p2: b"00000000000000000000".into(), | |
232 }; | |
233 let now = Timestamp(15000000); | |
234 let expected = [ | |
235 49, 50, 51, 52, 53, 54, 55, 56, 57, 49, 48, 49, 49, 49, 50, 49, | |
236 51, 49, 52, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, | |
237 48, 48, 48, 48, 48, 48, 48, 48, 110, 0, 0, 1, 164, 0, 0, 0, 0, 47, | |
238 41, 58, 244, 0, 0, 0, 2, 102, 49, | |
239 ] | |
240 .to_vec(); | |
241 | |
242 assert_eq!( | |
243 expected, | |
244 pack_dirstate(&mut state_map, ©map, parents, now).unwrap() | |
245 ); | |
246 | |
247 assert_eq!(expected_state_map, state_map); | |
248 } | |
249 #[test] | |
250 fn test_pack_dirstate_one_entry_with_copy() { | |
251 let expected_state_map: StateMap = [( | |
252 HgPathBuf::from_bytes(b"f1"), | |
253 DirstateEntry::from_v1_data( | |
254 EntryState::Normal, | |
255 0o644, | |
256 0, | |
257 791231220, | |
258 ), | |
259 )] | |
260 .iter() | |
261 .cloned() | |
262 .collect(); | |
263 let mut state_map = expected_state_map.clone(); | |
264 let mut copymap = FastHashMap::default(); | |
265 copymap.insert( | |
266 HgPathBuf::from_bytes(b"f1"), | |
267 HgPathBuf::from_bytes(b"copyname"), | |
268 ); | |
269 let parents = DirstateParents { | |
270 p1: b"12345678910111213141".into(), | |
271 p2: b"00000000000000000000".into(), | |
272 }; | |
273 let now = Timestamp(15000000); | |
274 let expected = [ | |
275 49, 50, 51, 52, 53, 54, 55, 56, 57, 49, 48, 49, 49, 49, 50, 49, | |
276 51, 49, 52, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, | |
277 48, 48, 48, 48, 48, 48, 48, 48, 110, 0, 0, 1, 164, 0, 0, 0, 0, 47, | |
278 41, 58, 244, 0, 0, 0, 11, 102, 49, 0, 99, 111, 112, 121, 110, 97, | |
279 109, 101, | |
280 ] | |
281 .to_vec(); | |
282 | |
283 assert_eq!( | |
284 expected, | |
285 pack_dirstate(&mut state_map, ©map, parents, now).unwrap() | |
286 ); | |
287 assert_eq!(expected_state_map, state_map); | |
288 } | |
289 | |
290 #[test] | |
291 fn test_parse_pack_one_entry_with_copy() { | |
292 let mut state_map: StateMap = [( | |
293 HgPathBuf::from_bytes(b"f1"), | |
294 DirstateEntry::from_v1_data( | |
295 EntryState::Normal, | |
296 0o644, | |
297 0, | |
298 791231220, | |
299 ), | |
300 )] | |
301 .iter() | |
302 .cloned() | |
303 .collect(); | |
304 let mut copymap = FastHashMap::default(); | |
305 copymap.insert( | |
306 HgPathBuf::from_bytes(b"f1"), | |
307 HgPathBuf::from_bytes(b"copyname"), | |
308 ); | |
309 let parents = DirstateParents { | |
310 p1: b"12345678910111213141".into(), | |
311 p2: b"00000000000000000000".into(), | |
312 }; | |
313 let now = Timestamp(15000000); | |
314 let result = | |
315 pack_dirstate(&mut state_map, ©map, parents.clone(), now) | |
316 .unwrap(); | |
317 | |
318 let (new_parents, entries, copies) = | |
319 parse_dirstate(result.as_slice()).unwrap(); | |
320 let new_state_map: StateMap = entries | |
321 .into_iter() | |
322 .map(|(path, entry)| (path.to_owned(), entry)) | |
323 .collect(); | |
324 let new_copy_map: CopyMap = copies | |
325 .into_iter() | |
326 .map(|(path, copy)| (path.to_owned(), copy.to_owned())) | |
327 .collect(); | |
328 | |
329 assert_eq!( | |
330 (&parents, state_map, copymap), | |
331 (new_parents, new_state_map, new_copy_map) | |
332 ) | |
333 } | |
334 | |
335 #[test] | |
336 fn test_parse_pack_multiple_entries_with_copy() { | |
337 let mut state_map: StateMap = [ | |
338 ( | |
339 HgPathBuf::from_bytes(b"f1"), | |
340 DirstateEntry::from_v1_data( | |
341 EntryState::Normal, | |
342 0o644, | |
343 0, | |
344 791231220, | |
345 ), | |
346 ), | |
347 ( | |
348 HgPathBuf::from_bytes(b"f2"), | |
349 DirstateEntry::from_v1_data( | |
350 EntryState::Merged, | |
351 0o777, | |
352 1000, | |
353 791231220, | |
354 ), | |
355 ), | |
356 ( | |
357 HgPathBuf::from_bytes(b"f3"), | |
358 DirstateEntry::from_v1_data( | |
359 EntryState::Removed, | |
360 0o644, | |
361 234553, | |
362 791231220, | |
363 ), | |
364 ), | |
365 ( | |
366 HgPathBuf::from_bytes(b"f4\xF6"), | |
367 DirstateEntry::from_v1_data(EntryState::Added, 0o644, -1, -1), | |
368 ), | |
369 ] | |
370 .iter() | |
371 .cloned() | |
372 .collect(); | |
373 let mut copymap = FastHashMap::default(); | |
374 copymap.insert( | |
375 HgPathBuf::from_bytes(b"f1"), | |
376 HgPathBuf::from_bytes(b"copyname"), | |
377 ); | |
378 copymap.insert( | |
379 HgPathBuf::from_bytes(b"f4\xF6"), | |
380 HgPathBuf::from_bytes(b"copyname2"), | |
381 ); | |
382 let parents = DirstateParents { | |
383 p1: b"12345678910111213141".into(), | |
384 p2: b"00000000000000000000".into(), | |
385 }; | |
386 let now = Timestamp(15000000); | |
387 let result = | |
388 pack_dirstate(&mut state_map, ©map, parents.clone(), now) | |
389 .unwrap(); | |
390 | |
391 let (new_parents, entries, copies) = | |
392 parse_dirstate(result.as_slice()).unwrap(); | |
393 let new_state_map: StateMap = entries | |
394 .into_iter() | |
395 .map(|(path, entry)| (path.to_owned(), entry)) | |
396 .collect(); | |
397 let new_copy_map: CopyMap = copies | |
398 .into_iter() | |
399 .map(|(path, copy)| (path.to_owned(), copy.to_owned())) | |
400 .collect(); | |
401 | |
402 assert_eq!( | |
403 (&parents, state_map, copymap), | |
404 (new_parents, new_state_map, new_copy_map) | |
405 ) | |
406 } | |
407 | |
408 #[test] | |
409 /// https://www.mercurial-scm.org/repo/hg/rev/af3f26b6bba4 | |
410 fn test_parse_pack_one_entry_with_copy_and_time_conflict() { | |
411 let mut state_map: StateMap = [( | |
412 HgPathBuf::from_bytes(b"f1"), | |
413 DirstateEntry::from_v1_data( | |
414 EntryState::Normal, | |
415 0o644, | |
416 0, | |
417 15000000, | |
418 ), | |
419 )] | |
420 .iter() | |
421 .cloned() | |
422 .collect(); | |
423 let mut copymap = FastHashMap::default(); | |
424 copymap.insert( | |
425 HgPathBuf::from_bytes(b"f1"), | |
426 HgPathBuf::from_bytes(b"copyname"), | |
427 ); | |
428 let parents = DirstateParents { | |
429 p1: b"12345678910111213141".into(), | |
430 p2: b"00000000000000000000".into(), | |
431 }; | |
432 let now = Timestamp(15000000); | |
433 let result = | |
434 pack_dirstate(&mut state_map, ©map, parents.clone(), now) | |
435 .unwrap(); | |
436 | |
437 let (new_parents, entries, copies) = | |
438 parse_dirstate(result.as_slice()).unwrap(); | |
439 let new_state_map: StateMap = entries | |
440 .into_iter() | |
441 .map(|(path, entry)| (path.to_owned(), entry)) | |
442 .collect(); | |
443 let new_copy_map: CopyMap = copies | |
444 .into_iter() | |
445 .map(|(path, copy)| (path.to_owned(), copy.to_owned())) | |
446 .collect(); | |
447 | |
448 assert_eq!( | |
449 ( | |
450 &parents, | |
451 [( | |
452 HgPathBuf::from_bytes(b"f1"), | |
453 DirstateEntry::from_v1_data( | |
454 EntryState::Normal, | |
455 0o644, | |
456 0, | |
457 -1 | |
458 ) | |
459 )] | |
460 .iter() | |
461 .cloned() | |
462 .collect::<StateMap>(), | |
463 copymap, | |
464 ), | |
465 (new_parents, new_state_map, new_copy_map) | |
466 ) | |
467 } | |
468 } |