##// END OF EJS Templates
snapshot: consider all snapshots in the parents' chains...
Boris Feld -
r39530:e72130f5 default
parent child Browse files
Show More
@@ -1,817 +1,827 b''
1 1 # revlogdeltas.py - Logic around delta computation for revlog
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 # Copyright 2018 Octobus <contact@octobus.net>
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2 or any later version.
8 8 """Helper class to compute deltas stored inside revlogs"""
9 9
10 10 from __future__ import absolute_import
11 11
12 12 import collections
13 13 import heapq
14 14 import struct
15 15
16 16 # import stuff from node for others to import from revlog
17 17 from ..node import (
18 18 nullrev,
19 19 )
20 20 from ..i18n import _
21 21
22 22 from .constants import (
23 23 REVIDX_ISCENSORED,
24 24 REVIDX_RAWTEXT_CHANGING_FLAGS,
25 25 )
26 26
27 27 from ..thirdparty import (
28 28 attr,
29 29 )
30 30
31 31 from .. import (
32 32 error,
33 33 mdiff,
34 34 )
35 35
36 36 RevlogError = error.RevlogError
37 37 CensoredNodeError = error.CensoredNodeError
38 38
39 39 # maximum <delta-chain-data>/<revision-text-length> ratio
40 40 LIMIT_DELTA2TEXT = 2
41 41
42 42 class _testrevlog(object):
43 43 """minimalist fake revlog to use in doctests"""
44 44
45 45 def __init__(self, data, density=0.5, mingap=0):
46 46 """data is an list of revision payload boundaries"""
47 47 self._data = data
48 48 self._srdensitythreshold = density
49 49 self._srmingapsize = mingap
50 50
51 51 def start(self, rev):
52 52 if rev == 0:
53 53 return 0
54 54 return self._data[rev - 1]
55 55
56 56 def end(self, rev):
57 57 return self._data[rev]
58 58
59 59 def length(self, rev):
60 60 return self.end(rev) - self.start(rev)
61 61
62 62 def __len__(self):
63 63 return len(self._data)
64 64
65 65 def slicechunk(revlog, revs, deltainfo=None, targetsize=None):
66 66 """slice revs to reduce the amount of unrelated data to be read from disk.
67 67
68 68 ``revs`` is sliced into groups that should be read in one time.
69 69 Assume that revs are sorted.
70 70
71 71 The initial chunk is sliced until the overall density (payload/chunks-span
72 72 ratio) is above `revlog._srdensitythreshold`. No gap smaller than
73 73 `revlog._srmingapsize` is skipped.
74 74
75 75 If `targetsize` is set, no chunk larger than `targetsize` will be yield.
76 76 For consistency with other slicing choice, this limit won't go lower than
77 77 `revlog._srmingapsize`.
78 78
79 79 If individual revisions chunk are larger than this limit, they will still
80 80 be raised individually.
81 81
82 82 >>> revlog = _testrevlog([
83 83 ... 5, #00 (5)
84 84 ... 10, #01 (5)
85 85 ... 12, #02 (2)
86 86 ... 12, #03 (empty)
87 87 ... 27, #04 (15)
88 88 ... 31, #05 (4)
89 89 ... 31, #06 (empty)
90 90 ... 42, #07 (11)
91 91 ... 47, #08 (5)
92 92 ... 47, #09 (empty)
93 93 ... 48, #10 (1)
94 94 ... 51, #11 (3)
95 95 ... 74, #12 (23)
96 96 ... 85, #13 (11)
97 97 ... 86, #14 (1)
98 98 ... 91, #15 (5)
99 99 ... ])
100 100
101 101 >>> list(slicechunk(revlog, list(range(16))))
102 102 [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]
103 103 >>> list(slicechunk(revlog, [0, 15]))
104 104 [[0], [15]]
105 105 >>> list(slicechunk(revlog, [0, 11, 15]))
106 106 [[0], [11], [15]]
107 107 >>> list(slicechunk(revlog, [0, 11, 13, 15]))
108 108 [[0], [11, 13, 15]]
109 109 >>> list(slicechunk(revlog, [1, 2, 3, 5, 8, 10, 11, 14]))
110 110 [[1, 2], [5, 8, 10, 11], [14]]
111 111
112 112 Slicing with a maximum chunk size
113 113 >>> list(slicechunk(revlog, [0, 11, 13, 15], targetsize=15))
114 114 [[0], [11], [13], [15]]
115 115 >>> list(slicechunk(revlog, [0, 11, 13, 15], targetsize=20))
116 116 [[0], [11], [13, 15]]
117 117 """
118 118 if targetsize is not None:
119 119 targetsize = max(targetsize, revlog._srmingapsize)
120 120 # targetsize should not be specified when evaluating delta candidates:
121 121 # * targetsize is used to ensure we stay within specification when reading,
122 122 # * deltainfo is used to pick are good delta chain when writing.
123 123 if not (deltainfo is None or targetsize is None):
124 124 msg = 'cannot use `targetsize` with a `deltainfo`'
125 125 raise error.ProgrammingError(msg)
126 126 for chunk in _slicechunktodensity(revlog, revs,
127 127 deltainfo,
128 128 revlog._srdensitythreshold,
129 129 revlog._srmingapsize):
130 130 for subchunk in _slicechunktosize(revlog, chunk, targetsize):
131 131 yield subchunk
132 132
133 133 def _slicechunktosize(revlog, revs, targetsize=None):
134 134 """slice revs to match the target size
135 135
136 136 This is intended to be used on chunk that density slicing selected by that
137 137 are still too large compared to the read garantee of revlog. This might
138 138 happens when "minimal gap size" interrupted the slicing or when chain are
139 139 built in a way that create large blocks next to each other.
140 140
141 141 >>> revlog = _testrevlog([
142 142 ... 3, #0 (3)
143 143 ... 5, #1 (2)
144 144 ... 6, #2 (1)
145 145 ... 8, #3 (2)
146 146 ... 8, #4 (empty)
147 147 ... 11, #5 (3)
148 148 ... 12, #6 (1)
149 149 ... 13, #7 (1)
150 150 ... 14, #8 (1)
151 151 ... ])
152 152
153 153 Cases where chunk is already small enough
154 154 >>> list(_slicechunktosize(revlog, [0], 3))
155 155 [[0]]
156 156 >>> list(_slicechunktosize(revlog, [6, 7], 3))
157 157 [[6, 7]]
158 158 >>> list(_slicechunktosize(revlog, [0], None))
159 159 [[0]]
160 160 >>> list(_slicechunktosize(revlog, [6, 7], None))
161 161 [[6, 7]]
162 162
163 163 cases where we need actual slicing
164 164 >>> list(_slicechunktosize(revlog, [0, 1], 3))
165 165 [[0], [1]]
166 166 >>> list(_slicechunktosize(revlog, [1, 3], 3))
167 167 [[1], [3]]
168 168 >>> list(_slicechunktosize(revlog, [1, 2, 3], 3))
169 169 [[1, 2], [3]]
170 170 >>> list(_slicechunktosize(revlog, [3, 5], 3))
171 171 [[3], [5]]
172 172 >>> list(_slicechunktosize(revlog, [3, 4, 5], 3))
173 173 [[3], [5]]
174 174 >>> list(_slicechunktosize(revlog, [5, 6, 7, 8], 3))
175 175 [[5], [6, 7, 8]]
176 176 >>> list(_slicechunktosize(revlog, [0, 1, 2, 3, 4, 5, 6, 7, 8], 3))
177 177 [[0], [1, 2], [3], [5], [6, 7, 8]]
178 178
179 179 Case with too large individual chunk (must return valid chunk)
180 180 >>> list(_slicechunktosize(revlog, [0, 1], 2))
181 181 [[0], [1]]
182 182 >>> list(_slicechunktosize(revlog, [1, 3], 1))
183 183 [[1], [3]]
184 184 >>> list(_slicechunktosize(revlog, [3, 4, 5], 2))
185 185 [[3], [5]]
186 186 """
187 187 assert targetsize is None or 0 <= targetsize
188 188 if targetsize is None or segmentspan(revlog, revs) <= targetsize:
189 189 yield revs
190 190 return
191 191
192 192 startrevidx = 0
193 193 startdata = revlog.start(revs[0])
194 194 endrevidx = 0
195 195 iterrevs = enumerate(revs)
196 196 next(iterrevs) # skip first rev.
197 197 for idx, r in iterrevs:
198 198 span = revlog.end(r) - startdata
199 199 if span <= targetsize:
200 200 endrevidx = idx
201 201 else:
202 202 chunk = _trimchunk(revlog, revs, startrevidx, endrevidx + 1)
203 203 if chunk:
204 204 yield chunk
205 205 startrevidx = idx
206 206 startdata = revlog.start(r)
207 207 endrevidx = idx
208 208 yield _trimchunk(revlog, revs, startrevidx)
209 209
210 210 def _slicechunktodensity(revlog, revs, deltainfo=None, targetdensity=0.5,
211 211 mingapsize=0):
212 212 """slice revs to reduce the amount of unrelated data to be read from disk.
213 213
214 214 ``revs`` is sliced into groups that should be read in one time.
215 215 Assume that revs are sorted.
216 216
217 217 ``deltainfo`` is a _deltainfo instance of a revision that we would append
218 218 to the top of the revlog.
219 219
220 220 The initial chunk is sliced until the overall density (payload/chunks-span
221 221 ratio) is above `targetdensity`. No gap smaller than `mingapsize` is
222 222 skipped.
223 223
224 224 >>> revlog = _testrevlog([
225 225 ... 5, #00 (5)
226 226 ... 10, #01 (5)
227 227 ... 12, #02 (2)
228 228 ... 12, #03 (empty)
229 229 ... 27, #04 (15)
230 230 ... 31, #05 (4)
231 231 ... 31, #06 (empty)
232 232 ... 42, #07 (11)
233 233 ... 47, #08 (5)
234 234 ... 47, #09 (empty)
235 235 ... 48, #10 (1)
236 236 ... 51, #11 (3)
237 237 ... 74, #12 (23)
238 238 ... 85, #13 (11)
239 239 ... 86, #14 (1)
240 240 ... 91, #15 (5)
241 241 ... ])
242 242
243 243 >>> list(_slicechunktodensity(revlog, list(range(16))))
244 244 [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]
245 245 >>> list(_slicechunktodensity(revlog, [0, 15]))
246 246 [[0], [15]]
247 247 >>> list(_slicechunktodensity(revlog, [0, 11, 15]))
248 248 [[0], [11], [15]]
249 249 >>> list(_slicechunktodensity(revlog, [0, 11, 13, 15]))
250 250 [[0], [11, 13, 15]]
251 251 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14]))
252 252 [[1, 2], [5, 8, 10, 11], [14]]
253 253 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
254 254 ... mingapsize=20))
255 255 [[1, 2, 3, 5, 8, 10, 11], [14]]
256 256 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
257 257 ... targetdensity=0.95))
258 258 [[1, 2], [5], [8, 10, 11], [14]]
259 259 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
260 260 ... targetdensity=0.95, mingapsize=12))
261 261 [[1, 2], [5, 8, 10, 11], [14]]
262 262 """
263 263 start = revlog.start
264 264 length = revlog.length
265 265
266 266 if len(revs) <= 1:
267 267 yield revs
268 268 return
269 269
270 270 nextrev = len(revlog)
271 271 nextoffset = revlog.end(nextrev - 1)
272 272
273 273 if deltainfo is None:
274 274 deltachainspan = segmentspan(revlog, revs)
275 275 chainpayload = sum(length(r) for r in revs)
276 276 else:
277 277 deltachainspan = deltainfo.distance
278 278 chainpayload = deltainfo.compresseddeltalen
279 279
280 280 if deltachainspan < mingapsize:
281 281 yield revs
282 282 return
283 283
284 284 readdata = deltachainspan
285 285
286 286 if deltachainspan:
287 287 density = chainpayload / float(deltachainspan)
288 288 else:
289 289 density = 1.0
290 290
291 291 if density >= targetdensity:
292 292 yield revs
293 293 return
294 294
295 295 if deltainfo is not None and deltainfo.deltalen:
296 296 revs = list(revs)
297 297 revs.append(nextrev)
298 298
299 299 # Store the gaps in a heap to have them sorted by decreasing size
300 300 gapsheap = []
301 301 heapq.heapify(gapsheap)
302 302 prevend = None
303 303 for i, rev in enumerate(revs):
304 304 if rev < nextrev:
305 305 revstart = start(rev)
306 306 revlen = length(rev)
307 307 else:
308 308 revstart = nextoffset
309 309 revlen = deltainfo.deltalen
310 310
311 311 # Skip empty revisions to form larger holes
312 312 if revlen == 0:
313 313 continue
314 314
315 315 if prevend is not None:
316 316 gapsize = revstart - prevend
317 317 # only consider holes that are large enough
318 318 if gapsize > mingapsize:
319 319 heapq.heappush(gapsheap, (-gapsize, i))
320 320
321 321 prevend = revstart + revlen
322 322
323 323 # Collect the indices of the largest holes until the density is acceptable
324 324 indicesheap = []
325 325 heapq.heapify(indicesheap)
326 326 while gapsheap and density < targetdensity:
327 327 oppgapsize, gapidx = heapq.heappop(gapsheap)
328 328
329 329 heapq.heappush(indicesheap, gapidx)
330 330
331 331 # the gap sizes are stored as negatives to be sorted decreasingly
332 332 # by the heap
333 333 readdata -= (-oppgapsize)
334 334 if readdata > 0:
335 335 density = chainpayload / float(readdata)
336 336 else:
337 337 density = 1.0
338 338
339 339 # Cut the revs at collected indices
340 340 previdx = 0
341 341 while indicesheap:
342 342 idx = heapq.heappop(indicesheap)
343 343
344 344 chunk = _trimchunk(revlog, revs, previdx, idx)
345 345 if chunk:
346 346 yield chunk
347 347
348 348 previdx = idx
349 349
350 350 chunk = _trimchunk(revlog, revs, previdx)
351 351 if chunk:
352 352 yield chunk
353 353
354 354 def _trimchunk(revlog, revs, startidx, endidx=None):
355 355 """returns revs[startidx:endidx] without empty trailing revs
356 356
357 357 Doctest Setup
358 358 >>> revlog = _testrevlog([
359 359 ... 5, #0
360 360 ... 10, #1
361 361 ... 12, #2
362 362 ... 12, #3 (empty)
363 363 ... 17, #4
364 364 ... 21, #5
365 365 ... 21, #6 (empty)
366 366 ... ])
367 367
368 368 Contiguous cases:
369 369 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0)
370 370 [0, 1, 2, 3, 4, 5]
371 371 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0, 5)
372 372 [0, 1, 2, 3, 4]
373 373 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0, 4)
374 374 [0, 1, 2]
375 375 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 2, 4)
376 376 [2]
377 377 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 3)
378 378 [3, 4, 5]
379 379 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 3, 5)
380 380 [3, 4]
381 381
382 382 Discontiguous cases:
383 383 >>> _trimchunk(revlog, [1, 3, 5, 6], 0)
384 384 [1, 3, 5]
385 385 >>> _trimchunk(revlog, [1, 3, 5, 6], 0, 2)
386 386 [1]
387 387 >>> _trimchunk(revlog, [1, 3, 5, 6], 1, 3)
388 388 [3, 5]
389 389 >>> _trimchunk(revlog, [1, 3, 5, 6], 1)
390 390 [3, 5]
391 391 """
392 392 length = revlog.length
393 393
394 394 if endidx is None:
395 395 endidx = len(revs)
396 396
397 397 # If we have a non-emtpy delta candidate, there are nothing to trim
398 398 if revs[endidx - 1] < len(revlog):
399 399 # Trim empty revs at the end, except the very first revision of a chain
400 400 while (endidx > 1
401 401 and endidx > startidx
402 402 and length(revs[endidx - 1]) == 0):
403 403 endidx -= 1
404 404
405 405 return revs[startidx:endidx]
406 406
407 407 def segmentspan(revlog, revs, deltainfo=None):
408 408 """Get the byte span of a segment of revisions
409 409
410 410 revs is a sorted array of revision numbers
411 411
412 412 >>> revlog = _testrevlog([
413 413 ... 5, #0
414 414 ... 10, #1
415 415 ... 12, #2
416 416 ... 12, #3 (empty)
417 417 ... 17, #4
418 418 ... ])
419 419
420 420 >>> segmentspan(revlog, [0, 1, 2, 3, 4])
421 421 17
422 422 >>> segmentspan(revlog, [0, 4])
423 423 17
424 424 >>> segmentspan(revlog, [3, 4])
425 425 5
426 426 >>> segmentspan(revlog, [1, 2, 3,])
427 427 7
428 428 >>> segmentspan(revlog, [1, 3])
429 429 7
430 430 """
431 431 if not revs:
432 432 return 0
433 433 if deltainfo is not None and len(revlog) <= revs[-1]:
434 434 if len(revs) == 1:
435 435 return deltainfo.deltalen
436 436 offset = revlog.end(len(revlog) - 1)
437 437 end = deltainfo.deltalen + offset
438 438 else:
439 439 end = revlog.end(revs[-1])
440 440 return end - revlog.start(revs[0])
441 441
442 442 def _textfromdelta(fh, revlog, baserev, delta, p1, p2, flags, expectednode):
443 443 """build full text from a (base, delta) pair and other metadata"""
444 444 # special case deltas which replace entire base; no need to decode
445 445 # base revision. this neatly avoids censored bases, which throw when
446 446 # they're decoded.
447 447 hlen = struct.calcsize(">lll")
448 448 if delta[:hlen] == mdiff.replacediffheader(revlog.rawsize(baserev),
449 449 len(delta) - hlen):
450 450 fulltext = delta[hlen:]
451 451 else:
452 452 # deltabase is rawtext before changed by flag processors, which is
453 453 # equivalent to non-raw text
454 454 basetext = revlog.revision(baserev, _df=fh, raw=False)
455 455 fulltext = mdiff.patch(basetext, delta)
456 456
457 457 try:
458 458 res = revlog._processflags(fulltext, flags, 'read', raw=True)
459 459 fulltext, validatehash = res
460 460 if validatehash:
461 461 revlog.checkhash(fulltext, expectednode, p1=p1, p2=p2)
462 462 if flags & REVIDX_ISCENSORED:
463 463 raise RevlogError(_('node %s is not censored') % expectednode)
464 464 except CensoredNodeError:
465 465 # must pass the censored index flag to add censored revisions
466 466 if not flags & REVIDX_ISCENSORED:
467 467 raise
468 468 return fulltext
469 469
470 470 @attr.s(slots=True, frozen=True)
471 471 class _deltainfo(object):
472 472 distance = attr.ib()
473 473 deltalen = attr.ib()
474 474 data = attr.ib()
475 475 base = attr.ib()
476 476 chainbase = attr.ib()
477 477 chainlen = attr.ib()
478 478 compresseddeltalen = attr.ib()
479 479 snapshotdepth = attr.ib()
480 480
481 481 def isgooddeltainfo(revlog, deltainfo, revinfo):
482 482 """Returns True if the given delta is good. Good means that it is within
483 483 the disk span, disk size, and chain length bounds that we know to be
484 484 performant."""
485 485 if deltainfo is None:
486 486 return False
487 487
488 488 # - 'deltainfo.distance' is the distance from the base revision --
489 489 # bounding it limits the amount of I/O we need to do.
490 490 # - 'deltainfo.compresseddeltalen' is the sum of the total size of
491 491 # deltas we need to apply -- bounding it limits the amount of CPU
492 492 # we consume.
493 493
494 494 if revlog._sparserevlog:
495 495 # As sparse-read will be used, we can consider that the distance,
496 496 # instead of being the span of the whole chunk,
497 497 # is the span of the largest read chunk
498 498 base = deltainfo.base
499 499
500 500 if base != nullrev:
501 501 deltachain = revlog._deltachain(base)[0]
502 502 else:
503 503 deltachain = []
504 504
505 505 # search for the first non-snapshot revision
506 506 for idx, r in enumerate(deltachain):
507 507 if not revlog.issnapshot(r):
508 508 break
509 509 deltachain = deltachain[idx:]
510 510 chunks = slicechunk(revlog, deltachain, deltainfo)
511 511 all_span = [segmentspan(revlog, revs, deltainfo)
512 512 for revs in chunks]
513 513 distance = max(all_span)
514 514 else:
515 515 distance = deltainfo.distance
516 516
517 517 textlen = revinfo.textlen
518 518 defaultmax = textlen * 4
519 519 maxdist = revlog._maxdeltachainspan
520 520 if not maxdist:
521 521 maxdist = distance # ensure the conditional pass
522 522 maxdist = max(maxdist, defaultmax)
523 523 if revlog._sparserevlog and maxdist < revlog._srmingapsize:
524 524 # In multiple place, we are ignoring irrelevant data range below a
525 525 # certain size. Be also apply this tradeoff here and relax span
526 526 # constraint for small enought content.
527 527 maxdist = revlog._srmingapsize
528 528
529 529 # Bad delta from read span:
530 530 #
531 531 # If the span of data read is larger than the maximum allowed.
532 532 if maxdist < distance:
533 533 return False
534 534
535 535 # Bad delta from new delta size:
536 536 #
537 537 # If the delta size is larger than the target text, storing the
538 538 # delta will be inefficient.
539 539 if textlen < deltainfo.deltalen:
540 540 return False
541 541
542 542 # Bad delta from cumulated payload size:
543 543 #
544 544 # If the sum of delta get larger than K * target text length.
545 545 if textlen * LIMIT_DELTA2TEXT < deltainfo.compresseddeltalen:
546 546 return False
547 547
548 548 # Bad delta from chain length:
549 549 #
550 550 # If the number of delta in the chain gets too high.
551 551 if (revlog._maxchainlen
552 552 and revlog._maxchainlen < deltainfo.chainlen):
553 553 return False
554 554
555 555 # bad delta from intermediate snapshot size limit
556 556 #
557 557 # If an intermediate snapshot size is higher than the limit. The
558 558 # limit exist to prevent endless chain of intermediate delta to be
559 559 # created.
560 560 if (deltainfo.snapshotdepth is not None and
561 561 (textlen >> deltainfo.snapshotdepth) < deltainfo.deltalen):
562 562 return False
563 563
564 564 # bad delta if new intermediate snapshot is larger than the previous
565 565 # snapshot
566 566 if (deltainfo.snapshotdepth
567 567 and revlog.length(deltainfo.base) < deltainfo.deltalen):
568 568 return False
569 569
570 570 return True
571 571
572 572 def _candidategroups(revlog, textlen, p1, p2, cachedelta):
573 573 """Provides group of revision to be tested as delta base
574 574
575 575 This top level function focus on emitting groups with unique and worthwhile
576 576 content. See _raw_candidate_groups for details about the group order.
577 577 """
578 578 # should we try to build a delta?
579 579 if not (len(revlog) and revlog._storedeltachains):
580 580 return
581 581
582 582 deltalength = revlog.length
583 583 deltaparent = revlog.deltaparent
584 584
585 585 deltas_limit = textlen * LIMIT_DELTA2TEXT
586 586
587 587 tested = set([nullrev])
588 588 for temptative in _rawgroups(revlog, p1, p2, cachedelta):
589 589 group = []
590 590 for rev in temptative:
591 591 # skip over empty delta (no need to include them in a chain)
592 592 while not (rev == nullrev or rev in tested or deltalength(rev)):
593 593 rev = deltaparent(rev)
594 594 tested.add(rev)
595 595 # filter out revision we tested already
596 596 if rev in tested:
597 597 continue
598 598 tested.add(rev)
599 599 # filter out delta base that will never produce good delta
600 600 if deltas_limit < revlog.length(rev):
601 601 continue
602 602 # no need to try a delta against nullrev, this will be done as a
603 603 # last resort.
604 604 if rev == nullrev:
605 605 continue
606 606 # no delta for rawtext-changing revs (see "candelta" for why)
607 607 if revlog.flags(rev) & REVIDX_RAWTEXT_CHANGING_FLAGS:
608 608 continue
609 609 group.append(rev)
610 610 if group:
611 611 # XXX: in the sparse revlog case, group can become large,
612 612 # impacting performances. Some bounding or slicing mecanism
613 613 # would help to reduce this impact.
614 614 yield tuple(group)
615 615
616 616 def _findsnapshots(revlog, cache, start_rev):
617 617 """find snapshot from start_rev to tip"""
618 618 deltaparent = revlog.deltaparent
619 619 for rev in revlog.revs(start_rev):
620 620 if deltaparent(rev) == nullrev:
621 621 cache[nullrev].append(rev)
622 622
623 623 def _rawgroups(revlog, p1, p2, cachedelta):
624 624 """Provides group of revision to be tested as delta base
625 625
626 626 This lower level function focus on emitting delta theorically interresting
627 627 without looking it any practical details.
628 628
629 629 The group order aims at providing fast or small candidates first.
630 630 """
631 631 gdelta = revlog._generaldelta
632 632 sparse = revlog._sparserevlog
633 633 curr = len(revlog)
634 634 prev = curr - 1
635 635 deltachain = lambda rev: revlog._deltachain(rev)[0]
636 636
637 637 # First we try to reuse a the delta contained in the bundle.
638 638 # (or from the source revlog)
639 639 #
640 640 # This logic only applies to general delta repositories and can be disabled
641 641 # through configuration. Disabling reuse of source delta is useful when
642 642 # we want to make sure we recomputed "optimal" deltas.
643 643 if cachedelta and gdelta and revlog._lazydeltabase:
644 644 # Assume what we received from the server is a good choice
645 645 # build delta will reuse the cache
646 646 yield (cachedelta[0],)
647 647
648 648 if gdelta:
649 649 # exclude already lazy tested base if any
650 650 parents = [p for p in (p1, p2) if p != nullrev]
651 651
652 652 if not revlog._deltabothparents and len(parents) == 2:
653 653 parents.sort()
654 654 # To minimize the chance of having to build a fulltext,
655 655 # pick first whichever parent is closest to us (max rev)
656 656 yield (parents[1],)
657 657 # then the other one (min rev) if the first did not fit
658 658 yield (parents[0],)
659 659 elif len(parents) > 0:
660 660 # Test all parents (1 or 2), and keep the best candidate
661 661 yield parents
662 662
663 663 if sparse and parents:
664 snapshots = collections.defaultdict(list) # map: base-rev: snapshot-rev
664 665 # See if we can use an existing snapshot in the parent chains to use as
665 666 # a base for a new intermediate-snapshot
666 bases = []
667 #
668 # search for snapshot in parents delta chain
669 # map: snapshot-level: snapshot-rev
670 parents_snaps = collections.defaultdict(set)
667 671 for p in parents:
668 bases.append(deltachain(p)[0])
669 yield tuple(sorted(bases))
672 for idx, s in enumerate(deltachain(p)):
673 if not revlog.issnapshot(s):
674 break
675 parents_snaps[idx].add(s)
676 # Test them as possible intermediate snapshot base
677 # We test them from highest to lowest level. High level one are more
678 # likely to result in small delta
679 for idx, snaps in sorted(parents_snaps.items(), reverse=True):
680 yield tuple(sorted(snaps))
670 681 # No suitable base found in the parent chain, search if any full
671 682 # snapshots emitted since parent's base would be a suitable base for an
672 683 # intermediate snapshot.
673 684 #
674 685 # It give a chance to reuse a delta chain unrelated to the current
675 686 # revisions instead of starting our own. Without such re-use,
676 687 # topological branches would keep reopening new full chains. Creating
677 688 # more and more snapshot as the repository grow.
678 snapfloor = min(bases) + 1
679 snapshots = collections.defaultdict(list)
689 snapfloor = min(parents_snaps[0]) + 1
680 690 _findsnapshots(revlog, snapshots, snapfloor)
681 691 yield tuple(snapshots[nullrev])
682 692
683 693 # other approach failed try against prev to hopefully save us a
684 694 # fulltext.
685 695 yield (prev,)
686 696
687 697 class deltacomputer(object):
688 698 def __init__(self, revlog):
689 699 self.revlog = revlog
690 700
691 701 def buildtext(self, revinfo, fh):
692 702 """Builds a fulltext version of a revision
693 703
694 704 revinfo: _revisioninfo instance that contains all needed info
695 705 fh: file handle to either the .i or the .d revlog file,
696 706 depending on whether it is inlined or not
697 707 """
698 708 btext = revinfo.btext
699 709 if btext[0] is not None:
700 710 return btext[0]
701 711
702 712 revlog = self.revlog
703 713 cachedelta = revinfo.cachedelta
704 714 baserev = cachedelta[0]
705 715 delta = cachedelta[1]
706 716
707 717 fulltext = btext[0] = _textfromdelta(fh, revlog, baserev, delta,
708 718 revinfo.p1, revinfo.p2,
709 719 revinfo.flags, revinfo.node)
710 720 return fulltext
711 721
712 722 def _builddeltadiff(self, base, revinfo, fh):
713 723 revlog = self.revlog
714 724 t = self.buildtext(revinfo, fh)
715 725 if revlog.iscensored(base):
716 726 # deltas based on a censored revision must replace the
717 727 # full content in one patch, so delta works everywhere
718 728 header = mdiff.replacediffheader(revlog.rawsize(base), len(t))
719 729 delta = header + t
720 730 else:
721 731 ptext = revlog.revision(base, _df=fh, raw=True)
722 732 delta = mdiff.textdiff(ptext, t)
723 733
724 734 return delta
725 735
726 736 def _builddeltainfo(self, revinfo, base, fh):
727 737 # can we use the cached delta?
728 738 if revinfo.cachedelta and revinfo.cachedelta[0] == base:
729 739 delta = revinfo.cachedelta[1]
730 740 else:
731 741 delta = self._builddeltadiff(base, revinfo, fh)
732 742 revlog = self.revlog
733 743 header, data = revlog.compress(delta)
734 744 deltalen = len(header) + len(data)
735 745 chainbase = revlog.chainbase(base)
736 746 offset = revlog.end(len(revlog) - 1)
737 747 dist = deltalen + offset - revlog.start(chainbase)
738 748 if revlog._generaldelta:
739 749 deltabase = base
740 750 else:
741 751 deltabase = chainbase
742 752 chainlen, compresseddeltalen = revlog._chaininfo(base)
743 753 chainlen += 1
744 754 compresseddeltalen += deltalen
745 755
746 756 revlog = self.revlog
747 757 snapshotdepth = None
748 758 if deltabase == nullrev:
749 759 snapshotdepth = 0
750 760 elif revlog._sparserevlog and revlog.issnapshot(deltabase):
751 761 # A delta chain should always be one full snapshot,
752 762 # zero or more semi-snapshots, and zero or more deltas
753 763 p1, p2 = revlog.rev(revinfo.p1), revlog.rev(revinfo.p2)
754 764 if deltabase not in (p1, p2) and revlog.issnapshot(deltabase):
755 765 snapshotdepth = len(revlog._deltachain(deltabase)[0])
756 766
757 767 return _deltainfo(dist, deltalen, (header, data), deltabase,
758 768 chainbase, chainlen, compresseddeltalen,
759 769 snapshotdepth)
760 770
761 771 def _fullsnapshotinfo(self, fh, revinfo):
762 772 curr = len(self.revlog)
763 773 rawtext = self.buildtext(revinfo, fh)
764 774 data = self.revlog.compress(rawtext)
765 775 compresseddeltalen = deltalen = dist = len(data[1]) + len(data[0])
766 776 deltabase = chainbase = curr
767 777 snapshotdepth = 0
768 778 chainlen = 1
769 779
770 780 return _deltainfo(dist, deltalen, data, deltabase,
771 781 chainbase, chainlen, compresseddeltalen,
772 782 snapshotdepth)
773 783
774 784 def finddeltainfo(self, revinfo, fh):
775 785 """Find an acceptable delta against a candidate revision
776 786
777 787 revinfo: information about the revision (instance of _revisioninfo)
778 788 fh: file handle to either the .i or the .d revlog file,
779 789 depending on whether it is inlined or not
780 790
781 791 Returns the first acceptable candidate revision, as ordered by
782 792 _candidategroups
783 793
784 794 If no suitable deltabase is found, we return delta info for a full
785 795 snapshot.
786 796 """
787 797 if not revinfo.textlen:
788 798 return self._fullsnapshotinfo(fh, revinfo)
789 799
790 800 # no delta for flag processor revision (see "candelta" for why)
791 801 # not calling candelta since only one revision needs test, also to
792 802 # avoid overhead fetching flags again.
793 803 if revinfo.flags & REVIDX_RAWTEXT_CHANGING_FLAGS:
794 804 return self._fullsnapshotinfo(fh, revinfo)
795 805
796 806 cachedelta = revinfo.cachedelta
797 807 p1 = revinfo.p1
798 808 p2 = revinfo.p2
799 809 revlog = self.revlog
800 810
801 811 deltainfo = None
802 812 p1r, p2r = revlog.rev(p1), revlog.rev(p2)
803 813 groups = _candidategroups(self.revlog, revinfo.textlen,
804 814 p1r, p2r, cachedelta)
805 815 for candidaterevs in groups:
806 816 nominateddeltas = []
807 817 for candidaterev in candidaterevs:
808 818 candidatedelta = self._builddeltainfo(revinfo, candidaterev, fh)
809 819 if isgooddeltainfo(self.revlog, candidatedelta, revinfo):
810 820 nominateddeltas.append(candidatedelta)
811 821 if nominateddeltas:
812 822 deltainfo = min(nominateddeltas, key=lambda x: x.deltalen)
813 823 break
814 824
815 825 if deltainfo is None:
816 826 deltainfo = self._fullsnapshotinfo(fh, revinfo)
817 827 return deltainfo
@@ -1,127 +1,133 b''
1 1 ====================================
2 2 Test delta choice with sparse revlog
3 3 ====================================
4 4
5 5 Sparse-revlog usually shows the most gain on Manifest. However, it is simpler
6 6 to general an appropriate file, so we test with a single file instead. The
7 7 goal is to observe intermediate snapshot being created.
8 8
9 9 We need a large enough file. Part of the content needs to be replaced
10 10 repeatedly while some of it changes rarely.
11 11
12 12 $ bundlepath="$TESTDIR/artifacts/cache/big-file-churn.hg"
13 13
14 14 $ expectedhash=`cat "$bundlepath".md5`
15 15 $ if [ ! -f "$bundlepath" ]; then
16 16 > echo 'skipped: missing artifact, run "'"$TESTDIR"'/artifacts/scripts/generate-churning-bundle.py"'
17 17 > exit 80
18 18 > fi
19 19 $ currenthash=`f -M "$bundlepath" | cut -d = -f 2`
20 20 $ if [ "$currenthash" != "$expectedhash" ]; then
21 21 > echo 'skipped: outdated artifact, md5 "'"$currenthash"'" expected "'"$expectedhash"'" run "'"$TESTDIR"'/artifacts/scripts/generate-churning-bundle.py"'
22 22 > exit 80
23 23 > fi
24 24
25 25 $ cat >> $HGRCPATH << EOF
26 26 > [format]
27 27 > sparse-revlog = yes
28 28 > [storage]
29 29 > revlog.optimize-delta-parent-choice = yes
30 30 > EOF
31 31 $ hg init sparse-repo
32 32 $ cd sparse-repo
33 33 $ hg unbundle $bundlepath
34 34 adding changesets
35 35 adding manifests
36 36 adding file changes
37 37 added 5001 changesets with 5001 changes to 1 files (+89 heads)
38 38 new changesets 9706f5af64f4:d9032adc8114 (5001 drafts)
39 39 (run 'hg heads' to see heads, 'hg merge' to merge)
40 40 $ hg up
41 41 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
42 42 updated to "d9032adc8114: commit #5000"
43 43 89 other heads for branch "default"
44 44
45 45 $ hg log --stat -r 0:3
46 46 changeset: 0:9706f5af64f4
47 47 user: test
48 48 date: Thu Jan 01 00:00:00 1970 +0000
49 49 summary: initial commit
50 50
51 51 SPARSE-REVLOG-TEST-FILE | 10500 ++++++++++++++++++++++++++++++++++++++++++++++
52 52 1 files changed, 10500 insertions(+), 0 deletions(-)
53 53
54 54 changeset: 1:724907deaa5e
55 55 user: test
56 56 date: Thu Jan 01 00:00:00 1970 +0000
57 57 summary: commit #1
58 58
59 59 SPARSE-REVLOG-TEST-FILE | 1068 +++++++++++++++++++++++-----------------------
60 60 1 files changed, 534 insertions(+), 534 deletions(-)
61 61
62 62 changeset: 2:62c41bce3e5d
63 63 user: test
64 64 date: Thu Jan 01 00:00:00 1970 +0000
65 65 summary: commit #2
66 66
67 67 SPARSE-REVLOG-TEST-FILE | 1068 +++++++++++++++++++++++-----------------------
68 68 1 files changed, 534 insertions(+), 534 deletions(-)
69 69
70 70 changeset: 3:348a9cbd6959
71 71 user: test
72 72 date: Thu Jan 01 00:00:00 1970 +0000
73 73 summary: commit #3
74 74
75 75 SPARSE-REVLOG-TEST-FILE | 1068 +++++++++++++++++++++++-----------------------
76 76 1 files changed, 534 insertions(+), 534 deletions(-)
77 77
78 78
79 79 $ f -s .hg/store/data/*.d
80 .hg/store/data/_s_p_a_r_s_e-_r_e_v_l_o_g-_t_e_s_t-_f_i_l_e.d: size=67810463
80 .hg/store/data/_s_p_a_r_s_e-_r_e_v_l_o_g-_t_e_s_t-_f_i_l_e.d: size=63812493
81 81 $ hg debugrevlog *
82 82 format : 1
83 83 flags : generaldelta
84 84
85 85 revisions : 5001
86 86 merges : 625 (12.50%)
87 87 normal : 4376 (87.50%)
88 88 revisions : 5001
89 89 empty : 0 ( 0.00%)
90 90 text : 0 (100.00%)
91 91 delta : 0 (100.00%)
92 snapshot : 126 ( 2.52%)
92 snapshot : 179 ( 3.58%)
93 93 lvl-0 : 4 ( 0.08%)
94 lvl-1 : 120 ( 2.40%)
95 lvl-2 : 2 ( 0.04%)
96 deltas : 4875 (97.48%)
97 revision size : 67810463
98 snapshot : 14373347 (21.20%)
99 lvl-0 : 804235 ( 1.19%)
100 lvl-1 : 13535903 (19.96%)
101 lvl-2 : 33209 ( 0.05%)
102 deltas : 53437116 (78.80%)
94 lvl-1 : 49 ( 0.98%)
95 lvl-2 : 56 ( 1.12%)
96 lvl-3 : 63 ( 1.26%)
97 lvl-4 : 7 ( 0.14%)
98 deltas : 4822 (96.42%)
99 revision size : 63812493
100 snapshot : 10745878 (16.84%)
101 lvl-0 : 804204 ( 1.26%)
102 lvl-1 : 4986508 ( 7.81%)
103 lvl-2 : 2858810 ( 4.48%)
104 lvl-3 : 1958906 ( 3.07%)
105 lvl-4 : 137450 ( 0.22%)
106 deltas : 53066615 (83.16%)
103 107
104 108 chunks : 5001
105 109 0x78 (x) : 5001 (100.00%)
106 chunks size : 67810463
107 0x78 (x) : 67810463 (100.00%)
110 chunks size : 63812493
111 0x78 (x) : 63812493 (100.00%)
108 112
109 avg chain length : 18
113 avg chain length : 17
110 114 max chain length : 45
111 max chain reach : 25808240
112 compression ratio : 25
115 max chain reach : 27506743
116 compression ratio : 27
113 117
114 118 uncompressed data size (min/max/avg) : 346468 / 346472 / 346471
115 full revision size (min/max/avg) : 201014 / 201116 / 201058
116 inter-snapshot size (min/max/avg) : 11623 / 173150 / 111222
117 level-1 (min/max/avg) : 11623 / 173150 / 112799
118 level-2 (min/max/avg) : 14151 / 19058 / 16604
119 delta size (min/max/avg) : 10649 / 101790 / 10961
119 full revision size (min/max/avg) : 201007 / 201077 / 201051
120 inter-snapshot size (min/max/avg) : 13223 / 172097 / 56809
121 level-1 (min/max/avg) : 13223 / 172097 / 101765
122 level-2 (min/max/avg) : 28558 / 85237 / 51050
123 level-3 (min/max/avg) : 14647 / 41752 / 31093
124 level-4 (min/max/avg) : 18596 / 20760 / 19635
125 delta size (min/max/avg) : 10649 / 104791 / 11005
120 126
121 deltas against prev : 4207 (86.30%)
122 where prev = p1 : 4164 (98.98%)
127 deltas against prev : 4171 (86.50%)
128 where prev = p1 : 4127 (98.95%)
123 129 where prev = p2 : 0 ( 0.00%)
124 other : 43 ( 1.02%)
125 deltas against p1 : 653 (13.39%)
126 deltas against p2 : 15 ( 0.31%)
130 other : 44 ( 1.05%)
131 deltas against p1 : 633 (13.13%)
132 deltas against p2 : 18 ( 0.37%)
127 133 deltas against other : 0 ( 0.00%)
General Comments 0
You need to be logged in to leave comments. Login now