##// END OF EJS Templates
copies-rust: leverage the immutability for efficient update...
marmoute -
r46585:cc759d3d default
parent child Browse files
Show More
@@ -1,6 +1,7 b''
1 use crate::utils::hg_path::HgPathBuf;
1 use crate::utils::hg_path::HgPathBuf;
2 use crate::Revision;
2 use crate::Revision;
3
3
4 use im_rc::ordmap::DiffItem;
4 use im_rc::ordmap::OrdMap;
5 use im_rc::ordmap::OrdMap;
5
6
6 use std::collections::HashMap;
7 use std::collections::HashMap;
@@ -8,7 +9,7 b' use std::collections::HashSet;'
8
9
9 pub type PathCopies = HashMap<HgPathBuf, HgPathBuf>;
10 pub type PathCopies = HashMap<HgPathBuf, HgPathBuf>;
10
11
11 #[derive(Clone, Debug)]
12 #[derive(Clone, Debug, PartialEq)]
12 struct TimeStampedPathCopy {
13 struct TimeStampedPathCopy {
13 /// revision at which the copy information was added
14 /// revision at which the copy information was added
14 rev: Revision,
15 rev: Revision,
@@ -199,70 +200,116 b' fn merge_copies_dict('
199 changes: &ChangedFiles,
200 changes: &ChangedFiles,
200 is_ancestor: &impl Fn(Revision, Revision) -> bool,
201 is_ancestor: &impl Fn(Revision, Revision) -> bool,
201 ) -> TimeStampedPathCopies {
202 ) -> TimeStampedPathCopies {
202 let mut result = minor.clone();
203 if minor.is_empty() {
203 for (dest, src_major) in major {
204 return major;
204 let overwrite;
205 } else if major.is_empty() {
205 if let Some(src_minor) = minor.get(&dest) {
206 return minor;
206 {
207 }
208 let mut override_minor = Vec::new();
209 let mut override_major = Vec::new();
210
211 let mut to_major = |k: &HgPathBuf, v: &TimeStampedPathCopy| {
212 override_major.push((k.clone(), v.clone()))
213 };
214 let mut to_minor = |k: &HgPathBuf, v: &TimeStampedPathCopy| {
215 override_minor.push((k.clone(), v.clone()))
216 };
217
218 // The diff function leverage detection of the identical subpart if minor
219 // and major has some common ancestors. This make it very fast is most
220 // case.
221 //
222 // In case where the two map are vastly different in size, the current
223 // approach is still slowish because the iteration will iterate over
224 // all the "exclusive" content of the larger on. This situation can be
225 // frequent when the subgraph of revision we are processing has a lot
226 // of roots. Each roots adding they own fully new map to the mix (and
227 // likely a small map, if the path from the root to the "main path" is
228 // small.
229 //
230 // We could do better by detecting such situation and processing them
231 // differently.
232 for d in minor.diff(&major) {
233 match d {
234 DiffItem::Add(k, v) => to_minor(k, v),
235 DiffItem::Remove(k, v) => to_major(k, v),
236 DiffItem::Update { old, new } => {
237 let (dest, src_major) = new;
238 let (_, src_minor) = old;
239 let mut pick_minor = || (to_major(dest, src_minor));
240 let mut pick_major = || (to_minor(dest, src_major));
207 if src_major.path == src_minor.path {
241 if src_major.path == src_minor.path {
208 // we have the same value, no need to battle;
242 // we have the same value, but from other source;
209 if src_major.rev == src_minor.rev {
243 if src_major.rev == src_minor.rev {
210 // If the two entry are identical, no need to do
244 // If the two entry are identical, no need to do
211 // anything
245 // anything (but diff should not have yield them)
212 overwrite = false;
246 unreachable!();
213 } else if is_ancestor(src_major.rev, src_minor.rev) {
247 } else if is_ancestor(src_major.rev, src_minor.rev) {
214 overwrite = false;
248 pick_minor();
215 } else {
249 } else {
216 overwrite = true;
250 pick_major();
217 }
251 }
218 } else if src_major.rev == src_minor.rev {
252 } else if src_major.rev == src_minor.rev {
219 // We cannot get copy information for both p1 and p2 in the
253 // We cannot get copy information for both p1 and p2 in the
220 // same rev. So this is the same value.
254 // same rev. So this is the same value.
221 overwrite = false;
255 unreachable!();
222 } else if src_major.path.is_none()
256 } else if src_major.path.is_none()
223 && changes.salvaged.contains(&dest)
257 && changes.salvaged.contains(dest)
224 {
258 {
225 // If the file is "deleted" in the major side but was
259 // If the file is "deleted" in the major side but was
226 // salvaged by the merge, we keep the minor side alive
260 // salvaged by the merge, we keep the minor side alive
227 overwrite = false;
261 pick_minor();
228 } else if src_minor.path.is_none()
262 } else if src_minor.path.is_none()
229 && changes.salvaged.contains(&dest)
263 && changes.salvaged.contains(dest)
230 {
264 {
231 // If the file is "deleted" in the minor side but was
265 // If the file is "deleted" in the minor side but was
232 // salvaged by the merge, unconditionnaly preserve the
266 // salvaged by the merge, unconditionnaly preserve the
233 // major side.
267 // major side.
234 overwrite = true;
268 pick_major();
235 } else if changes.merged.contains(&dest) {
269 } else if changes.merged.contains(dest) {
236 // If the file was actively merged, copy information from
270 // If the file was actively merged, copy information from
237 // each side might conflict. The major side will win such
271 // each side might conflict. The major side will win such
238 // conflict.
272 // conflict.
239 overwrite = true;
273 pick_major();
240 } else if is_ancestor(src_major.rev, src_minor.rev) {
274 } else if is_ancestor(src_major.rev, src_minor.rev) {
241 // If the minor side is strictly newer than the major side,
275 // If the minor side is strictly newer than the major side,
242 // it should be kept.
276 // it should be kept.
243 overwrite = false;
277 pick_minor();
244 } else if src_major.path.is_some() {
278 } else if src_major.path.is_some() {
245 // without any special case, the "major" value win other
279 // without any special case, the "major" value win other
246 // the "minor" one.
280 // the "minor" one.
247 overwrite = true;
281 pick_major();
248 } else if is_ancestor(src_minor.rev, src_major.rev) {
282 } else if is_ancestor(src_minor.rev, src_major.rev) {
249 // the "major" rev is a direct ancestors of "minor", any
283 // the "major" rev is a direct ancestors of "minor", any
250 // different value should overwrite
284 // different value should overwrite
251 overwrite = true;
285 pick_major();
252 } else {
286 } else {
253 // major version is None (so the file was deleted on that
287 // major version is None (so the file was deleted on that
254 // branch) and that branch is independant (neither minor
288 // branch) and that branch is independant (neither minor
255 // nor major is an ancestors of the other one.) We preserve
289 // nor major is an ancestors of the other one.) We preserve
256 // the new information about the new file.
290 // the new information about the new file.
257 overwrite = false;
291 pick_minor();
258 }
292 }
259 }
293 }
294 };
295 }
296
297 let updates;
298 let mut result;
299 if override_major.is_empty() {
300 result = major
301 } else if override_minor.is_empty() {
302 result = minor
303 } else {
304 if override_minor.len() < override_major.len() {
305 updates = override_minor;
306 result = minor;
260 } else {
307 } else {
261 // minor had no value
308 updates = override_major;
262 overwrite = true;
309 result = major;
263 }
310 }
264 if overwrite {
311 for (k, v) in updates {
265 result.insert(dest, src_major);
312 result.insert(k, v);
266 }
313 }
267 }
314 }
268 result
315 result
General Comments 0
You need to be logged in to leave comments. Login now