| 1 |
//! This module contains code to understand and process deltas used by Mercurial |
| 2 |
|
| 3 |
use crate::Revision; |
| 4 |
|
| 5 |
/// Size of a patch header piece |
| 6 |
const HP_SIZE: usize = size_of::<i32>(); |
| 7 |
/// Size of a patch header (start, old_end, size) |
| 8 |
const H_SIZE: usize = HP_SIZE * 3; |
| 9 |
|
| 10 |
/// A piece of delta that replace a section of the base by another piece of |
| 11 |
/// data. |
| 12 |
#[derive(Copy, Clone, Debug, PartialEq)] |
| 13 |
struct DeltaPiece { |
| 14 |
start: i32, |
| 15 |
old_end: i32, |
| 16 |
size: i32, |
| 17 |
} |
| 18 |
|
| 19 |
impl DeltaPiece { |
| 20 |
/// return a new version of this DeltaPiece move by <offset> bytes |
| 21 |
/// |
| 22 |
/// This is used when adjusting a delta to the change made by another one |
| 23 |
pub fn offsetted(&self, offset: i32) -> Self { |
| 24 |
if offset == 0 { |
| 25 |
return *self; |
| 26 |
} |
| 27 |
assert!( |
| 28 |
0 <= (self.start + offset), |
| 29 |
"start: {}, offset: {}", |
| 30 |
self.start, |
| 31 |
offset |
| 32 |
); |
| 33 |
|
| 34 |
Self { |
| 35 |
start: self.start + offset, |
| 36 |
old_end: self.old_end + offset, |
| 37 |
size: self.size, |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/// the amount of bytes content after this DeltaPiece get shifted. |
| 42 |
pub fn offset(&self) -> i32 { |
| 43 |
(self.start + self.size) - self.old_end |
| 44 |
} |
| 45 |
|
| 46 |
/// return a new version of this DeltaPiece, removing the <amount> first |
| 47 |
/// bytes change to the base. |
| 48 |
pub fn truncated(&self, amount: i32) -> Self { |
| 49 |
assert!(amount < (self.old_end - self.start)); |
| 50 |
DeltaPiece { |
| 51 |
start: self.start + amount, |
| 52 |
old_end: self.old_end, |
| 53 |
size: self.size - amount.min(self.size), |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/// estimate an upper bound of a delta combining and equivalement range |
| 59 |
pub fn estimate_combined_deltas_size(deltas: Vec<&[u8]>) -> usize { |
| 60 |
assert!(deltas.len() > 1); |
| 61 |
let mut high_delta = vec![]; |
| 62 |
let mut low_delta = vec![]; |
| 63 |
let mut folded_delta = vec![]; |
| 64 |
let base_data = deltas.last().expect("can't be empty"); |
| 65 |
let mut combined_size = chunk_to_delta(&mut high_delta, base_data); |
| 66 |
for idx in (0..deltas.len() - 1).rev() { |
| 67 |
low_delta.clear(); |
| 68 |
chunk_to_delta(&mut low_delta, deltas[idx]); |
| 69 |
combined_size = fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 70 |
std::mem::swap(&mut high_delta, &mut folded_delta); |
| 71 |
} |
| 72 |
combined_size |
| 73 |
} |
| 74 |
|
| 75 |
/// Search for an base down the delta chain that would produce a better delta |
| 76 |
/// |
| 77 |
/// This use delta folding to estimate the effect using a delta base further |
| 78 |
/// down the chain and keep going down as long as the resulting estimate are |
| 79 |
/// better than the target size. |
| 80 |
pub fn optimize_base( |
| 81 |
delta: &[u8], |
| 82 |
next_deltas: &mut impl Iterator< |
| 83 |
Item = (Revision, impl std::ops::Deref<Target = [u8]>), |
| 84 |
>, |
| 85 |
max_size: usize, |
| 86 |
) -> Option<Revision> { |
| 87 |
if delta.is_empty() { |
| 88 |
// if the delta is empty, we won't do better unless the rest of the |
| 89 |
// chain is also empty, and those should have already be optimized by |
| 90 |
// the search strategy |
| 91 |
return None; |
| 92 |
} |
| 93 |
let mut high_delta = vec![]; |
| 94 |
let mut low_delta = vec![]; |
| 95 |
let mut folded_delta = vec![]; |
| 96 |
let mut best_base: Option<Revision> = None; |
| 97 |
chunk_to_delta(&mut high_delta, delta); |
| 98 |
|
| 99 |
for (base, data) in next_deltas { |
| 100 |
let delta_data = data.deref(); |
| 101 |
if !delta_data.is_empty() { |
| 102 |
low_delta.clear(); |
| 103 |
folded_delta.clear(); |
| 104 |
chunk_to_delta(&mut low_delta, delta_data); |
| 105 |
let folded_size = |
| 106 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 107 |
if folded_size > max_size { |
| 108 |
break; |
| 109 |
} |
| 110 |
std::mem::swap(&mut high_delta, &mut folded_delta); |
| 111 |
} |
| 112 |
best_base = Some(base); |
| 113 |
} |
| 114 |
best_base |
| 115 |
} |
| 116 |
|
| 117 |
/// Combine two delta into a new delta |
| 118 |
/// |
| 119 |
/// The `bottom_delta` should be the earlier delta in the chain, the |
| 120 |
/// `top_delta` object applying over it. |
| 121 |
/// This produce a new delta that would result in `top_delta` content when |
| 122 |
/// applied on `bottom_delta` base. |
| 123 |
/// |
| 124 |
/// input: A --[bottom_delta]--> B --[top_delta]-->C |
| 125 |
/// output: A --[folded]--> C |
| 126 |
/// |
| 127 |
/// Resulting delta is always "correct", but there is some case where is it not |
| 128 |
/// "minimal" |
| 129 |
/// |
| 130 |
/// For example, if a line is reverted to a previous content (A → B → A), |
| 131 |
/// folding the two last patch would result in a patch requesting an unecessary |
| 132 |
/// replacement of the line with "A". |
| 133 |
fn fold_deltas( |
| 134 |
bottom_delta: &[DeltaPiece], |
| 135 |
top_delta: &[DeltaPiece], |
| 136 |
folded_delta: &mut Vec<DeltaPiece>, |
| 137 |
) -> usize { |
| 138 |
assert!(folded_delta.is_empty()); |
| 139 |
// The amount of bytes added or removed by content from "bottom" so far |
| 140 |
// |
| 141 |
// This value needs to be substracted to "top" patches "start" to compute |
| 142 |
// the "start" position in "base" (and, therefor, how they need to be |
| 143 |
// represented in "folded") |
| 144 |
// |
| 145 |
// When the "bottom" patch: |
| 146 |
// - add N extra bytes of content compared the "base", it goes up by N |
| 147 |
// - remove N bytes of content compared the "base", it goes down by N |
| 148 |
// |
| 149 |
// For a patch "b" from bottom: |
| 150 |
// - start of the resulting area in B is b.start + offset_top_bottom |
| 151 |
// - end of the resulting area in B is b.start + b.size + |
| 152 |
// offset_top_bottom |
| 153 |
// |
| 154 |
// For a patch "t" from top: |
| 155 |
// - start of the replaced area in A is t.start - offset_top_bottom |
| 156 |
// - end of the replaced area in A is t.old_end - offset_top_bottom |
| 157 |
let mut offset_top_bottom: i32 = 0; |
| 158 |
|
| 159 |
// Hold temporary "truncated" value |
| 160 |
let mut tmp_bottom: DeltaPiece; |
| 161 |
let mut tmp_top: DeltaPiece; |
| 162 |
|
| 163 |
// patches from the bottom delta |
| 164 |
let mut bottom_iter = bottom_delta.iter(); |
| 165 |
let mut bottom = bottom_iter.next(); |
| 166 |
|
| 167 |
// patches from the top delta |
| 168 |
let mut top_iter = top_delta.iter(); |
| 169 |
let mut top = top_iter.next(); |
| 170 |
|
| 171 |
while bottom.is_some() || top.is_some() { |
| 172 |
match (bottom, top) { |
| 173 |
// only patches from bottom remains, add them to the result |
| 174 |
(Some(bot), None) => { |
| 175 |
folded_delta.push(*bot); |
| 176 |
for p in bottom_iter { |
| 177 |
folded_delta.push(*p); |
| 178 |
} |
| 179 |
break; |
| 180 |
} |
| 181 |
// only patches from top remains, add them to the result |
| 182 |
(None, Some(t)) => { |
| 183 |
folded_delta.push(t.offsetted(-offset_top_bottom)); |
| 184 |
for p in top_iter { |
| 185 |
folded_delta.push(p.offsetted(-offset_top_bottom)); |
| 186 |
} |
| 187 |
break; |
| 188 |
} |
| 189 |
(Some(bot), Some(t)) |
| 190 |
if (bot.start + bot.size) < (t.start - offset_top_bottom) => |
| 191 |
{ |
| 192 |
folded_delta.push(*bot); |
| 193 |
offset_top_bottom += bot.offset(); |
| 194 |
bottom = bottom_iter.next(); |
| 195 |
} |
| 196 |
(Some(bot), Some(t)) |
| 197 |
if (t.old_end - offset_top_bottom) < bot.start => |
| 198 |
{ |
| 199 |
folded_delta.push(t.offsetted(-offset_top_bottom)); |
| 200 |
top = top_iter.next(); |
| 201 |
} |
| 202 |
(Some(bot), Some(t)) => { |
| 203 |
// There is some overlap between the next patch in "bottom" and |
| 204 |
// the next patch in "top" and they need to be merged. |
| 205 |
|
| 206 |
// start with a zero size area from the first patch |
| 207 |
let start_a = bot.start.min(t.start - offset_top_bottom); |
| 208 |
let mut end_a = start_a; |
| 209 |
|
| 210 |
// check content offset from each layer |
| 211 |
// |
| 212 |
// We don't consume the initial patch yet, as consuming them is |
| 213 |
// no different than consuming the subsequent one. |
| 214 |
let mut local_offset_from_bottom = 0; |
| 215 |
let mut local_offset_from_top = 0; |
| 216 |
|
| 217 |
// We look for successive patches in either list the overlap |
| 218 |
// with the current one, and merge them (extend end points and |
| 219 |
// adapt offsets) |
| 220 |
|
| 221 |
loop { |
| 222 |
let (current_bottom, current_top) = match (bottom, top) { |
| 223 |
(None, None) => break, |
| 224 |
(Some(_), None) => { |
| 225 |
while match bottom { |
| 226 |
None => false, |
| 227 |
Some(b) => b.start <= end_a, |
| 228 |
} { |
| 229 |
let b = bottom.unwrap(); |
| 230 |
end_a = end_a.max(b.old_end); |
| 231 |
local_offset_from_bottom += b.offset(); |
| 232 |
bottom = bottom_iter.next(); |
| 233 |
} |
| 234 |
break; |
| 235 |
} |
| 236 |
(None, Some(_)) => { |
| 237 |
let local_offset = |
| 238 |
offset_top_bottom + local_offset_from_bottom; |
| 239 |
while match top { |
| 240 |
None => false, |
| 241 |
Some(t) => t.start - local_offset <= end_a, |
| 242 |
} { |
| 243 |
let t = top.unwrap(); |
| 244 |
end_a = end_a.max(t.old_end - local_offset); |
| 245 |
local_offset_from_top += t.offset(); |
| 246 |
top = top_iter.next(); |
| 247 |
} |
| 248 |
break; |
| 249 |
} |
| 250 |
(Some(bot), Some(top)) => (bot, top), |
| 251 |
}; |
| 252 |
|
| 253 |
let start_bottom = current_bottom.start; |
| 254 |
let start_top = current_top.start |
| 255 |
- offset_top_bottom |
| 256 |
- local_offset_from_bottom; |
| 257 |
|
| 258 |
if start_bottom > end_a && start_top > end_a { |
| 259 |
// No remaining overlap, go to conclusion of this merge |
| 260 |
break; |
| 261 |
} |
| 262 |
|
| 263 |
// check if any patch is ahead of the other and by how |
| 264 |
// much. |
| 265 |
// |
| 266 |
// If one is ahead of the other, we process it first. |
| 267 |
// Either by consuming it entirely if |
| 268 |
// there is no overlap with the |
| 269 |
// next one, or by processing the part ahead. |
| 270 |
// |
| 271 |
// If the two patch start at the same level, consume the |
| 272 |
// one contained in the other |
| 273 |
match start_bottom - start_top { |
| 274 |
rd if rd < 0 => { |
| 275 |
let d = -rd; |
| 276 |
let size_a = |
| 277 |
current_bottom.old_end - current_bottom.start; |
| 278 |
if size_a <= d { |
| 279 |
// "top" does not overlap with the current |
| 280 |
// patch, merge wholly |
| 281 |
end_a = end_a.max(current_bottom.old_end); |
| 282 |
local_offset_from_bottom += |
| 283 |
current_bottom.offset(); |
| 284 |
bottom = bottom_iter.next(); |
| 285 |
} else { |
| 286 |
// truncate the start to align it with the |
| 287 |
// other one |
| 288 |
end_a += d; |
| 289 |
local_offset_from_bottom += |
| 290 |
0.min(current_bottom.size - d); |
| 291 |
tmp_bottom = current_bottom.truncated(d); |
| 292 |
bottom = Some(&tmp_bottom); |
| 293 |
} |
| 294 |
} |
| 295 |
d if d > 0 => { |
| 296 |
let size_b = |
| 297 |
current_top.old_end - current_top.start; |
| 298 |
if size_b <= d { |
| 299 |
// "bottom" patch does not overlap with the |
| 300 |
// current patch, merge wholly |
| 301 |
end_a = end_a.max( |
| 302 |
current_top.old_end |
| 303 |
- offset_top_bottom |
| 304 |
- local_offset_from_bottom, |
| 305 |
); |
| 306 |
local_offset_from_top += current_top.offset(); |
| 307 |
top = top_iter.next(); |
| 308 |
} else { |
| 309 |
// truncate the current "top" patch top align |
| 310 |
// it with the "bottom" one |
| 311 |
end_a += d; |
| 312 |
local_offset_from_top += |
| 313 |
0.min(current_top.size - d); |
| 314 |
tmp_top = current_top.truncated(d); |
| 315 |
top = Some(&tmp_top); |
| 316 |
} |
| 317 |
} |
| 318 |
_ => { |
| 319 |
debug_assert_eq!(start_bottom, start_top); |
| 320 |
// Check if the "top" patch only replace "bottom" |
| 321 |
// content |
| 322 |
let size_b = |
| 323 |
current_top.old_end - current_top.start; |
| 324 |
if size_b <= current_bottom.size { |
| 325 |
// "top" only replace bottom content, adjust |
| 326 |
// the |
| 327 |
// final patch size and rely on "bottom" to |
| 328 |
// update |
| 329 |
// the `end_a` value. |
| 330 |
local_offset_from_top += current_top.offset(); |
| 331 |
top = top_iter.next(); |
| 332 |
} else { |
| 333 |
// "top" replace more that the current "bottom" |
| 334 |
// data, |
| 335 |
// truncate the part associated with that |
| 336 |
// "bottom" |
| 337 |
// patch and consume it. |
| 338 |
local_offset_from_top += 0.min( |
| 339 |
current_top.size - current_bottom.size, |
| 340 |
); |
| 341 |
tmp_top = |
| 342 |
current_top.truncated(current_bottom.size); |
| 343 |
top = Some(&tmp_top); |
| 344 |
|
| 345 |
// consume the "bottom" patch |
| 346 |
end_a += current_bottom.old_end |
| 347 |
- current_bottom.start; |
| 348 |
local_offset_from_bottom += |
| 349 |
current_bottom.offset(); |
| 350 |
bottom = bottom_iter.next(); |
| 351 |
} |
| 352 |
} |
| 353 |
}; |
| 354 |
} |
| 355 |
// all contiguous patches are now merged, we can register |
| 356 |
// this patch coordinates are relative |
| 357 |
// to A, the size is relative to C |
| 358 |
let size = end_a - start_a |
| 359 |
+ local_offset_from_top |
| 360 |
+ local_offset_from_bottom; |
| 361 |
folded_delta.push(DeltaPiece { |
| 362 |
start: start_a, |
| 363 |
old_end: end_a, |
| 364 |
size, |
| 365 |
}); |
| 366 |
offset_top_bottom += local_offset_from_bottom; |
| 367 |
} |
| 368 |
_ => unreachable!(), |
| 369 |
} |
| 370 |
} |
| 371 |
folded_delta.iter().map(|p| (p.size as usize) + H_SIZE).sum() |
| 372 |
} |
| 373 |
|
| 374 |
/// Parse a binary chunk into a Delta |
| 375 |
/// |
| 376 |
/// return the storage size of the parsed delta |
| 377 |
/// (which should be the size of `chunk` anyway) |
| 378 |
fn chunk_to_delta(delta: &mut Vec<DeltaPiece>, chunk: &[u8]) -> usize { |
| 379 |
assert!(delta.is_empty()); |
| 380 |
let mut offset = 0; |
| 381 |
let mut delta_size = 0; |
| 382 |
|
| 383 |
while (offset + H_SIZE) <= chunk.len() { |
| 384 |
let start = i32::from_be_bytes( |
| 385 |
chunk[offset..offset + HP_SIZE].try_into().unwrap(), |
| 386 |
); |
| 387 |
offset += HP_SIZE; |
| 388 |
let old_end = i32::from_be_bytes( |
| 389 |
chunk[offset..offset + HP_SIZE].try_into().unwrap(), |
| 390 |
); |
| 391 |
offset += HP_SIZE; |
| 392 |
let size = i32::from_be_bytes( |
| 393 |
chunk[offset..offset + HP_SIZE].try_into().unwrap(), |
| 394 |
); |
| 395 |
offset += HP_SIZE; |
| 396 |
if start < 0 |
| 397 |
|| old_end < 0 |
| 398 |
|| start > old_end |
| 399 |
|| size < 0 |
| 400 |
|| (offset + size as usize) > chunk.len() |
| 401 |
{ |
| 402 |
// inconsistent patch ‽ |
| 403 |
break; |
| 404 |
} |
| 405 |
delta.push(DeltaPiece { start, old_end, size }); |
| 406 |
delta_size = delta_size + H_SIZE + size as usize; |
| 407 |
offset += size as usize; |
| 408 |
} |
| 409 |
assert!(chunk.len() == delta_size); |
| 410 |
delta_size |
| 411 |
} |
| 412 |
|
| 413 |
#[cfg(test)] |
| 414 |
mod tests { |
| 415 |
|
| 416 |
use super::*; |
| 417 |
|
| 418 |
fn delta_size(delta: &[DeltaPiece]) -> usize { |
| 419 |
delta.iter().map(|p| p.size as usize + H_SIZE).sum() |
| 420 |
} |
| 421 |
|
| 422 |
/// Check that two disjoint patch does not get merged |
| 423 |
/// |
| 424 |
/// (with patches not doing any offsets) |
| 425 |
#[test] |
| 426 |
fn test_disjoint_patches() { |
| 427 |
let low_delta = vec![DeltaPiece { start: 0, old_end: 10, size: 10 }]; |
| 428 |
let high_delta = vec![DeltaPiece { start: 20, old_end: 30, size: 10 }]; |
| 429 |
let mut folded_delta = vec![]; |
| 430 |
|
| 431 |
assert_eq!(delta_size(&low_delta), 22); |
| 432 |
assert_eq!(delta_size(&high_delta), 22); |
| 433 |
|
| 434 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 435 |
assert_eq!(delta_size(&folded_delta), 44); |
| 436 |
assert_eq!( |
| 437 |
folded_delta, |
| 438 |
vec![ |
| 439 |
DeltaPiece { start: 0, old_end: 10, size: 10 }, |
| 440 |
DeltaPiece { start: 20, old_end: 30, size: 10 }, |
| 441 |
] |
| 442 |
) |
| 443 |
} |
| 444 |
|
| 445 |
/// Check that two consecutive patch do get merged |
| 446 |
/// |
| 447 |
/// (with patches not doing any offsets) |
| 448 |
#[test] |
| 449 |
fn test_consecutive_patches() { |
| 450 |
let low_delta = vec![DeltaPiece { start: 0, old_end: 10, size: 10 }]; |
| 451 |
let high_delta = vec![DeltaPiece { start: 10, old_end: 20, size: 10 }]; |
| 452 |
let mut folded_delta = vec![]; |
| 453 |
|
| 454 |
assert_eq!(delta_size(&low_delta), 22); |
| 455 |
assert_eq!(delta_size(&high_delta), 22); |
| 456 |
|
| 457 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 458 |
assert_eq!(delta_size(&folded_delta), 32); |
| 459 |
assert_eq!( |
| 460 |
folded_delta, |
| 461 |
vec![DeltaPiece { start: 0, old_end: 20, size: 20 },] |
| 462 |
) |
| 463 |
} |
| 464 |
|
| 465 |
/// Merging overlaping patches |
| 466 |
/// |
| 467 |
/// (without offset) |
| 468 |
#[test] |
| 469 |
fn test_overlapping_patches() { |
| 470 |
let low_delta = vec![DeltaPiece { start: 0, old_end: 10, size: 10 }]; |
| 471 |
let high_delta = vec![DeltaPiece { start: 5, old_end: 15, size: 10 }]; |
| 472 |
let mut folded_delta = vec![]; |
| 473 |
|
| 474 |
assert_eq!(delta_size(&low_delta), 22); |
| 475 |
assert_eq!(delta_size(&high_delta), 22); |
| 476 |
|
| 477 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 478 |
assert_eq!(delta_size(&folded_delta), 27); |
| 479 |
assert_eq!( |
| 480 |
folded_delta, |
| 481 |
vec![DeltaPiece { start: 0, old_end: 15, size: 15 },] |
| 482 |
) |
| 483 |
} |
| 484 |
|
| 485 |
/// Merging a chain of overlapping patches |
| 486 |
/// |
| 487 |
/// (without offsets) |
| 488 |
#[test] |
| 489 |
fn test_overlapping_chain_or_patches() { |
| 490 |
let low_delta = vec![ |
| 491 |
DeltaPiece { start: 0, old_end: 10, size: 10 }, |
| 492 |
DeltaPiece { start: 15, old_end: 25, size: 10 }, |
| 493 |
DeltaPiece { start: 30, old_end: 40, size: 10 }, |
| 494 |
]; |
| 495 |
let high_delta = vec![ |
| 496 |
DeltaPiece { start: 8, old_end: 18, size: 10 }, |
| 497 |
DeltaPiece { start: 23, old_end: 33, size: 10 }, |
| 498 |
]; |
| 499 |
let mut folded_delta = vec![]; |
| 500 |
|
| 501 |
assert_eq!(delta_size(&low_delta), 66); |
| 502 |
assert_eq!(delta_size(&high_delta), 44); |
| 503 |
|
| 504 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 505 |
assert_eq!( |
| 506 |
folded_delta, |
| 507 |
vec![DeltaPiece { start: 0, old_end: 40, size: 40 },] |
| 508 |
); |
| 509 |
assert_eq!(delta_size(&folded_delta), 52); |
| 510 |
} |
| 511 |
|
| 512 |
/// Merge patch that are super set of the others |
| 513 |
/// |
| 514 |
/// () |
| 515 |
#[test] |
| 516 |
fn test_superset_patches() { |
| 517 |
let low_delta = vec![ |
| 518 |
DeltaPiece { start: 0, old_end: 30, size: 30 }, |
| 519 |
DeltaPiece { start: 50, old_end: 60, size: 10 }, |
| 520 |
]; |
| 521 |
let high_delta = vec![ |
| 522 |
DeltaPiece { start: 10, old_end: 20, size: 10 }, |
| 523 |
DeltaPiece { start: 40, old_end: 70, size: 30 }, |
| 524 |
]; |
| 525 |
let mut folded_delta = vec![]; |
| 526 |
|
| 527 |
assert_eq!(delta_size(&low_delta), 64); |
| 528 |
assert_eq!(delta_size(&high_delta), 64); |
| 529 |
|
| 530 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 531 |
assert_eq!( |
| 532 |
folded_delta, |
| 533 |
vec![ |
| 534 |
DeltaPiece { start: 0, old_end: 30, size: 30 }, |
| 535 |
DeltaPiece { start: 40, old_end: 70, size: 30 }, |
| 536 |
] |
| 537 |
); |
| 538 |
assert_eq!(delta_size(&folded_delta), 84); |
| 539 |
} |
| 540 |
|
| 541 |
/// Disjoint patches |
| 542 |
/// |
| 543 |
/// (with offsets) |
| 544 |
#[test] |
| 545 |
fn test_disjoint_patches_offsets() { |
| 546 |
let low_delta = vec![DeltaPiece { start: 0, old_end: 10, size: 5 }]; |
| 547 |
let high_delta = vec![DeltaPiece { start: 10, old_end: 15, size: 10 }]; |
| 548 |
let mut folded_delta = vec![]; |
| 549 |
|
| 550 |
assert_eq!(delta_size(&low_delta), 17); |
| 551 |
assert_eq!(delta_size(&high_delta), 22); |
| 552 |
|
| 553 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 554 |
assert_eq!( |
| 555 |
folded_delta, |
| 556 |
vec![ |
| 557 |
DeltaPiece { start: 0, old_end: 10, size: 5 }, |
| 558 |
DeltaPiece { start: 15, old_end: 20, size: 10 }, |
| 559 |
] |
| 560 |
); |
| 561 |
assert_eq!(delta_size(&folded_delta), 39); |
| 562 |
} |
| 563 |
|
| 564 |
/// Merge consecutive patches with offsets |
| 565 |
/// |
| 566 |
/// (with offsets) |
| 567 |
#[test] |
| 568 |
fn test_consecutive_patches_offsets() { |
| 569 |
let low_delta = vec![DeltaPiece { start: 0, old_end: 10, size: 15 }]; |
| 570 |
let high_delta = vec![DeltaPiece { start: 15, old_end: 20, size: 7 }]; |
| 571 |
let mut folded_delta = vec![]; |
| 572 |
|
| 573 |
assert_eq!(delta_size(&low_delta), 27); |
| 574 |
assert_eq!(delta_size(&high_delta), 19); |
| 575 |
|
| 576 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 577 |
assert_eq!( |
| 578 |
folded_delta, |
| 579 |
vec![DeltaPiece { start: 0, old_end: 15, size: 22 },] |
| 580 |
); |
| 581 |
assert_eq!(delta_size(&folded_delta), 34); |
| 582 |
} |
| 583 |
|
| 584 |
/// Merge overlapping patches |
| 585 |
/// |
| 586 |
/// (with offsets) |
| 587 |
#[test] |
| 588 |
fn test_overlapping_patches_offsets() { |
| 589 |
let low_delta = vec![DeltaPiece { start: 10, old_end: 20, size: 20 }]; |
| 590 |
let high_delta = vec![DeltaPiece { start: 25, old_end: 35, size: 3 }]; |
| 591 |
let mut folded_delta = vec![]; |
| 592 |
|
| 593 |
assert_eq!(delta_size(&low_delta), 32); |
| 594 |
assert_eq!(delta_size(&high_delta), 15); |
| 595 |
|
| 596 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 597 |
assert_eq!( |
| 598 |
folded_delta, |
| 599 |
vec![DeltaPiece { start: 10, old_end: 25, size: 18 },] |
| 600 |
); |
| 601 |
assert_eq!(delta_size(&folded_delta), 30); |
| 602 |
} |
| 603 |
|
| 604 |
/// Merge chain of patches |
| 605 |
/// |
| 606 |
/// (with offsets) |
| 607 |
#[test] |
| 608 |
fn test_overlapping_chain_of_patches_offsets() { |
| 609 |
let low_delta = vec![ |
| 610 |
DeltaPiece { start: 0, old_end: 0, size: 5 }, |
| 611 |
DeltaPiece { start: 5, old_end: 10, size: 15 }, |
| 612 |
DeltaPiece { start: 15, old_end: 20, size: 0 }, |
| 613 |
DeltaPiece { start: 30, old_end: 35, size: 15 }, |
| 614 |
DeltaPiece { start: 40, old_end: 80, size: 10 }, |
| 615 |
]; |
| 616 |
let high_delta = vec![ |
| 617 |
DeltaPiece { start: 18, old_end: 45, size: 5 }, |
| 618 |
DeltaPiece { start: 50, old_end: 65, size: 30 }, |
| 619 |
DeltaPiece { start: 75, old_end: 75, size: 5 }, |
| 620 |
]; |
| 621 |
let mut folded_delta = vec![]; |
| 622 |
|
| 623 |
assert_eq!(delta_size(&low_delta), 105); |
| 624 |
assert_eq!(delta_size(&high_delta), 76); |
| 625 |
|
| 626 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 627 |
assert_eq!( |
| 628 |
folded_delta, |
| 629 |
vec![ |
| 630 |
DeltaPiece { start: 0, old_end: 0, size: 5 }, |
| 631 |
DeltaPiece { start: 5, old_end: 82, size: 55 }, |
| 632 |
DeltaPiece { start: 85, old_end: 85, size: 5 }, |
| 633 |
] |
| 634 |
); |
| 635 |
assert_eq!(delta_size(&folded_delta), 101); |
| 636 |
} |
| 637 |
|
| 638 |
/// Merge patches that are superset of others |
| 639 |
/// |
| 640 |
/// (with offsets) |
| 641 |
#[test] |
| 642 |
fn test_superset_patches_offsets() { |
| 643 |
let low_delta = vec![ |
| 644 |
DeltaPiece { start: 5, old_end: 10, size: 15 }, |
| 645 |
DeltaPiece { start: 20, old_end: 50, size: 10 }, |
| 646 |
DeltaPiece { start: 80, old_end: 90, size: 40 }, |
| 647 |
]; |
| 648 |
let high_delta = vec![ |
| 649 |
DeltaPiece { start: 33, old_end: 38, size: 65 }, |
| 650 |
DeltaPiece { start: 65, old_end: 120, size: 10 }, |
| 651 |
DeltaPiece { start: 150, old_end: 200, size: 66 }, |
| 652 |
]; |
| 653 |
let mut folded_delta = vec![]; |
| 654 |
|
| 655 |
assert_eq!(delta_size(&low_delta), 101); |
| 656 |
assert_eq!(delta_size(&high_delta), 177); |
| 657 |
|
| 658 |
fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 659 |
assert_eq!( |
| 660 |
folded_delta, |
| 661 |
vec![ |
| 662 |
DeltaPiece { start: 5, old_end: 10, size: 15 }, |
| 663 |
DeltaPiece { start: 20, old_end: 50, size: 70 }, |
| 664 |
DeltaPiece { start: 75, old_end: 100, size: 10 }, |
| 665 |
DeltaPiece { start: 130, old_end: 180, size: 66 }, |
| 666 |
] |
| 667 |
); |
| 668 |
assert_eq!(delta_size(&folded_delta), 209); |
| 669 |
} |
| 670 |
|
| 671 |
/// A large test coming from an error in the .t |
| 672 |
#[test] |
| 673 |
fn test_large() { |
| 674 |
let low_delta = vec![ |
| 675 |
DeltaPiece { start: 0, old_end: 1651, size: 1652 }, |
| 676 |
DeltaPiece { start: 9868, old_end: 9901, size: 33 }, |
| 677 |
DeltaPiece { start: 19768, old_end: 19801, size: 33 }, |
| 678 |
DeltaPiece { start: 29668, old_end: 29701, size: 33 }, |
| 679 |
DeltaPiece { start: 39568, old_end: 39601, size: 33 }, |
| 680 |
DeltaPiece { start: 49468, old_end: 49501, size: 33 }, |
| 681 |
DeltaPiece { start: 59368, old_end: 59401, size: 33 }, |
| 682 |
DeltaPiece { start: 69268, old_end: 69301, size: 33 }, |
| 683 |
DeltaPiece { start: 79168, old_end: 79201, size: 33 }, |
| 684 |
DeltaPiece { start: 89068, old_end: 89101, size: 33 }, |
| 685 |
DeltaPiece { start: 98968, old_end: 99001, size: 33 }, |
| 686 |
DeltaPiece { start: 108868, old_end: 108901, size: 33 }, |
| 687 |
DeltaPiece { start: 118768, old_end: 118801, size: 33 }, |
| 688 |
DeltaPiece { start: 128668, old_end: 128701, size: 33 }, |
| 689 |
DeltaPiece { start: 138568, old_end: 138601, size: 33 }, |
| 690 |
DeltaPiece { start: 148468, old_end: 148501, size: 33 }, |
| 691 |
DeltaPiece { start: 158368, old_end: 158401, size: 33 }, |
| 692 |
DeltaPiece { start: 168268, old_end: 168301, size: 33 }, |
| 693 |
DeltaPiece { start: 178168, old_end: 178201, size: 33 }, |
| 694 |
DeltaPiece { start: 188068, old_end: 188101, size: 33 }, |
| 695 |
DeltaPiece { start: 197968, old_end: 198001, size: 33 }, |
| 696 |
DeltaPiece { start: 207868, old_end: 207901, size: 33 }, |
| 697 |
DeltaPiece { start: 217768, old_end: 217801, size: 33 }, |
| 698 |
DeltaPiece { start: 227668, old_end: 227701, size: 33 }, |
| 699 |
DeltaPiece { start: 237568, old_end: 237601, size: 33 }, |
| 700 |
DeltaPiece { start: 247468, old_end: 247501, size: 33 }, |
| 701 |
DeltaPiece { start: 257368, old_end: 257401, size: 33 }, |
| 702 |
DeltaPiece { start: 267268, old_end: 267301, size: 33 }, |
| 703 |
DeltaPiece { start: 277168, old_end: 277201, size: 33 }, |
| 704 |
DeltaPiece { start: 287068, old_end: 287101, size: 33 }, |
| 705 |
DeltaPiece { start: 296968, old_end: 297001, size: 33 }, |
| 706 |
DeltaPiece { start: 306868, old_end: 306901, size: 33 }, |
| 707 |
DeltaPiece { start: 316768, old_end: 316801, size: 33 }, |
| 708 |
DeltaPiece { start: 326668, old_end: 326701, size: 33 }, |
| 709 |
DeltaPiece { start: 336568, old_end: 336601, size: 33 }, |
| 710 |
]; |
| 711 |
let high_delta = vec![ |
| 712 |
DeltaPiece { start: 0, old_end: 1652, size: 1652 }, |
| 713 |
DeltaPiece { start: 9869, old_end: 9902, size: 33 }, |
| 714 |
DeltaPiece { start: 29669, old_end: 29702, size: 33 }, |
| 715 |
DeltaPiece { start: 49469, old_end: 49502, size: 33 }, |
| 716 |
DeltaPiece { start: 59369, old_end: 59402, size: 33 }, |
| 717 |
DeltaPiece { start: 69269, old_end: 69302, size: 33 }, |
| 718 |
DeltaPiece { start: 69335, old_end: 69368, size: 33 }, |
| 719 |
DeltaPiece { start: 79169, old_end: 79202, size: 33 }, |
| 720 |
DeltaPiece { start: 98969, old_end: 99002, size: 33 }, |
| 721 |
DeltaPiece { start: 108869, old_end: 108902, size: 33 }, |
| 722 |
DeltaPiece { start: 118769, old_end: 118802, size: 33 }, |
| 723 |
DeltaPiece { start: 138569, old_end: 138602, size: 33 }, |
| 724 |
DeltaPiece { start: 158369, old_end: 158402, size: 33 }, |
| 725 |
DeltaPiece { start: 168269, old_end: 168302, size: 33 }, |
| 726 |
DeltaPiece { start: 178169, old_end: 178202, size: 33 }, |
| 727 |
DeltaPiece { start: 178235, old_end: 178268, size: 33 }, |
| 728 |
DeltaPiece { start: 188069, old_end: 188102, size: 33 }, |
| 729 |
DeltaPiece { start: 207869, old_end: 207902, size: 33 }, |
| 730 |
DeltaPiece { start: 217769, old_end: 217802, size: 33 }, |
| 731 |
DeltaPiece { start: 227669, old_end: 227702, size: 33 }, |
| 732 |
DeltaPiece { start: 247469, old_end: 247502, size: 33 }, |
| 733 |
DeltaPiece { start: 267269, old_end: 267302, size: 33 }, |
| 734 |
DeltaPiece { start: 277169, old_end: 277202, size: 33 }, |
| 735 |
DeltaPiece { start: 287069, old_end: 287102, size: 33 }, |
| 736 |
DeltaPiece { start: 287135, old_end: 287168, size: 33 }, |
| 737 |
DeltaPiece { start: 296969, old_end: 297002, size: 33 }, |
| 738 |
DeltaPiece { start: 316769, old_end: 316802, size: 33 }, |
| 739 |
DeltaPiece { start: 326669, old_end: 326702, size: 33 }, |
| 740 |
DeltaPiece { start: 336569, old_end: 336602, size: 33 }, |
| 741 |
]; |
| 742 |
let mut folded_delta = vec![]; |
| 743 |
|
| 744 |
assert_eq!(delta_size(&low_delta), 3194); |
| 745 |
assert_eq!(delta_size(&high_delta), 2924); |
| 746 |
|
| 747 |
let size = fold_deltas(&low_delta, &high_delta, &mut folded_delta); |
| 748 |
assert_eq!( |
| 749 |
folded_delta, |
| 750 |
vec![ |
| 751 |
DeltaPiece { start: 0, old_end: 1651, size: 1652 }, |
| 752 |
DeltaPiece { start: 9868, old_end: 9901, size: 33 }, |
| 753 |
DeltaPiece { start: 19768, old_end: 19801, size: 33 }, |
| 754 |
DeltaPiece { start: 29668, old_end: 29701, size: 33 }, |
| 755 |
DeltaPiece { start: 39568, old_end: 39601, size: 33 }, |
| 756 |
DeltaPiece { start: 49468, old_end: 49501, size: 33 }, |
| 757 |
DeltaPiece { start: 59368, old_end: 59401, size: 33 }, |
| 758 |
DeltaPiece { start: 69268, old_end: 69301, size: 33 }, |
| 759 |
DeltaPiece { start: 69334, old_end: 69367, size: 33 }, |
| 760 |
DeltaPiece { start: 79168, old_end: 79201, size: 33 }, |
| 761 |
DeltaPiece { start: 89068, old_end: 89101, size: 33 }, |
| 762 |
DeltaPiece { start: 98968, old_end: 99001, size: 33 }, |
| 763 |
DeltaPiece { start: 108868, old_end: 108901, size: 33 }, |
| 764 |
DeltaPiece { start: 118768, old_end: 118801, size: 33 }, |
| 765 |
DeltaPiece { start: 128668, old_end: 128701, size: 33 }, |
| 766 |
DeltaPiece { start: 138568, old_end: 138601, size: 33 }, |
| 767 |
DeltaPiece { start: 148468, old_end: 148501, size: 33 }, |
| 768 |
DeltaPiece { start: 158368, old_end: 158401, size: 33 }, |
| 769 |
DeltaPiece { start: 168268, old_end: 168301, size: 33 }, |
| 770 |
DeltaPiece { start: 178168, old_end: 178201, size: 33 }, |
| 771 |
DeltaPiece { start: 178234, old_end: 178267, size: 33 }, |
| 772 |
DeltaPiece { start: 188068, old_end: 188101, size: 33 }, |
| 773 |
DeltaPiece { start: 197968, old_end: 198001, size: 33 }, |
| 774 |
DeltaPiece { start: 207868, old_end: 207901, size: 33 }, |
| 775 |
DeltaPiece { start: 217768, old_end: 217801, size: 33 }, |
| 776 |
DeltaPiece { start: 227668, old_end: 227701, size: 33 }, |
| 777 |
DeltaPiece { start: 237568, old_end: 237601, size: 33 }, |
| 778 |
DeltaPiece { start: 247468, old_end: 247501, size: 33 }, |
| 779 |
DeltaPiece { start: 257368, old_end: 257401, size: 33 }, |
| 780 |
DeltaPiece { start: 267268, old_end: 267301, size: 33 }, |
| 781 |
DeltaPiece { start: 277168, old_end: 277201, size: 33 }, |
| 782 |
DeltaPiece { start: 287068, old_end: 287101, size: 33 }, |
| 783 |
DeltaPiece { start: 287134, old_end: 287167, size: 33 }, |
| 784 |
DeltaPiece { start: 296968, old_end: 297001, size: 33 }, |
| 785 |
DeltaPiece { start: 306868, old_end: 306901, size: 33 }, |
| 786 |
DeltaPiece { start: 316768, old_end: 316801, size: 33 }, |
| 787 |
DeltaPiece { start: 326668, old_end: 326701, size: 33 }, |
| 788 |
DeltaPiece { start: 336568, old_end: 336601, size: 33 }, |
| 789 |
] |
| 790 |
); |
| 791 |
assert_eq!(delta_size(&folded_delta), 3329); |
| 792 |
assert_eq!(delta_size(&folded_delta), size); |
| 793 |
} |
| 794 |
} |