##// END OF EJS Templates
revlog: remove legacy usage of `_deltabothparents`...
marmoute -
r51946:96f521df default
parent child Browse files
Show More
@@ -1,1630 +1,1630 b''
1 1 # revlogdeltas.py - Logic around delta computation for revlog
2 2 #
3 3 # Copyright 2005-2007 Olivia Mackall <olivia@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
11 11 import collections
12 12 import struct
13 13
14 14 # import stuff from node for others to import from revlog
15 15 from ..node import nullrev
16 16 from ..i18n import _
17 17
18 18 from .constants import (
19 19 COMP_MODE_DEFAULT,
20 20 COMP_MODE_INLINE,
21 21 COMP_MODE_PLAIN,
22 22 DELTA_BASE_REUSE_FORCE,
23 23 DELTA_BASE_REUSE_NO,
24 24 KIND_CHANGELOG,
25 25 KIND_FILELOG,
26 26 KIND_MANIFESTLOG,
27 27 REVIDX_ISCENSORED,
28 28 REVIDX_RAWTEXT_CHANGING_FLAGS,
29 29 )
30 30
31 31 from ..thirdparty import attr
32 32
33 33 from .. import (
34 34 error,
35 35 mdiff,
36 36 util,
37 37 )
38 38
39 39 from . import flagutil
40 40
41 41 # maximum <delta-chain-data>/<revision-text-length> ratio
42 42 LIMIT_DELTA2TEXT = 2
43 43
44 44
45 45 class _testrevlog:
46 46 """minimalist fake revlog to use in doctests"""
47 47
48 48 def __init__(self, data, density=0.5, mingap=0, snapshot=()):
49 49 """data is an list of revision payload boundaries"""
50 50 from .. import revlog
51 51
52 52 self._data = data
53 53 self._srdensitythreshold = density
54 54 self._srmingapsize = mingap
55 55 self.data_config = revlog.DataConfig()
56 56 self.data_config.sr_density_threshold = density
57 57 self.data_config.sr_min_gap_size = mingap
58 58 self.delta_config = revlog.DeltaConfig()
59 59 self.feature_config = revlog.FeatureConfig()
60 60 self._snapshot = set(snapshot)
61 61 self.index = None
62 62
63 63 def start(self, rev):
64 64 if rev == nullrev:
65 65 return 0
66 66 if rev == 0:
67 67 return 0
68 68 return self._data[rev - 1]
69 69
70 70 def end(self, rev):
71 71 if rev == nullrev:
72 72 return 0
73 73 return self._data[rev]
74 74
75 75 def length(self, rev):
76 76 return self.end(rev) - self.start(rev)
77 77
78 78 def __len__(self):
79 79 return len(self._data)
80 80
81 81 def issnapshot(self, rev):
82 82 if rev == nullrev:
83 83 return True
84 84 return rev in self._snapshot
85 85
86 86
87 87 def slicechunk(revlog, revs, targetsize=None):
88 88 """slice revs to reduce the amount of unrelated data to be read from disk.
89 89
90 90 ``revs`` is sliced into groups that should be read in one time.
91 91 Assume that revs are sorted.
92 92
93 93 The initial chunk is sliced until the overall density (payload/chunks-span
94 94 ratio) is above `revlog._srdensitythreshold`. No gap smaller than
95 95 `revlog._srmingapsize` is skipped.
96 96
97 97 If `targetsize` is set, no chunk larger than `targetsize` will be yield.
98 98 For consistency with other slicing choice, this limit won't go lower than
99 99 `revlog._srmingapsize`.
100 100
101 101 If individual revisions chunk are larger than this limit, they will still
102 102 be raised individually.
103 103
104 104 >>> data = [
105 105 ... 5, #00 (5)
106 106 ... 10, #01 (5)
107 107 ... 12, #02 (2)
108 108 ... 12, #03 (empty)
109 109 ... 27, #04 (15)
110 110 ... 31, #05 (4)
111 111 ... 31, #06 (empty)
112 112 ... 42, #07 (11)
113 113 ... 47, #08 (5)
114 114 ... 47, #09 (empty)
115 115 ... 48, #10 (1)
116 116 ... 51, #11 (3)
117 117 ... 74, #12 (23)
118 118 ... 85, #13 (11)
119 119 ... 86, #14 (1)
120 120 ... 91, #15 (5)
121 121 ... ]
122 122 >>> revlog = _testrevlog(data, snapshot=range(16))
123 123
124 124 >>> list(slicechunk(revlog, list(range(16))))
125 125 [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]
126 126 >>> list(slicechunk(revlog, [0, 15]))
127 127 [[0], [15]]
128 128 >>> list(slicechunk(revlog, [0, 11, 15]))
129 129 [[0], [11], [15]]
130 130 >>> list(slicechunk(revlog, [0, 11, 13, 15]))
131 131 [[0], [11, 13, 15]]
132 132 >>> list(slicechunk(revlog, [1, 2, 3, 5, 8, 10, 11, 14]))
133 133 [[1, 2], [5, 8, 10, 11], [14]]
134 134
135 135 Slicing with a maximum chunk size
136 136 >>> list(slicechunk(revlog, [0, 11, 13, 15], targetsize=15))
137 137 [[0], [11], [13], [15]]
138 138 >>> list(slicechunk(revlog, [0, 11, 13, 15], targetsize=20))
139 139 [[0], [11], [13, 15]]
140 140
141 141 Slicing involving nullrev
142 142 >>> list(slicechunk(revlog, [-1, 0, 11, 13, 15], targetsize=20))
143 143 [[-1, 0], [11], [13, 15]]
144 144 >>> list(slicechunk(revlog, [-1, 13, 15], targetsize=5))
145 145 [[-1], [13], [15]]
146 146 """
147 147 if targetsize is not None:
148 148 targetsize = max(targetsize, revlog._srmingapsize)
149 149 # targetsize should not be specified when evaluating delta candidates:
150 150 # * targetsize is used to ensure we stay within specification when reading,
151 151 densityslicing = getattr(revlog.index, 'slicechunktodensity', None)
152 152 if densityslicing is None:
153 153 densityslicing = lambda x, y, z: _slicechunktodensity(revlog, x, y, z)
154 154 for chunk in densityslicing(
155 155 revs, revlog._srdensitythreshold, revlog._srmingapsize
156 156 ):
157 157 for subchunk in _slicechunktosize(revlog, chunk, targetsize):
158 158 yield subchunk
159 159
160 160
161 161 def _slicechunktosize(revlog, revs, targetsize=None):
162 162 """slice revs to match the target size
163 163
164 164 This is intended to be used on chunk that density slicing selected by that
165 165 are still too large compared to the read garantee of revlog. This might
166 166 happens when "minimal gap size" interrupted the slicing or when chain are
167 167 built in a way that create large blocks next to each other.
168 168
169 169 >>> data = [
170 170 ... 3, #0 (3)
171 171 ... 5, #1 (2)
172 172 ... 6, #2 (1)
173 173 ... 8, #3 (2)
174 174 ... 8, #4 (empty)
175 175 ... 11, #5 (3)
176 176 ... 12, #6 (1)
177 177 ... 13, #7 (1)
178 178 ... 14, #8 (1)
179 179 ... ]
180 180
181 181 == All snapshots cases ==
182 182 >>> revlog = _testrevlog(data, snapshot=range(9))
183 183
184 184 Cases where chunk is already small enough
185 185 >>> list(_slicechunktosize(revlog, [0], 3))
186 186 [[0]]
187 187 >>> list(_slicechunktosize(revlog, [6, 7], 3))
188 188 [[6, 7]]
189 189 >>> list(_slicechunktosize(revlog, [0], None))
190 190 [[0]]
191 191 >>> list(_slicechunktosize(revlog, [6, 7], None))
192 192 [[6, 7]]
193 193
194 194 cases where we need actual slicing
195 195 >>> list(_slicechunktosize(revlog, [0, 1], 3))
196 196 [[0], [1]]
197 197 >>> list(_slicechunktosize(revlog, [1, 3], 3))
198 198 [[1], [3]]
199 199 >>> list(_slicechunktosize(revlog, [1, 2, 3], 3))
200 200 [[1, 2], [3]]
201 201 >>> list(_slicechunktosize(revlog, [3, 5], 3))
202 202 [[3], [5]]
203 203 >>> list(_slicechunktosize(revlog, [3, 4, 5], 3))
204 204 [[3], [5]]
205 205 >>> list(_slicechunktosize(revlog, [5, 6, 7, 8], 3))
206 206 [[5], [6, 7, 8]]
207 207 >>> list(_slicechunktosize(revlog, [0, 1, 2, 3, 4, 5, 6, 7, 8], 3))
208 208 [[0], [1, 2], [3], [5], [6, 7, 8]]
209 209
210 210 Case with too large individual chunk (must return valid chunk)
211 211 >>> list(_slicechunktosize(revlog, [0, 1], 2))
212 212 [[0], [1]]
213 213 >>> list(_slicechunktosize(revlog, [1, 3], 1))
214 214 [[1], [3]]
215 215 >>> list(_slicechunktosize(revlog, [3, 4, 5], 2))
216 216 [[3], [5]]
217 217
218 218 == No Snapshot cases ==
219 219 >>> revlog = _testrevlog(data)
220 220
221 221 Cases where chunk is already small enough
222 222 >>> list(_slicechunktosize(revlog, [0], 3))
223 223 [[0]]
224 224 >>> list(_slicechunktosize(revlog, [6, 7], 3))
225 225 [[6, 7]]
226 226 >>> list(_slicechunktosize(revlog, [0], None))
227 227 [[0]]
228 228 >>> list(_slicechunktosize(revlog, [6, 7], None))
229 229 [[6, 7]]
230 230
231 231 cases where we need actual slicing
232 232 >>> list(_slicechunktosize(revlog, [0, 1], 3))
233 233 [[0], [1]]
234 234 >>> list(_slicechunktosize(revlog, [1, 3], 3))
235 235 [[1], [3]]
236 236 >>> list(_slicechunktosize(revlog, [1, 2, 3], 3))
237 237 [[1], [2, 3]]
238 238 >>> list(_slicechunktosize(revlog, [3, 5], 3))
239 239 [[3], [5]]
240 240 >>> list(_slicechunktosize(revlog, [3, 4, 5], 3))
241 241 [[3], [4, 5]]
242 242 >>> list(_slicechunktosize(revlog, [5, 6, 7, 8], 3))
243 243 [[5], [6, 7, 8]]
244 244 >>> list(_slicechunktosize(revlog, [0, 1, 2, 3, 4, 5, 6, 7, 8], 3))
245 245 [[0], [1, 2], [3], [5], [6, 7, 8]]
246 246
247 247 Case with too large individual chunk (must return valid chunk)
248 248 >>> list(_slicechunktosize(revlog, [0, 1], 2))
249 249 [[0], [1]]
250 250 >>> list(_slicechunktosize(revlog, [1, 3], 1))
251 251 [[1], [3]]
252 252 >>> list(_slicechunktosize(revlog, [3, 4, 5], 2))
253 253 [[3], [5]]
254 254
255 255 == mixed case ==
256 256 >>> revlog = _testrevlog(data, snapshot=[0, 1, 2])
257 257 >>> list(_slicechunktosize(revlog, list(range(9)), 5))
258 258 [[0, 1], [2], [3, 4, 5], [6, 7, 8]]
259 259 """
260 260 assert targetsize is None or 0 <= targetsize
261 261 startdata = revlog.start(revs[0])
262 262 enddata = revlog.end(revs[-1])
263 263 fullspan = enddata - startdata
264 264 if targetsize is None or fullspan <= targetsize:
265 265 yield revs
266 266 return
267 267
268 268 startrevidx = 0
269 269 endrevidx = 1
270 270 iterrevs = enumerate(revs)
271 271 next(iterrevs) # skip first rev.
272 272 # first step: get snapshots out of the way
273 273 for idx, r in iterrevs:
274 274 span = revlog.end(r) - startdata
275 275 snapshot = revlog.issnapshot(r)
276 276 if span <= targetsize and snapshot:
277 277 endrevidx = idx + 1
278 278 else:
279 279 chunk = _trimchunk(revlog, revs, startrevidx, endrevidx)
280 280 if chunk:
281 281 yield chunk
282 282 startrevidx = idx
283 283 startdata = revlog.start(r)
284 284 endrevidx = idx + 1
285 285 if not snapshot:
286 286 break
287 287
288 288 # for the others, we use binary slicing to quickly converge toward valid
289 289 # chunks (otherwise, we might end up looking for start/end of many
290 290 # revisions). This logic is not looking for the perfect slicing point, it
291 291 # focuses on quickly converging toward valid chunks.
292 292 nbitem = len(revs)
293 293 while (enddata - startdata) > targetsize:
294 294 endrevidx = nbitem
295 295 if nbitem - startrevidx <= 1:
296 296 break # protect against individual chunk larger than limit
297 297 localenddata = revlog.end(revs[endrevidx - 1])
298 298 span = localenddata - startdata
299 299 while span > targetsize:
300 300 if endrevidx - startrevidx <= 1:
301 301 break # protect against individual chunk larger than limit
302 302 endrevidx -= (endrevidx - startrevidx) // 2
303 303 localenddata = revlog.end(revs[endrevidx - 1])
304 304 span = localenddata - startdata
305 305 chunk = _trimchunk(revlog, revs, startrevidx, endrevidx)
306 306 if chunk:
307 307 yield chunk
308 308 startrevidx = endrevidx
309 309 startdata = revlog.start(revs[startrevidx])
310 310
311 311 chunk = _trimchunk(revlog, revs, startrevidx)
312 312 if chunk:
313 313 yield chunk
314 314
315 315
316 316 def _slicechunktodensity(revlog, revs, targetdensity=0.5, mingapsize=0):
317 317 """slice revs to reduce the amount of unrelated data to be read from disk.
318 318
319 319 ``revs`` is sliced into groups that should be read in one time.
320 320 Assume that revs are sorted.
321 321
322 322 The initial chunk is sliced until the overall density (payload/chunks-span
323 323 ratio) is above `targetdensity`. No gap smaller than `mingapsize` is
324 324 skipped.
325 325
326 326 >>> revlog = _testrevlog([
327 327 ... 5, #00 (5)
328 328 ... 10, #01 (5)
329 329 ... 12, #02 (2)
330 330 ... 12, #03 (empty)
331 331 ... 27, #04 (15)
332 332 ... 31, #05 (4)
333 333 ... 31, #06 (empty)
334 334 ... 42, #07 (11)
335 335 ... 47, #08 (5)
336 336 ... 47, #09 (empty)
337 337 ... 48, #10 (1)
338 338 ... 51, #11 (3)
339 339 ... 74, #12 (23)
340 340 ... 85, #13 (11)
341 341 ... 86, #14 (1)
342 342 ... 91, #15 (5)
343 343 ... ])
344 344
345 345 >>> list(_slicechunktodensity(revlog, list(range(16))))
346 346 [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]
347 347 >>> list(_slicechunktodensity(revlog, [0, 15]))
348 348 [[0], [15]]
349 349 >>> list(_slicechunktodensity(revlog, [0, 11, 15]))
350 350 [[0], [11], [15]]
351 351 >>> list(_slicechunktodensity(revlog, [0, 11, 13, 15]))
352 352 [[0], [11, 13, 15]]
353 353 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14]))
354 354 [[1, 2], [5, 8, 10, 11], [14]]
355 355 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
356 356 ... mingapsize=20))
357 357 [[1, 2, 3, 5, 8, 10, 11], [14]]
358 358 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
359 359 ... targetdensity=0.95))
360 360 [[1, 2], [5], [8, 10, 11], [14]]
361 361 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
362 362 ... targetdensity=0.95, mingapsize=12))
363 363 [[1, 2], [5, 8, 10, 11], [14]]
364 364 """
365 365 start = revlog.start
366 366 length = revlog.length
367 367
368 368 if len(revs) <= 1:
369 369 yield revs
370 370 return
371 371
372 372 deltachainspan = segmentspan(revlog, revs)
373 373
374 374 if deltachainspan < mingapsize:
375 375 yield revs
376 376 return
377 377
378 378 readdata = deltachainspan
379 379 chainpayload = sum(length(r) for r in revs)
380 380
381 381 if deltachainspan:
382 382 density = chainpayload / float(deltachainspan)
383 383 else:
384 384 density = 1.0
385 385
386 386 if density >= targetdensity:
387 387 yield revs
388 388 return
389 389
390 390 # Store the gaps in a heap to have them sorted by decreasing size
391 391 gaps = []
392 392 prevend = None
393 393 for i, rev in enumerate(revs):
394 394 revstart = start(rev)
395 395 revlen = length(rev)
396 396
397 397 # Skip empty revisions to form larger holes
398 398 if revlen == 0:
399 399 continue
400 400
401 401 if prevend is not None:
402 402 gapsize = revstart - prevend
403 403 # only consider holes that are large enough
404 404 if gapsize > mingapsize:
405 405 gaps.append((gapsize, i))
406 406
407 407 prevend = revstart + revlen
408 408 # sort the gaps to pop them from largest to small
409 409 gaps.sort()
410 410
411 411 # Collect the indices of the largest holes until the density is acceptable
412 412 selected = []
413 413 while gaps and density < targetdensity:
414 414 gapsize, gapidx = gaps.pop()
415 415
416 416 selected.append(gapidx)
417 417
418 418 # the gap sizes are stored as negatives to be sorted decreasingly
419 419 # by the heap
420 420 readdata -= gapsize
421 421 if readdata > 0:
422 422 density = chainpayload / float(readdata)
423 423 else:
424 424 density = 1.0
425 425 selected.sort()
426 426
427 427 # Cut the revs at collected indices
428 428 previdx = 0
429 429 for idx in selected:
430 430
431 431 chunk = _trimchunk(revlog, revs, previdx, idx)
432 432 if chunk:
433 433 yield chunk
434 434
435 435 previdx = idx
436 436
437 437 chunk = _trimchunk(revlog, revs, previdx)
438 438 if chunk:
439 439 yield chunk
440 440
441 441
442 442 def _trimchunk(revlog, revs, startidx, endidx=None):
443 443 """returns revs[startidx:endidx] without empty trailing revs
444 444
445 445 Doctest Setup
446 446 >>> revlog = _testrevlog([
447 447 ... 5, #0
448 448 ... 10, #1
449 449 ... 12, #2
450 450 ... 12, #3 (empty)
451 451 ... 17, #4
452 452 ... 21, #5
453 453 ... 21, #6 (empty)
454 454 ... ])
455 455
456 456 Contiguous cases:
457 457 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0)
458 458 [0, 1, 2, 3, 4, 5]
459 459 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0, 5)
460 460 [0, 1, 2, 3, 4]
461 461 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0, 4)
462 462 [0, 1, 2]
463 463 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 2, 4)
464 464 [2]
465 465 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 3)
466 466 [3, 4, 5]
467 467 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 3, 5)
468 468 [3, 4]
469 469
470 470 Discontiguous cases:
471 471 >>> _trimchunk(revlog, [1, 3, 5, 6], 0)
472 472 [1, 3, 5]
473 473 >>> _trimchunk(revlog, [1, 3, 5, 6], 0, 2)
474 474 [1]
475 475 >>> _trimchunk(revlog, [1, 3, 5, 6], 1, 3)
476 476 [3, 5]
477 477 >>> _trimchunk(revlog, [1, 3, 5, 6], 1)
478 478 [3, 5]
479 479 """
480 480 length = revlog.length
481 481
482 482 if endidx is None:
483 483 endidx = len(revs)
484 484
485 485 # If we have a non-emtpy delta candidate, there are nothing to trim
486 486 if revs[endidx - 1] < len(revlog):
487 487 # Trim empty revs at the end, except the very first revision of a chain
488 488 while (
489 489 endidx > 1 and endidx > startidx and length(revs[endidx - 1]) == 0
490 490 ):
491 491 endidx -= 1
492 492
493 493 return revs[startidx:endidx]
494 494
495 495
496 496 def segmentspan(revlog, revs):
497 497 """Get the byte span of a segment of revisions
498 498
499 499 revs is a sorted array of revision numbers
500 500
501 501 >>> revlog = _testrevlog([
502 502 ... 5, #0
503 503 ... 10, #1
504 504 ... 12, #2
505 505 ... 12, #3 (empty)
506 506 ... 17, #4
507 507 ... ])
508 508
509 509 >>> segmentspan(revlog, [0, 1, 2, 3, 4])
510 510 17
511 511 >>> segmentspan(revlog, [0, 4])
512 512 17
513 513 >>> segmentspan(revlog, [3, 4])
514 514 5
515 515 >>> segmentspan(revlog, [1, 2, 3,])
516 516 7
517 517 >>> segmentspan(revlog, [1, 3])
518 518 7
519 519 """
520 520 if not revs:
521 521 return 0
522 522 end = revlog.end(revs[-1])
523 523 return end - revlog.start(revs[0])
524 524
525 525
526 526 def _textfromdelta(revlog, baserev, delta, p1, p2, flags, expectednode):
527 527 """build full text from a (base, delta) pair and other metadata"""
528 528 # special case deltas which replace entire base; no need to decode
529 529 # base revision. this neatly avoids censored bases, which throw when
530 530 # they're decoded.
531 531 hlen = struct.calcsize(b">lll")
532 532 if delta[:hlen] == mdiff.replacediffheader(
533 533 revlog.rawsize(baserev), len(delta) - hlen
534 534 ):
535 535 fulltext = delta[hlen:]
536 536 else:
537 537 # deltabase is rawtext before changed by flag processors, which is
538 538 # equivalent to non-raw text
539 539 basetext = revlog.revision(baserev)
540 540 fulltext = mdiff.patch(basetext, delta)
541 541
542 542 try:
543 543 validatehash = flagutil.processflagsraw(revlog, fulltext, flags)
544 544 if validatehash:
545 545 revlog.checkhash(fulltext, expectednode, p1=p1, p2=p2)
546 546 if flags & REVIDX_ISCENSORED:
547 547 raise error.StorageError(
548 548 _(b'node %s is not censored') % expectednode
549 549 )
550 550 except error.CensoredNodeError:
551 551 # must pass the censored index flag to add censored revisions
552 552 if not flags & REVIDX_ISCENSORED:
553 553 raise
554 554 return fulltext
555 555
556 556
557 557 @attr.s(slots=True, frozen=True)
558 558 class _deltainfo:
559 559 distance = attr.ib()
560 560 deltalen = attr.ib()
561 561 data = attr.ib()
562 562 base = attr.ib()
563 563 chainbase = attr.ib()
564 564 chainlen = attr.ib()
565 565 compresseddeltalen = attr.ib()
566 566 snapshotdepth = attr.ib()
567 567
568 568
569 569 def drop_u_compression(delta):
570 570 """turn into a "u" (no-compression) into no-compression without header
571 571
572 572 This is useful for revlog format that has better compression method.
573 573 """
574 574 assert delta.data[0] == b'u', delta.data[0]
575 575 return _deltainfo(
576 576 delta.distance,
577 577 delta.deltalen - 1,
578 578 (b'', delta.data[1]),
579 579 delta.base,
580 580 delta.chainbase,
581 581 delta.chainlen,
582 582 delta.compresseddeltalen,
583 583 delta.snapshotdepth,
584 584 )
585 585
586 586
587 587 def is_good_delta_info(revlog, deltainfo, revinfo):
588 588 """Returns True if the given delta is good. Good means that it is within
589 589 the disk span, disk size, and chain length bounds that we know to be
590 590 performant."""
591 591 if deltainfo is None:
592 592 return False
593 593
594 594 # the DELTA_BASE_REUSE_FORCE case should have been taken care of sooner so
595 595 # we should never end up asking such question. Adding the assert as a
596 596 # safe-guard to detect anything that would be fishy in this regard.
597 597 assert (
598 598 revinfo.cachedelta is None
599 599 or revinfo.cachedelta[2] != DELTA_BASE_REUSE_FORCE
600 600 or not revlog.delta_config.general_delta
601 601 )
602 602
603 603 # - 'deltainfo.distance' is the distance from the base revision --
604 604 # bounding it limits the amount of I/O we need to do.
605 605 # - 'deltainfo.compresseddeltalen' is the sum of the total size of
606 606 # deltas we need to apply -- bounding it limits the amount of CPU
607 607 # we consume.
608 608
609 609 textlen = revinfo.textlen
610 610 defaultmax = textlen * 4
611 611 maxdist = revlog._maxdeltachainspan
612 612 if not maxdist:
613 613 maxdist = deltainfo.distance # ensure the conditional pass
614 614 maxdist = max(maxdist, defaultmax)
615 615
616 616 # Bad delta from read span:
617 617 #
618 618 # If the span of data read is larger than the maximum allowed.
619 619 #
620 620 # In the sparse-revlog case, we rely on the associated "sparse reading"
621 621 # to avoid issue related to the span of data. In theory, it would be
622 622 # possible to build pathological revlog where delta pattern would lead
623 623 # to too many reads. However, they do not happen in practice at all. So
624 624 # we skip the span check entirely.
625 625 if not revlog._sparserevlog and maxdist < deltainfo.distance:
626 626 return False
627 627
628 628 # Bad delta from new delta size:
629 629 #
630 630 # If the delta size is larger than the target text, storing the
631 631 # delta will be inefficient.
632 632 if textlen < deltainfo.deltalen:
633 633 return False
634 634
635 635 # Bad delta from cumulated payload size:
636 636 #
637 637 # If the sum of delta get larger than K * target text length.
638 638 if textlen * LIMIT_DELTA2TEXT < deltainfo.compresseddeltalen:
639 639 return False
640 640
641 641 # Bad delta from chain length:
642 642 #
643 643 # If the number of delta in the chain gets too high.
644 644 if (
645 645 revlog.delta_config.max_chain_len
646 646 and revlog.delta_config.max_chain_len < deltainfo.chainlen
647 647 ):
648 648 return False
649 649
650 650 # bad delta from intermediate snapshot size limit
651 651 #
652 652 # If an intermediate snapshot size is higher than the limit. The
653 653 # limit exist to prevent endless chain of intermediate delta to be
654 654 # created.
655 655 if (
656 656 deltainfo.snapshotdepth is not None
657 657 and (textlen >> deltainfo.snapshotdepth) < deltainfo.deltalen
658 658 ):
659 659 return False
660 660
661 661 # bad delta if new intermediate snapshot is larger than the previous
662 662 # snapshot
663 663 if (
664 664 deltainfo.snapshotdepth
665 665 and revlog.length(deltainfo.base) < deltainfo.deltalen
666 666 ):
667 667 return False
668 668
669 669 return True
670 670
671 671
672 672 # If a revision's full text is that much bigger than a base candidate full
673 673 # text's, it is very unlikely that it will produce a valid delta. We no longer
674 674 # consider these candidates.
675 675 LIMIT_BASE2TEXT = 500
676 676
677 677
678 678 def _candidategroups(
679 679 revlog,
680 680 textlen,
681 681 p1,
682 682 p2,
683 683 cachedelta,
684 684 excluded_bases=None,
685 685 target_rev=None,
686 686 snapshot_cache=None,
687 687 ):
688 688 """Provides group of revision to be tested as delta base
689 689
690 690 This top level function focus on emitting groups with unique and worthwhile
691 691 content. See _raw_candidate_groups for details about the group order.
692 692 """
693 693 # should we try to build a delta?
694 694 if not (len(revlog) and revlog._storedeltachains):
695 695 yield None
696 696 return
697 697
698 698 if target_rev is None:
699 699 target_rev = len(revlog)
700 700
701 701 if not revlog.delta_config.general_delta:
702 702 # before general delta, there is only one possible delta base
703 703 yield (target_rev - 1,)
704 704 yield None
705 705 return
706 706
707 707 # the DELTA_BASE_REUSE_FORCE case should have been taken care of sooner so
708 708 # we should never end up asking such question. Adding the assert as a
709 709 # safe-guard to detect anything that would be fishy in this regard.
710 710 assert (
711 711 cachedelta is None
712 712 or cachedelta[2] != DELTA_BASE_REUSE_FORCE
713 713 or not revlog.delta_config.general_delta
714 714 )
715 715
716 716 deltalength = revlog.length
717 717 deltaparent = revlog.deltaparent
718 718 sparse = revlog._sparserevlog
719 719 good = None
720 720
721 721 deltas_limit = textlen * LIMIT_DELTA2TEXT
722 722 group_chunk_size = revlog._candidate_group_chunk_size
723 723
724 724 tested = {nullrev}
725 725 candidates = _refinedgroups(
726 726 revlog,
727 727 p1,
728 728 p2,
729 729 cachedelta,
730 730 snapshot_cache=snapshot_cache,
731 731 )
732 732 while True:
733 733 temptative = candidates.send(good)
734 734 if temptative is None:
735 735 break
736 736 group = []
737 737 for rev in temptative:
738 738 # skip over empty delta (no need to include them in a chain)
739 739 while not (rev == nullrev or rev in tested or deltalength(rev)):
740 740 tested.add(rev)
741 741 rev = deltaparent(rev)
742 742 # no need to try a delta against nullrev, this will be done as a
743 743 # last resort.
744 744 if rev == nullrev:
745 745 continue
746 746 # filter out revision we tested already
747 747 if rev in tested:
748 748 continue
749 749
750 750 # an higher authority deamed the base unworthy (e.g. censored)
751 751 if excluded_bases is not None and rev in excluded_bases:
752 752 tested.add(rev)
753 753 continue
754 754 # We are in some recomputation cases and that rev is too high in
755 755 # the revlog
756 756 if target_rev is not None and rev >= target_rev:
757 757 tested.add(rev)
758 758 continue
759 759 # filter out delta base that will never produce good delta
760 760 if deltas_limit < revlog.length(rev):
761 761 tested.add(rev)
762 762 continue
763 763 if sparse and revlog.rawsize(rev) < (textlen // LIMIT_BASE2TEXT):
764 764 tested.add(rev)
765 765 continue
766 766 # no delta for rawtext-changing revs (see "candelta" for why)
767 767 if revlog.flags(rev) & REVIDX_RAWTEXT_CHANGING_FLAGS:
768 768 tested.add(rev)
769 769 continue
770 770
771 771 # If we reach here, we are about to build and test a delta.
772 772 # The delta building process will compute the chaininfo in all
773 773 # case, since that computation is cached, it is fine to access it
774 774 # here too.
775 775 chainlen, chainsize = revlog._chaininfo(rev)
776 776 # if chain will be too long, skip base
777 777 if (
778 778 revlog.delta_config.max_chain_len
779 779 and chainlen >= revlog.delta_config.max_chain_len
780 780 ):
781 781 tested.add(rev)
782 782 continue
783 783 # if chain already have too much data, skip base
784 784 if deltas_limit < chainsize:
785 785 tested.add(rev)
786 786 continue
787 787 if sparse and revlog.upperboundcomp is not None:
788 788 maxcomp = revlog.upperboundcomp
789 789 basenotsnap = (p1, p2, nullrev)
790 790 if rev not in basenotsnap and revlog.issnapshot(rev):
791 791 snapshotdepth = revlog.snapshotdepth(rev)
792 792 # If text is significantly larger than the base, we can
793 793 # expect the resulting delta to be proportional to the size
794 794 # difference
795 795 revsize = revlog.rawsize(rev)
796 796 rawsizedistance = max(textlen - revsize, 0)
797 797 # use an estimate of the compression upper bound.
798 798 lowestrealisticdeltalen = rawsizedistance // maxcomp
799 799
800 800 # check the absolute constraint on the delta size
801 801 snapshotlimit = textlen >> snapshotdepth
802 802 if snapshotlimit < lowestrealisticdeltalen:
803 803 # delta lower bound is larger than accepted upper bound
804 804 tested.add(rev)
805 805 continue
806 806
807 807 # check the relative constraint on the delta size
808 808 revlength = revlog.length(rev)
809 809 if revlength < lowestrealisticdeltalen:
810 810 # delta probable lower bound is larger than target base
811 811 tested.add(rev)
812 812 continue
813 813
814 814 group.append(rev)
815 815 if group:
816 816 # When the size of the candidate group is big, it can result in a
817 817 # quite significant performance impact. To reduce this, we can send
818 818 # them in smaller batches until the new batch does not provide any
819 819 # improvements.
820 820 #
821 821 # This might reduce the overall efficiency of the compression in
822 822 # some corner cases, but that should also prevent very pathological
823 823 # cases from being an issue. (eg. 20 000 candidates).
824 824 #
825 825 # XXX note that the ordering of the group becomes important as it
826 826 # now impacts the final result. The current order is unprocessed
827 827 # and can be improved.
828 828 if group_chunk_size == 0:
829 829 tested.update(group)
830 830 good = yield tuple(group)
831 831 else:
832 832 prev_good = good
833 833 for start in range(0, len(group), group_chunk_size):
834 834 sub_group = group[start : start + group_chunk_size]
835 835 tested.update(sub_group)
836 836 good = yield tuple(sub_group)
837 837 if prev_good == good:
838 838 break
839 839
840 840 yield None
841 841
842 842
843 843 def _refinedgroups(revlog, p1, p2, cachedelta, snapshot_cache=None):
844 844 good = None
845 845 # First we try to reuse a the delta contained in the bundle.
846 846 # (or from the source revlog)
847 847 #
848 848 # This logic only applies to general delta repositories and can be disabled
849 849 # through configuration. Disabling reuse source delta is useful when
850 850 # we want to make sure we recomputed "optimal" deltas.
851 851 debug_info = None
852 852 if cachedelta is not None and cachedelta[2] > DELTA_BASE_REUSE_NO:
853 853 # Assume what we received from the server is a good choice
854 854 # build delta will reuse the cache
855 855 if debug_info is not None:
856 856 debug_info['cached-delta.tested'] += 1
857 857 good = yield (cachedelta[0],)
858 858 if good is not None:
859 859 if debug_info is not None:
860 860 debug_info['cached-delta.accepted'] += 1
861 861 yield None
862 862 return
863 863 if snapshot_cache is None:
864 864 snapshot_cache = SnapshotCache()
865 865 groups = _rawgroups(
866 866 revlog,
867 867 p1,
868 868 p2,
869 869 cachedelta,
870 870 snapshot_cache,
871 871 )
872 872 for candidates in groups:
873 873 good = yield candidates
874 874 if good is not None:
875 875 break
876 876
877 877 # If sparse revlog is enabled, we can try to refine the available deltas
878 878 if not revlog._sparserevlog:
879 879 yield None
880 880 return
881 881
882 882 # if we have a refinable value, try to refine it
883 883 if good is not None and good not in (p1, p2) and revlog.issnapshot(good):
884 884 # refine snapshot down
885 885 previous = None
886 886 while previous != good:
887 887 previous = good
888 888 base = revlog.deltaparent(good)
889 889 if base == nullrev:
890 890 break
891 891 good = yield (base,)
892 892 # refine snapshot up
893 893 if not snapshot_cache.snapshots:
894 894 snapshot_cache.update(revlog, good + 1)
895 895 previous = None
896 896 while good != previous:
897 897 previous = good
898 898 children = tuple(sorted(c for c in snapshot_cache.snapshots[good]))
899 899 good = yield children
900 900
901 901 if debug_info is not None:
902 902 if good is None:
903 903 debug_info['no-solution'] += 1
904 904
905 905 yield None
906 906
907 907
908 908 def _rawgroups(revlog, p1, p2, cachedelta, snapshot_cache=None):
909 909 """Provides group of revision to be tested as delta base
910 910
911 911 This lower level function focus on emitting delta theorically interresting
912 912 without looking it any practical details.
913 913
914 914 The group order aims at providing fast or small candidates first.
915 915 """
916 916 # Why search for delta base if we cannot use a delta base ?
917 917 assert revlog.delta_config.general_delta
918 918 # also see issue6056
919 919 sparse = revlog._sparserevlog
920 920 curr = len(revlog)
921 921 prev = curr - 1
922 922 deltachain = lambda rev: revlog._deltachain(rev)[0]
923 923
924 924 # exclude already lazy tested base if any
925 925 parents = [p for p in (p1, p2) if p != nullrev]
926 926
927 if not revlog._deltabothparents and len(parents) == 2:
927 if not revlog.delta_config.delta_both_parents and len(parents) == 2:
928 928 parents.sort()
929 929 # To minimize the chance of having to build a fulltext,
930 930 # pick first whichever parent is closest to us (max rev)
931 931 yield (parents[1],)
932 932 # then the other one (min rev) if the first did not fit
933 933 yield (parents[0],)
934 934 elif len(parents) > 0:
935 935 # Test all parents (1 or 2), and keep the best candidate
936 936 yield parents
937 937
938 938 if sparse and parents:
939 939 if snapshot_cache is None:
940 940 # map: base-rev: [snapshot-revs]
941 941 snapshot_cache = SnapshotCache()
942 942 # See if we can use an existing snapshot in the parent chains to use as
943 943 # a base for a new intermediate-snapshot
944 944 #
945 945 # search for snapshot in parents delta chain
946 946 # map: snapshot-level: snapshot-rev
947 947 parents_snaps = collections.defaultdict(set)
948 948 candidate_chains = [deltachain(p) for p in parents]
949 949 for chain in candidate_chains:
950 950 for idx, s in enumerate(chain):
951 951 if not revlog.issnapshot(s):
952 952 break
953 953 parents_snaps[idx].add(s)
954 954 snapfloor = min(parents_snaps[0]) + 1
955 955 snapshot_cache.update(revlog, snapfloor)
956 956 # search for the highest "unrelated" revision
957 957 #
958 958 # Adding snapshots used by "unrelated" revision increase the odd we
959 959 # reuse an independant, yet better snapshot chain.
960 960 #
961 961 # XXX instead of building a set of revisions, we could lazily enumerate
962 962 # over the chains. That would be more efficient, however we stick to
963 963 # simple code for now.
964 964 all_revs = set()
965 965 for chain in candidate_chains:
966 966 all_revs.update(chain)
967 967 other = None
968 968 for r in revlog.revs(prev, snapfloor):
969 969 if r not in all_revs:
970 970 other = r
971 971 break
972 972 if other is not None:
973 973 # To avoid unfair competition, we won't use unrelated intermediate
974 974 # snapshot that are deeper than the ones from the parent delta
975 975 # chain.
976 976 max_depth = max(parents_snaps.keys())
977 977 chain = deltachain(other)
978 978 for depth, s in enumerate(chain):
979 979 if s < snapfloor:
980 980 continue
981 981 if max_depth < depth:
982 982 break
983 983 if not revlog.issnapshot(s):
984 984 break
985 985 parents_snaps[depth].add(s)
986 986 # Test them as possible intermediate snapshot base
987 987 # We test them from highest to lowest level. High level one are more
988 988 # likely to result in small delta
989 989 floor = None
990 990 for idx, snaps in sorted(parents_snaps.items(), reverse=True):
991 991 siblings = set()
992 992 for s in snaps:
993 993 siblings.update(snapshot_cache.snapshots[s])
994 994 # Before considering making a new intermediate snapshot, we check
995 995 # if an existing snapshot, children of base we consider, would be
996 996 # suitable.
997 997 #
998 998 # It give a change to reuse a delta chain "unrelated" to the
999 999 # current revision instead of starting our own. Without such
1000 1000 # re-use, topological branches would keep reopening new chains.
1001 1001 # Creating more and more snapshot as the repository grow.
1002 1002
1003 1003 if floor is not None:
1004 1004 # We only do this for siblings created after the one in our
1005 1005 # parent's delta chain. Those created before has less chances
1006 1006 # to be valid base since our ancestors had to create a new
1007 1007 # snapshot.
1008 1008 siblings = [r for r in siblings if floor < r]
1009 1009 yield tuple(sorted(siblings))
1010 1010 # then test the base from our parent's delta chain.
1011 1011 yield tuple(sorted(snaps))
1012 1012 floor = min(snaps)
1013 1013 # No suitable base found in the parent chain, search if any full
1014 1014 # snapshots emitted since parent's base would be a suitable base for an
1015 1015 # intermediate snapshot.
1016 1016 #
1017 1017 # It give a chance to reuse a delta chain unrelated to the current
1018 1018 # revisions instead of starting our own. Without such re-use,
1019 1019 # topological branches would keep reopening new full chains. Creating
1020 1020 # more and more snapshot as the repository grow.
1021 1021 full = [r for r in snapshot_cache.snapshots[nullrev] if snapfloor <= r]
1022 1022 yield tuple(sorted(full))
1023 1023
1024 1024 if not sparse:
1025 1025 # other approach failed try against prev to hopefully save us a
1026 1026 # fulltext.
1027 1027 yield (prev,)
1028 1028
1029 1029
1030 1030 class SnapshotCache:
1031 1031 __slots__ = ('snapshots', '_start_rev', '_end_rev')
1032 1032
1033 1033 def __init__(self):
1034 1034 self.snapshots = collections.defaultdict(set)
1035 1035 self._start_rev = None
1036 1036 self._end_rev = None
1037 1037
1038 1038 def update(self, revlog, start_rev=0):
1039 1039 """find snapshots from start_rev to tip"""
1040 1040 nb_revs = len(revlog)
1041 1041 end_rev = nb_revs - 1
1042 1042 if start_rev > end_rev:
1043 1043 return # range is empty
1044 1044
1045 1045 if self._start_rev is None:
1046 1046 assert self._end_rev is None
1047 1047 self._update(revlog, start_rev, end_rev)
1048 1048 elif not (self._start_rev <= start_rev and end_rev <= self._end_rev):
1049 1049 if start_rev < self._start_rev:
1050 1050 self._update(revlog, start_rev, self._start_rev - 1)
1051 1051 if self._end_rev < end_rev:
1052 1052 self._update(revlog, self._end_rev + 1, end_rev)
1053 1053
1054 1054 if self._start_rev is None:
1055 1055 assert self._end_rev is None
1056 1056 self._end_rev = end_rev
1057 1057 self._start_rev = start_rev
1058 1058 else:
1059 1059 self._start_rev = min(self._start_rev, start_rev)
1060 1060 self._end_rev = max(self._end_rev, end_rev)
1061 1061 assert self._start_rev <= self._end_rev, (
1062 1062 self._start_rev,
1063 1063 self._end_rev,
1064 1064 )
1065 1065
1066 1066 def _update(self, revlog, start_rev, end_rev):
1067 1067 """internal method that actually do update content"""
1068 1068 assert self._start_rev is None or (
1069 1069 start_rev < self._start_rev or start_rev > self._end_rev
1070 1070 ), (self._start_rev, self._end_rev, start_rev, end_rev)
1071 1071 assert self._start_rev is None or (
1072 1072 end_rev < self._start_rev or end_rev > self._end_rev
1073 1073 ), (self._start_rev, self._end_rev, start_rev, end_rev)
1074 1074 cache = self.snapshots
1075 1075 if hasattr(revlog.index, 'findsnapshots'):
1076 1076 revlog.index.findsnapshots(cache, start_rev, end_rev)
1077 1077 else:
1078 1078 deltaparent = revlog.deltaparent
1079 1079 issnapshot = revlog.issnapshot
1080 1080 for rev in revlog.revs(start_rev, end_rev):
1081 1081 if issnapshot(rev):
1082 1082 cache[deltaparent(rev)].add(rev)
1083 1083
1084 1084
1085 1085 class deltacomputer:
1086 1086 def __init__(
1087 1087 self,
1088 1088 revlog,
1089 1089 write_debug=None,
1090 1090 debug_search=False,
1091 1091 debug_info=None,
1092 1092 ):
1093 1093 self.revlog = revlog
1094 1094 self._write_debug = write_debug
1095 1095 if write_debug is None:
1096 1096 self._debug_search = False
1097 1097 else:
1098 1098 self._debug_search = debug_search
1099 1099 self._debug_info = debug_info
1100 1100 self._snapshot_cache = SnapshotCache()
1101 1101
1102 1102 @property
1103 1103 def _gather_debug(self):
1104 1104 return self._write_debug is not None or self._debug_info is not None
1105 1105
1106 1106 def buildtext(self, revinfo):
1107 1107 """Builds a fulltext version of a revision
1108 1108
1109 1109 revinfo: revisioninfo instance that contains all needed info
1110 1110 """
1111 1111 btext = revinfo.btext
1112 1112 if btext[0] is not None:
1113 1113 return btext[0]
1114 1114
1115 1115 revlog = self.revlog
1116 1116 cachedelta = revinfo.cachedelta
1117 1117 baserev = cachedelta[0]
1118 1118 delta = cachedelta[1]
1119 1119
1120 1120 fulltext = btext[0] = _textfromdelta(
1121 1121 revlog,
1122 1122 baserev,
1123 1123 delta,
1124 1124 revinfo.p1,
1125 1125 revinfo.p2,
1126 1126 revinfo.flags,
1127 1127 revinfo.node,
1128 1128 )
1129 1129 return fulltext
1130 1130
1131 1131 def _builddeltadiff(self, base, revinfo):
1132 1132 revlog = self.revlog
1133 1133 t = self.buildtext(revinfo)
1134 1134 if revlog.iscensored(base):
1135 1135 # deltas based on a censored revision must replace the
1136 1136 # full content in one patch, so delta works everywhere
1137 1137 header = mdiff.replacediffheader(revlog.rawsize(base), len(t))
1138 1138 delta = header + t
1139 1139 else:
1140 1140 ptext = revlog.rawdata(base)
1141 1141 delta = mdiff.textdiff(ptext, t)
1142 1142
1143 1143 return delta
1144 1144
1145 1145 def _builddeltainfo(self, revinfo, base, target_rev=None):
1146 1146 # can we use the cached delta?
1147 1147 revlog = self.revlog
1148 1148 chainbase = revlog.chainbase(base)
1149 1149 if revlog.delta_config.general_delta:
1150 1150 deltabase = base
1151 1151 else:
1152 1152 if target_rev is not None and base != target_rev - 1:
1153 1153 msg = (
1154 1154 b'general delta cannot use delta for something else '
1155 1155 b'than `prev`: %d<-%d'
1156 1156 )
1157 1157 msg %= (base, target_rev)
1158 1158 raise error.ProgrammingError(msg)
1159 1159 deltabase = chainbase
1160 1160 snapshotdepth = None
1161 1161 if revlog._sparserevlog and deltabase == nullrev:
1162 1162 snapshotdepth = 0
1163 1163 elif revlog._sparserevlog and revlog.issnapshot(deltabase):
1164 1164 # A delta chain should always be one full snapshot,
1165 1165 # zero or more semi-snapshots, and zero or more deltas
1166 1166 p1, p2 = revlog.rev(revinfo.p1), revlog.rev(revinfo.p2)
1167 1167 if deltabase not in (p1, p2) and revlog.issnapshot(deltabase):
1168 1168 snapshotdepth = len(revlog._deltachain(deltabase)[0])
1169 1169 delta = None
1170 1170 if revinfo.cachedelta:
1171 1171 cachebase = revinfo.cachedelta[0]
1172 1172 # check if the diff still apply
1173 1173 currentbase = cachebase
1174 1174 while (
1175 1175 currentbase != nullrev
1176 1176 and currentbase != base
1177 1177 and self.revlog.length(currentbase) == 0
1178 1178 ):
1179 1179 currentbase = self.revlog.deltaparent(currentbase)
1180 1180 if self.revlog._lazydelta and currentbase == base:
1181 1181 delta = revinfo.cachedelta[1]
1182 1182 if delta is None:
1183 1183 delta = self._builddeltadiff(base, revinfo)
1184 1184 if self._debug_search:
1185 1185 msg = b"DBG-DELTAS-SEARCH: uncompressed-delta-size=%d\n"
1186 1186 msg %= len(delta)
1187 1187 self._write_debug(msg)
1188 1188 # snapshotdept need to be neither None nor 0 level snapshot
1189 1189 if revlog.upperboundcomp is not None and snapshotdepth:
1190 1190 lowestrealisticdeltalen = len(delta) // revlog.upperboundcomp
1191 1191 snapshotlimit = revinfo.textlen >> snapshotdepth
1192 1192 if self._debug_search:
1193 1193 msg = b"DBG-DELTAS-SEARCH: projected-lower-size=%d\n"
1194 1194 msg %= lowestrealisticdeltalen
1195 1195 self._write_debug(msg)
1196 1196 if snapshotlimit < lowestrealisticdeltalen:
1197 1197 if self._debug_search:
1198 1198 msg = b"DBG-DELTAS-SEARCH: DISCARDED (snapshot limit)\n"
1199 1199 self._write_debug(msg)
1200 1200 return None
1201 1201 if revlog.length(base) < lowestrealisticdeltalen:
1202 1202 if self._debug_search:
1203 1203 msg = b"DBG-DELTAS-SEARCH: DISCARDED (prev size)\n"
1204 1204 self._write_debug(msg)
1205 1205 return None
1206 1206 header, data = revlog.compress(delta)
1207 1207 deltalen = len(header) + len(data)
1208 1208 offset = revlog.end(len(revlog) - 1)
1209 1209 dist = deltalen + offset - revlog.start(chainbase)
1210 1210 chainlen, compresseddeltalen = revlog._chaininfo(base)
1211 1211 chainlen += 1
1212 1212 compresseddeltalen += deltalen
1213 1213
1214 1214 return _deltainfo(
1215 1215 dist,
1216 1216 deltalen,
1217 1217 (header, data),
1218 1218 deltabase,
1219 1219 chainbase,
1220 1220 chainlen,
1221 1221 compresseddeltalen,
1222 1222 snapshotdepth,
1223 1223 )
1224 1224
1225 1225 def _fullsnapshotinfo(self, revinfo, curr):
1226 1226 rawtext = self.buildtext(revinfo)
1227 1227 data = self.revlog.compress(rawtext)
1228 1228 compresseddeltalen = deltalen = dist = len(data[1]) + len(data[0])
1229 1229 deltabase = chainbase = curr
1230 1230 snapshotdepth = 0
1231 1231 chainlen = 1
1232 1232
1233 1233 return _deltainfo(
1234 1234 dist,
1235 1235 deltalen,
1236 1236 data,
1237 1237 deltabase,
1238 1238 chainbase,
1239 1239 chainlen,
1240 1240 compresseddeltalen,
1241 1241 snapshotdepth,
1242 1242 )
1243 1243
1244 1244 def finddeltainfo(self, revinfo, excluded_bases=None, target_rev=None):
1245 1245 """Find an acceptable delta against a candidate revision
1246 1246
1247 1247 revinfo: information about the revision (instance of _revisioninfo)
1248 1248
1249 1249 Returns the first acceptable candidate revision, as ordered by
1250 1250 _candidategroups
1251 1251
1252 1252 If no suitable deltabase is found, we return delta info for a full
1253 1253 snapshot.
1254 1254
1255 1255 `excluded_bases` is an optional set of revision that cannot be used as
1256 1256 a delta base. Use this to recompute delta suitable in censor or strip
1257 1257 context.
1258 1258 """
1259 1259 if target_rev is None:
1260 1260 target_rev = len(self.revlog)
1261 1261
1262 1262 gather_debug = self._gather_debug
1263 1263 cachedelta = revinfo.cachedelta
1264 1264 revlog = self.revlog
1265 1265 p1r = p2r = None
1266 1266
1267 1267 if excluded_bases is None:
1268 1268 excluded_bases = set()
1269 1269
1270 1270 if gather_debug:
1271 1271 start = util.timer()
1272 1272 dbg = self._one_dbg_data()
1273 1273 dbg['revision'] = target_rev
1274 1274 target_revlog = b"UNKNOWN"
1275 1275 target_type = self.revlog.target[0]
1276 1276 target_key = self.revlog.target[1]
1277 1277 if target_type == KIND_CHANGELOG:
1278 1278 target_revlog = b'CHANGELOG:'
1279 1279 elif target_type == KIND_MANIFESTLOG:
1280 1280 target_revlog = b'MANIFESTLOG:'
1281 1281 if target_key:
1282 1282 target_revlog += b'%s:' % target_key
1283 1283 elif target_type == KIND_FILELOG:
1284 1284 target_revlog = b'FILELOG:'
1285 1285 if target_key:
1286 1286 target_revlog += b'%s:' % target_key
1287 1287 dbg['target-revlog'] = target_revlog
1288 1288 p1r = revlog.rev(revinfo.p1)
1289 1289 p2r = revlog.rev(revinfo.p2)
1290 1290 if p1r != nullrev:
1291 1291 p1_chain_len = revlog._chaininfo(p1r)[0]
1292 1292 else:
1293 1293 p1_chain_len = -1
1294 1294 if p2r != nullrev:
1295 1295 p2_chain_len = revlog._chaininfo(p2r)[0]
1296 1296 else:
1297 1297 p2_chain_len = -1
1298 1298 dbg['p1-chain-len'] = p1_chain_len
1299 1299 dbg['p2-chain-len'] = p2_chain_len
1300 1300
1301 1301 # 1) if the revision is empty, no amount of delta can beat it
1302 1302 #
1303 1303 # 2) no delta for flag processor revision (see "candelta" for why)
1304 1304 # not calling candelta since only one revision needs test, also to
1305 1305 # avoid overhead fetching flags again.
1306 1306 if not revinfo.textlen or revinfo.flags & REVIDX_RAWTEXT_CHANGING_FLAGS:
1307 1307 deltainfo = self._fullsnapshotinfo(revinfo, target_rev)
1308 1308 if gather_debug:
1309 1309 end = util.timer()
1310 1310 dbg['duration'] = end - start
1311 1311 dbg[
1312 1312 'delta-base'
1313 1313 ] = deltainfo.base # pytype: disable=attribute-error
1314 1314 dbg['search_round_count'] = 0
1315 1315 dbg['using-cached-base'] = False
1316 1316 dbg['delta_try_count'] = 0
1317 1317 dbg['type'] = b"full"
1318 1318 dbg['snapshot-depth'] = 0
1319 1319 self._dbg_process_data(dbg)
1320 1320 return deltainfo
1321 1321
1322 1322 deltainfo = None
1323 1323
1324 1324 # If this source delta are to be forcibly reuse, let us comply early.
1325 1325 if (
1326 1326 revlog.delta_config.general_delta
1327 1327 and revinfo.cachedelta is not None
1328 1328 and revinfo.cachedelta[2] == DELTA_BASE_REUSE_FORCE
1329 1329 ):
1330 1330 base = revinfo.cachedelta[0]
1331 1331 if base == nullrev:
1332 1332 dbg_type = b"full"
1333 1333 deltainfo = self._fullsnapshotinfo(revinfo, target_rev)
1334 1334 if gather_debug:
1335 1335 snapshotdepth = 0
1336 1336 elif base not in excluded_bases:
1337 1337 delta = revinfo.cachedelta[1]
1338 1338 header, data = revlog.compress(delta)
1339 1339 deltalen = len(header) + len(data)
1340 1340 if gather_debug:
1341 1341 offset = revlog.end(len(revlog) - 1)
1342 1342 chainbase = revlog.chainbase(base)
1343 1343 distance = deltalen + offset - revlog.start(chainbase)
1344 1344 chainlen, compresseddeltalen = revlog._chaininfo(base)
1345 1345 chainlen += 1
1346 1346 compresseddeltalen += deltalen
1347 1347 if base == p1r or base == p2r:
1348 1348 dbg_type = b"delta"
1349 1349 snapshotdepth = None
1350 1350 elif not revlog.issnapshot(base):
1351 1351 snapshotdepth = None
1352 1352 else:
1353 1353 dbg_type = b"snapshot"
1354 1354 snapshotdepth = revlog.snapshotdepth(base) + 1
1355 1355 else:
1356 1356 distance = None
1357 1357 chainbase = None
1358 1358 chainlen = None
1359 1359 compresseddeltalen = None
1360 1360 snapshotdepth = None
1361 1361 deltainfo = _deltainfo(
1362 1362 distance=distance,
1363 1363 deltalen=deltalen,
1364 1364 data=(header, data),
1365 1365 base=base,
1366 1366 chainbase=chainbase,
1367 1367 chainlen=chainlen,
1368 1368 compresseddeltalen=compresseddeltalen,
1369 1369 snapshotdepth=snapshotdepth,
1370 1370 )
1371 1371
1372 1372 if deltainfo is not None:
1373 1373 if gather_debug:
1374 1374 end = util.timer()
1375 1375 dbg['duration'] = end - start
1376 1376 dbg[
1377 1377 'delta-base'
1378 1378 ] = deltainfo.base # pytype: disable=attribute-error
1379 1379 dbg['search_round_count'] = 0
1380 1380 dbg['using-cached-base'] = True
1381 1381 dbg['delta_try_count'] = 0
1382 1382 dbg['type'] = b"full"
1383 1383 if snapshotdepth is None:
1384 1384 dbg['snapshot-depth'] = 0
1385 1385 else:
1386 1386 dbg['snapshot-depth'] = snapshotdepth
1387 1387 self._dbg_process_data(dbg)
1388 1388 return deltainfo
1389 1389
1390 1390 # count the number of different delta we tried (for debug purpose)
1391 1391 dbg_try_count = 0
1392 1392 # count the number of "search round" we did. (for debug purpose)
1393 1393 dbg_try_rounds = 0
1394 1394 dbg_type = b'unknown'
1395 1395
1396 1396 if p1r is None:
1397 1397 p1r = revlog.rev(revinfo.p1)
1398 1398 p2r = revlog.rev(revinfo.p2)
1399 1399
1400 1400 if self._debug_search:
1401 1401 msg = b"DBG-DELTAS-SEARCH: SEARCH rev=%d\n"
1402 1402 msg %= target_rev
1403 1403 self._write_debug(msg)
1404 1404
1405 1405 groups = _candidategroups(
1406 1406 self.revlog,
1407 1407 revinfo.textlen,
1408 1408 p1r,
1409 1409 p2r,
1410 1410 cachedelta,
1411 1411 excluded_bases,
1412 1412 target_rev,
1413 1413 snapshot_cache=self._snapshot_cache,
1414 1414 )
1415 1415 candidaterevs = next(groups)
1416 1416 while candidaterevs is not None:
1417 1417 dbg_try_rounds += 1
1418 1418 if self._debug_search:
1419 1419 prev = None
1420 1420 if deltainfo is not None:
1421 1421 prev = deltainfo.base
1422 1422
1423 1423 if (
1424 1424 cachedelta is not None
1425 1425 and len(candidaterevs) == 1
1426 1426 and cachedelta[0] in candidaterevs
1427 1427 ):
1428 1428 round_type = b"cached-delta"
1429 1429 elif p1r in candidaterevs or p2r in candidaterevs:
1430 1430 round_type = b"parents"
1431 1431 elif prev is not None and all(c < prev for c in candidaterevs):
1432 1432 round_type = b"refine-down"
1433 1433 elif prev is not None and all(c > prev for c in candidaterevs):
1434 1434 round_type = b"refine-up"
1435 1435 else:
1436 1436 round_type = b"search-down"
1437 1437 msg = b"DBG-DELTAS-SEARCH: ROUND #%d - %d candidates - %s\n"
1438 1438 msg %= (dbg_try_rounds, len(candidaterevs), round_type)
1439 1439 self._write_debug(msg)
1440 1440 nominateddeltas = []
1441 1441 if deltainfo is not None:
1442 1442 if self._debug_search:
1443 1443 msg = (
1444 1444 b"DBG-DELTAS-SEARCH: CONTENDER: rev=%d - length=%d\n"
1445 1445 )
1446 1446 msg %= (deltainfo.base, deltainfo.deltalen)
1447 1447 self._write_debug(msg)
1448 1448 # if we already found a good delta,
1449 1449 # challenge it against refined candidates
1450 1450 nominateddeltas.append(deltainfo)
1451 1451 for candidaterev in candidaterevs:
1452 1452 if self._debug_search:
1453 1453 msg = b"DBG-DELTAS-SEARCH: CANDIDATE: rev=%d\n"
1454 1454 msg %= candidaterev
1455 1455 self._write_debug(msg)
1456 1456 candidate_type = None
1457 1457 if candidaterev == p1r:
1458 1458 candidate_type = b"p1"
1459 1459 elif candidaterev == p2r:
1460 1460 candidate_type = b"p2"
1461 1461 elif self.revlog.issnapshot(candidaterev):
1462 1462 candidate_type = b"snapshot-%d"
1463 1463 candidate_type %= self.revlog.snapshotdepth(
1464 1464 candidaterev
1465 1465 )
1466 1466
1467 1467 if candidate_type is not None:
1468 1468 msg = b"DBG-DELTAS-SEARCH: type=%s\n"
1469 1469 msg %= candidate_type
1470 1470 self._write_debug(msg)
1471 1471 msg = b"DBG-DELTAS-SEARCH: size=%d\n"
1472 1472 msg %= self.revlog.length(candidaterev)
1473 1473 self._write_debug(msg)
1474 1474 msg = b"DBG-DELTAS-SEARCH: base=%d\n"
1475 1475 msg %= self.revlog.deltaparent(candidaterev)
1476 1476 self._write_debug(msg)
1477 1477
1478 1478 dbg_try_count += 1
1479 1479
1480 1480 if self._debug_search:
1481 1481 delta_start = util.timer()
1482 1482 candidatedelta = self._builddeltainfo(
1483 1483 revinfo,
1484 1484 candidaterev,
1485 1485 target_rev=target_rev,
1486 1486 )
1487 1487 if self._debug_search:
1488 1488 delta_end = util.timer()
1489 1489 msg = b"DBG-DELTAS-SEARCH: delta-search-time=%f\n"
1490 1490 msg %= delta_end - delta_start
1491 1491 self._write_debug(msg)
1492 1492 if candidatedelta is not None:
1493 1493 if is_good_delta_info(self.revlog, candidatedelta, revinfo):
1494 1494 if self._debug_search:
1495 1495 msg = b"DBG-DELTAS-SEARCH: DELTA: length=%d (GOOD)\n"
1496 1496 msg %= candidatedelta.deltalen
1497 1497 self._write_debug(msg)
1498 1498 nominateddeltas.append(candidatedelta)
1499 1499 elif self._debug_search:
1500 1500 msg = b"DBG-DELTAS-SEARCH: DELTA: length=%d (BAD)\n"
1501 1501 msg %= candidatedelta.deltalen
1502 1502 self._write_debug(msg)
1503 1503 elif self._debug_search:
1504 1504 msg = b"DBG-DELTAS-SEARCH: NO-DELTA\n"
1505 1505 self._write_debug(msg)
1506 1506 if nominateddeltas:
1507 1507 deltainfo = min(nominateddeltas, key=lambda x: x.deltalen)
1508 1508 if deltainfo is not None:
1509 1509 candidaterevs = groups.send(deltainfo.base)
1510 1510 else:
1511 1511 candidaterevs = next(groups)
1512 1512
1513 1513 if deltainfo is None:
1514 1514 dbg_type = b"full"
1515 1515 deltainfo = self._fullsnapshotinfo(revinfo, target_rev)
1516 1516 elif deltainfo.snapshotdepth: # pytype: disable=attribute-error
1517 1517 dbg_type = b"snapshot"
1518 1518 else:
1519 1519 dbg_type = b"delta"
1520 1520
1521 1521 if gather_debug:
1522 1522 end = util.timer()
1523 1523 if dbg_type == b'full':
1524 1524 used_cached = (
1525 1525 cachedelta is not None
1526 1526 and dbg_try_rounds == 0
1527 1527 and dbg_try_count == 0
1528 1528 and cachedelta[0] == nullrev
1529 1529 )
1530 1530 else:
1531 1531 used_cached = (
1532 1532 cachedelta is not None
1533 1533 and dbg_try_rounds == 1
1534 1534 and dbg_try_count == 1
1535 1535 and deltainfo.base == cachedelta[0]
1536 1536 )
1537 1537 dbg['duration'] = end - start
1538 1538 dbg[
1539 1539 'delta-base'
1540 1540 ] = deltainfo.base # pytype: disable=attribute-error
1541 1541 dbg['search_round_count'] = dbg_try_rounds
1542 1542 dbg['using-cached-base'] = used_cached
1543 1543 dbg['delta_try_count'] = dbg_try_count
1544 1544 dbg['type'] = dbg_type
1545 1545 if (
1546 1546 deltainfo.snapshotdepth # pytype: disable=attribute-error
1547 1547 is not None
1548 1548 ):
1549 1549 dbg[
1550 1550 'snapshot-depth'
1551 1551 ] = deltainfo.snapshotdepth # pytype: disable=attribute-error
1552 1552 else:
1553 1553 dbg['snapshot-depth'] = 0
1554 1554 self._dbg_process_data(dbg)
1555 1555 return deltainfo
1556 1556
1557 1557 def _one_dbg_data(self):
1558 1558 return {
1559 1559 'duration': None,
1560 1560 'revision': None,
1561 1561 'delta-base': None,
1562 1562 'search_round_count': None,
1563 1563 'using-cached-base': None,
1564 1564 'delta_try_count': None,
1565 1565 'type': None,
1566 1566 'p1-chain-len': None,
1567 1567 'p2-chain-len': None,
1568 1568 'snapshot-depth': None,
1569 1569 'target-revlog': None,
1570 1570 }
1571 1571
1572 1572 def _dbg_process_data(self, dbg):
1573 1573 if self._debug_info is not None:
1574 1574 self._debug_info.append(dbg)
1575 1575
1576 1576 if self._write_debug is not None:
1577 1577 msg = (
1578 1578 b"DBG-DELTAS:"
1579 1579 b" %-12s"
1580 1580 b" rev=%d:"
1581 1581 b" delta-base=%d"
1582 1582 b" is-cached=%d"
1583 1583 b" - search-rounds=%d"
1584 1584 b" try-count=%d"
1585 1585 b" - delta-type=%-6s"
1586 1586 b" snap-depth=%d"
1587 1587 b" - p1-chain-length=%d"
1588 1588 b" p2-chain-length=%d"
1589 1589 b" - duration=%f"
1590 1590 b"\n"
1591 1591 )
1592 1592 msg %= (
1593 1593 dbg["target-revlog"],
1594 1594 dbg["revision"],
1595 1595 dbg["delta-base"],
1596 1596 dbg["using-cached-base"],
1597 1597 dbg["search_round_count"],
1598 1598 dbg["delta_try_count"],
1599 1599 dbg["type"],
1600 1600 dbg["snapshot-depth"],
1601 1601 dbg["p1-chain-len"],
1602 1602 dbg["p2-chain-len"],
1603 1603 dbg["duration"],
1604 1604 )
1605 1605 self._write_debug(msg)
1606 1606
1607 1607
1608 1608 def delta_compression(default_compression_header, deltainfo):
1609 1609 """return (COMPRESSION_MODE, deltainfo)
1610 1610
1611 1611 used by revlog v2+ format to dispatch between PLAIN and DEFAULT
1612 1612 compression.
1613 1613 """
1614 1614 h, d = deltainfo.data
1615 1615 compression_mode = COMP_MODE_INLINE
1616 1616 if not h and not d:
1617 1617 # not data to store at all... declare them uncompressed
1618 1618 compression_mode = COMP_MODE_PLAIN
1619 1619 elif not h:
1620 1620 t = d[0:1]
1621 1621 if t == b'\0':
1622 1622 compression_mode = COMP_MODE_PLAIN
1623 1623 elif t == default_compression_header:
1624 1624 compression_mode = COMP_MODE_DEFAULT
1625 1625 elif h == b'u':
1626 1626 # we have a more efficient way to declare uncompressed
1627 1627 h = b''
1628 1628 compression_mode = COMP_MODE_PLAIN
1629 1629 deltainfo = drop_u_compression(deltainfo)
1630 1630 return compression_mode, deltainfo
General Comments 0
You need to be logged in to leave comments. Login now