##// END OF EJS Templates
revlog: remove legacy usage of `_lazydeltabase`...
marmoute -
r51960:26dcdbe1 default
parent child Browse files
Show More
@@ -1,4623 +1,4626 b''
1 # perf.py - performance test routines
1 # perf.py - performance test routines
2 '''helper extension to measure performance
2 '''helper extension to measure performance
3
3
4 Configurations
4 Configurations
5 ==============
5 ==============
6
6
7 ``perf``
7 ``perf``
8 --------
8 --------
9
9
10 ``all-timing``
10 ``all-timing``
11 When set, additional statistics will be reported for each benchmark: best,
11 When set, additional statistics will be reported for each benchmark: best,
12 worst, median average. If not set only the best timing is reported
12 worst, median average. If not set only the best timing is reported
13 (default: off).
13 (default: off).
14
14
15 ``presleep``
15 ``presleep``
16 number of second to wait before any group of runs (default: 1)
16 number of second to wait before any group of runs (default: 1)
17
17
18 ``pre-run``
18 ``pre-run``
19 number of run to perform before starting measurement.
19 number of run to perform before starting measurement.
20
20
21 ``profile-benchmark``
21 ``profile-benchmark``
22 Enable profiling for the benchmarked section.
22 Enable profiling for the benchmarked section.
23 (The first iteration is benchmarked)
23 (The first iteration is benchmarked)
24
24
25 ``run-limits``
25 ``run-limits``
26 Control the number of runs each benchmark will perform. The option value
26 Control the number of runs each benchmark will perform. The option value
27 should be a list of `<time>-<numberofrun>` pairs. After each run the
27 should be a list of `<time>-<numberofrun>` pairs. After each run the
28 conditions are considered in order with the following logic:
28 conditions are considered in order with the following logic:
29
29
30 If benchmark has been running for <time> seconds, and we have performed
30 If benchmark has been running for <time> seconds, and we have performed
31 <numberofrun> iterations, stop the benchmark,
31 <numberofrun> iterations, stop the benchmark,
32
32
33 The default value is: `3.0-100, 10.0-3`
33 The default value is: `3.0-100, 10.0-3`
34
34
35 ``stub``
35 ``stub``
36 When set, benchmarks will only be run once, useful for testing
36 When set, benchmarks will only be run once, useful for testing
37 (default: off)
37 (default: off)
38 '''
38 '''
39
39
40 # "historical portability" policy of perf.py:
40 # "historical portability" policy of perf.py:
41 #
41 #
42 # We have to do:
42 # We have to do:
43 # - make perf.py "loadable" with as wide Mercurial version as possible
43 # - make perf.py "loadable" with as wide Mercurial version as possible
44 # This doesn't mean that perf commands work correctly with that Mercurial.
44 # This doesn't mean that perf commands work correctly with that Mercurial.
45 # BTW, perf.py itself has been available since 1.1 (or eb240755386d).
45 # BTW, perf.py itself has been available since 1.1 (or eb240755386d).
46 # - make historical perf command work correctly with as wide Mercurial
46 # - make historical perf command work correctly with as wide Mercurial
47 # version as possible
47 # version as possible
48 #
48 #
49 # We have to do, if possible with reasonable cost:
49 # We have to do, if possible with reasonable cost:
50 # - make recent perf command for historical feature work correctly
50 # - make recent perf command for historical feature work correctly
51 # with early Mercurial
51 # with early Mercurial
52 #
52 #
53 # We don't have to do:
53 # We don't have to do:
54 # - make perf command for recent feature work correctly with early
54 # - make perf command for recent feature work correctly with early
55 # Mercurial
55 # Mercurial
56
56
57 import contextlib
57 import contextlib
58 import functools
58 import functools
59 import gc
59 import gc
60 import os
60 import os
61 import random
61 import random
62 import shutil
62 import shutil
63 import struct
63 import struct
64 import sys
64 import sys
65 import tempfile
65 import tempfile
66 import threading
66 import threading
67 import time
67 import time
68
68
69 import mercurial.revlog
69 import mercurial.revlog
70 from mercurial import (
70 from mercurial import (
71 changegroup,
71 changegroup,
72 cmdutil,
72 cmdutil,
73 commands,
73 commands,
74 copies,
74 copies,
75 error,
75 error,
76 extensions,
76 extensions,
77 hg,
77 hg,
78 mdiff,
78 mdiff,
79 merge,
79 merge,
80 util,
80 util,
81 )
81 )
82
82
83 # for "historical portability":
83 # for "historical portability":
84 # try to import modules separately (in dict order), and ignore
84 # try to import modules separately (in dict order), and ignore
85 # failure, because these aren't available with early Mercurial
85 # failure, because these aren't available with early Mercurial
86 try:
86 try:
87 from mercurial import branchmap # since 2.5 (or bcee63733aad)
87 from mercurial import branchmap # since 2.5 (or bcee63733aad)
88 except ImportError:
88 except ImportError:
89 pass
89 pass
90 try:
90 try:
91 from mercurial import obsolete # since 2.3 (or ad0d6c2b3279)
91 from mercurial import obsolete # since 2.3 (or ad0d6c2b3279)
92 except ImportError:
92 except ImportError:
93 pass
93 pass
94 try:
94 try:
95 from mercurial import registrar # since 3.7 (or 37d50250b696)
95 from mercurial import registrar # since 3.7 (or 37d50250b696)
96
96
97 dir(registrar) # forcibly load it
97 dir(registrar) # forcibly load it
98 except ImportError:
98 except ImportError:
99 registrar = None
99 registrar = None
100 try:
100 try:
101 from mercurial import repoview # since 2.5 (or 3a6ddacb7198)
101 from mercurial import repoview # since 2.5 (or 3a6ddacb7198)
102 except ImportError:
102 except ImportError:
103 pass
103 pass
104 try:
104 try:
105 from mercurial.utils import repoviewutil # since 5.0
105 from mercurial.utils import repoviewutil # since 5.0
106 except ImportError:
106 except ImportError:
107 repoviewutil = None
107 repoviewutil = None
108 try:
108 try:
109 from mercurial import scmutil # since 1.9 (or 8b252e826c68)
109 from mercurial import scmutil # since 1.9 (or 8b252e826c68)
110 except ImportError:
110 except ImportError:
111 pass
111 pass
112 try:
112 try:
113 from mercurial import setdiscovery # since 1.9 (or cb98fed52495)
113 from mercurial import setdiscovery # since 1.9 (or cb98fed52495)
114 except ImportError:
114 except ImportError:
115 pass
115 pass
116
116
117 try:
117 try:
118 from mercurial import profiling
118 from mercurial import profiling
119 except ImportError:
119 except ImportError:
120 profiling = None
120 profiling = None
121
121
122 try:
122 try:
123 from mercurial.revlogutils import constants as revlog_constants
123 from mercurial.revlogutils import constants as revlog_constants
124
124
125 perf_rl_kind = (revlog_constants.KIND_OTHER, b'created-by-perf')
125 perf_rl_kind = (revlog_constants.KIND_OTHER, b'created-by-perf')
126
126
127 def revlog(opener, *args, **kwargs):
127 def revlog(opener, *args, **kwargs):
128 return mercurial.revlog.revlog(opener, perf_rl_kind, *args, **kwargs)
128 return mercurial.revlog.revlog(opener, perf_rl_kind, *args, **kwargs)
129
129
130
130
131 except (ImportError, AttributeError):
131 except (ImportError, AttributeError):
132 perf_rl_kind = None
132 perf_rl_kind = None
133
133
134 def revlog(opener, *args, **kwargs):
134 def revlog(opener, *args, **kwargs):
135 return mercurial.revlog.revlog(opener, *args, **kwargs)
135 return mercurial.revlog.revlog(opener, *args, **kwargs)
136
136
137
137
138 def identity(a):
138 def identity(a):
139 return a
139 return a
140
140
141
141
142 try:
142 try:
143 from mercurial import pycompat
143 from mercurial import pycompat
144
144
145 getargspec = pycompat.getargspec # added to module after 4.5
145 getargspec = pycompat.getargspec # added to module after 4.5
146 _byteskwargs = pycompat.byteskwargs # since 4.1 (or fbc3f73dc802)
146 _byteskwargs = pycompat.byteskwargs # since 4.1 (or fbc3f73dc802)
147 _sysstr = pycompat.sysstr # since 4.0 (or 2219f4f82ede)
147 _sysstr = pycompat.sysstr # since 4.0 (or 2219f4f82ede)
148 _bytestr = pycompat.bytestr # since 4.2 (or b70407bd84d5)
148 _bytestr = pycompat.bytestr # since 4.2 (or b70407bd84d5)
149 _xrange = pycompat.xrange # since 4.8 (or 7eba8f83129b)
149 _xrange = pycompat.xrange # since 4.8 (or 7eba8f83129b)
150 fsencode = pycompat.fsencode # since 3.9 (or f4a5e0e86a7e)
150 fsencode = pycompat.fsencode # since 3.9 (or f4a5e0e86a7e)
151 if pycompat.ispy3:
151 if pycompat.ispy3:
152 _maxint = sys.maxsize # per py3 docs for replacing maxint
152 _maxint = sys.maxsize # per py3 docs for replacing maxint
153 else:
153 else:
154 _maxint = sys.maxint
154 _maxint = sys.maxint
155 except (NameError, ImportError, AttributeError):
155 except (NameError, ImportError, AttributeError):
156 import inspect
156 import inspect
157
157
158 getargspec = inspect.getargspec
158 getargspec = inspect.getargspec
159 _byteskwargs = identity
159 _byteskwargs = identity
160 _bytestr = str
160 _bytestr = str
161 fsencode = identity # no py3 support
161 fsencode = identity # no py3 support
162 _maxint = sys.maxint # no py3 support
162 _maxint = sys.maxint # no py3 support
163 _sysstr = lambda x: x # no py3 support
163 _sysstr = lambda x: x # no py3 support
164 _xrange = xrange
164 _xrange = xrange
165
165
166 try:
166 try:
167 # 4.7+
167 # 4.7+
168 queue = pycompat.queue.Queue
168 queue = pycompat.queue.Queue
169 except (NameError, AttributeError, ImportError):
169 except (NameError, AttributeError, ImportError):
170 # <4.7.
170 # <4.7.
171 try:
171 try:
172 queue = pycompat.queue
172 queue = pycompat.queue
173 except (NameError, AttributeError, ImportError):
173 except (NameError, AttributeError, ImportError):
174 import Queue as queue
174 import Queue as queue
175
175
176 try:
176 try:
177 from mercurial import logcmdutil
177 from mercurial import logcmdutil
178
178
179 makelogtemplater = logcmdutil.maketemplater
179 makelogtemplater = logcmdutil.maketemplater
180 except (AttributeError, ImportError):
180 except (AttributeError, ImportError):
181 try:
181 try:
182 makelogtemplater = cmdutil.makelogtemplater
182 makelogtemplater = cmdutil.makelogtemplater
183 except (AttributeError, ImportError):
183 except (AttributeError, ImportError):
184 makelogtemplater = None
184 makelogtemplater = None
185
185
186 # for "historical portability":
186 # for "historical portability":
187 # define util.safehasattr forcibly, because util.safehasattr has been
187 # define util.safehasattr forcibly, because util.safehasattr has been
188 # available since 1.9.3 (or 94b200a11cf7)
188 # available since 1.9.3 (or 94b200a11cf7)
189 _undefined = object()
189 _undefined = object()
190
190
191
191
192 def safehasattr(thing, attr):
192 def safehasattr(thing, attr):
193 return getattr(thing, _sysstr(attr), _undefined) is not _undefined
193 return getattr(thing, _sysstr(attr), _undefined) is not _undefined
194
194
195
195
196 setattr(util, 'safehasattr', safehasattr)
196 setattr(util, 'safehasattr', safehasattr)
197
197
198 # for "historical portability":
198 # for "historical portability":
199 # define util.timer forcibly, because util.timer has been available
199 # define util.timer forcibly, because util.timer has been available
200 # since ae5d60bb70c9
200 # since ae5d60bb70c9
201 if safehasattr(time, 'perf_counter'):
201 if safehasattr(time, 'perf_counter'):
202 util.timer = time.perf_counter
202 util.timer = time.perf_counter
203 elif os.name == b'nt':
203 elif os.name == b'nt':
204 util.timer = time.clock
204 util.timer = time.clock
205 else:
205 else:
206 util.timer = time.time
206 util.timer = time.time
207
207
208 # for "historical portability":
208 # for "historical portability":
209 # use locally defined empty option list, if formatteropts isn't
209 # use locally defined empty option list, if formatteropts isn't
210 # available, because commands.formatteropts has been available since
210 # available, because commands.formatteropts has been available since
211 # 3.2 (or 7a7eed5176a4), even though formatting itself has been
211 # 3.2 (or 7a7eed5176a4), even though formatting itself has been
212 # available since 2.2 (or ae5f92e154d3)
212 # available since 2.2 (or ae5f92e154d3)
213 formatteropts = getattr(
213 formatteropts = getattr(
214 cmdutil, "formatteropts", getattr(commands, "formatteropts", [])
214 cmdutil, "formatteropts", getattr(commands, "formatteropts", [])
215 )
215 )
216
216
217 # for "historical portability":
217 # for "historical portability":
218 # use locally defined option list, if debugrevlogopts isn't available,
218 # use locally defined option list, if debugrevlogopts isn't available,
219 # because commands.debugrevlogopts has been available since 3.7 (or
219 # because commands.debugrevlogopts has been available since 3.7 (or
220 # 5606f7d0d063), even though cmdutil.openrevlog() has been available
220 # 5606f7d0d063), even though cmdutil.openrevlog() has been available
221 # since 1.9 (or a79fea6b3e77).
221 # since 1.9 (or a79fea6b3e77).
222 revlogopts = getattr(
222 revlogopts = getattr(
223 cmdutil,
223 cmdutil,
224 "debugrevlogopts",
224 "debugrevlogopts",
225 getattr(
225 getattr(
226 commands,
226 commands,
227 "debugrevlogopts",
227 "debugrevlogopts",
228 [
228 [
229 (b'c', b'changelog', False, b'open changelog'),
229 (b'c', b'changelog', False, b'open changelog'),
230 (b'm', b'manifest', False, b'open manifest'),
230 (b'm', b'manifest', False, b'open manifest'),
231 (b'', b'dir', False, b'open directory manifest'),
231 (b'', b'dir', False, b'open directory manifest'),
232 ],
232 ],
233 ),
233 ),
234 )
234 )
235
235
236 cmdtable = {}
236 cmdtable = {}
237
237
238
238
239 # for "historical portability":
239 # for "historical portability":
240 # define parsealiases locally, because cmdutil.parsealiases has been
240 # define parsealiases locally, because cmdutil.parsealiases has been
241 # available since 1.5 (or 6252852b4332)
241 # available since 1.5 (or 6252852b4332)
242 def parsealiases(cmd):
242 def parsealiases(cmd):
243 return cmd.split(b"|")
243 return cmd.split(b"|")
244
244
245
245
246 if safehasattr(registrar, 'command'):
246 if safehasattr(registrar, 'command'):
247 command = registrar.command(cmdtable)
247 command = registrar.command(cmdtable)
248 elif safehasattr(cmdutil, 'command'):
248 elif safehasattr(cmdutil, 'command'):
249 command = cmdutil.command(cmdtable)
249 command = cmdutil.command(cmdtable)
250 if 'norepo' not in getargspec(command).args:
250 if 'norepo' not in getargspec(command).args:
251 # for "historical portability":
251 # for "historical portability":
252 # wrap original cmdutil.command, because "norepo" option has
252 # wrap original cmdutil.command, because "norepo" option has
253 # been available since 3.1 (or 75a96326cecb)
253 # been available since 3.1 (or 75a96326cecb)
254 _command = command
254 _command = command
255
255
256 def command(name, options=(), synopsis=None, norepo=False):
256 def command(name, options=(), synopsis=None, norepo=False):
257 if norepo:
257 if norepo:
258 commands.norepo += b' %s' % b' '.join(parsealiases(name))
258 commands.norepo += b' %s' % b' '.join(parsealiases(name))
259 return _command(name, list(options), synopsis)
259 return _command(name, list(options), synopsis)
260
260
261
261
262 else:
262 else:
263 # for "historical portability":
263 # for "historical portability":
264 # define "@command" annotation locally, because cmdutil.command
264 # define "@command" annotation locally, because cmdutil.command
265 # has been available since 1.9 (or 2daa5179e73f)
265 # has been available since 1.9 (or 2daa5179e73f)
266 def command(name, options=(), synopsis=None, norepo=False):
266 def command(name, options=(), synopsis=None, norepo=False):
267 def decorator(func):
267 def decorator(func):
268 if synopsis:
268 if synopsis:
269 cmdtable[name] = func, list(options), synopsis
269 cmdtable[name] = func, list(options), synopsis
270 else:
270 else:
271 cmdtable[name] = func, list(options)
271 cmdtable[name] = func, list(options)
272 if norepo:
272 if norepo:
273 commands.norepo += b' %s' % b' '.join(parsealiases(name))
273 commands.norepo += b' %s' % b' '.join(parsealiases(name))
274 return func
274 return func
275
275
276 return decorator
276 return decorator
277
277
278
278
279 try:
279 try:
280 import mercurial.registrar
280 import mercurial.registrar
281 import mercurial.configitems
281 import mercurial.configitems
282
282
283 configtable = {}
283 configtable = {}
284 configitem = mercurial.registrar.configitem(configtable)
284 configitem = mercurial.registrar.configitem(configtable)
285 configitem(
285 configitem(
286 b'perf',
286 b'perf',
287 b'presleep',
287 b'presleep',
288 default=mercurial.configitems.dynamicdefault,
288 default=mercurial.configitems.dynamicdefault,
289 experimental=True,
289 experimental=True,
290 )
290 )
291 configitem(
291 configitem(
292 b'perf',
292 b'perf',
293 b'stub',
293 b'stub',
294 default=mercurial.configitems.dynamicdefault,
294 default=mercurial.configitems.dynamicdefault,
295 experimental=True,
295 experimental=True,
296 )
296 )
297 configitem(
297 configitem(
298 b'perf',
298 b'perf',
299 b'parentscount',
299 b'parentscount',
300 default=mercurial.configitems.dynamicdefault,
300 default=mercurial.configitems.dynamicdefault,
301 experimental=True,
301 experimental=True,
302 )
302 )
303 configitem(
303 configitem(
304 b'perf',
304 b'perf',
305 b'all-timing',
305 b'all-timing',
306 default=mercurial.configitems.dynamicdefault,
306 default=mercurial.configitems.dynamicdefault,
307 experimental=True,
307 experimental=True,
308 )
308 )
309 configitem(
309 configitem(
310 b'perf',
310 b'perf',
311 b'pre-run',
311 b'pre-run',
312 default=mercurial.configitems.dynamicdefault,
312 default=mercurial.configitems.dynamicdefault,
313 )
313 )
314 configitem(
314 configitem(
315 b'perf',
315 b'perf',
316 b'profile-benchmark',
316 b'profile-benchmark',
317 default=mercurial.configitems.dynamicdefault,
317 default=mercurial.configitems.dynamicdefault,
318 )
318 )
319 configitem(
319 configitem(
320 b'perf',
320 b'perf',
321 b'run-limits',
321 b'run-limits',
322 default=mercurial.configitems.dynamicdefault,
322 default=mercurial.configitems.dynamicdefault,
323 experimental=True,
323 experimental=True,
324 )
324 )
325 except (ImportError, AttributeError):
325 except (ImportError, AttributeError):
326 pass
326 pass
327 except TypeError:
327 except TypeError:
328 # compatibility fix for a11fd395e83f
328 # compatibility fix for a11fd395e83f
329 # hg version: 5.2
329 # hg version: 5.2
330 configitem(
330 configitem(
331 b'perf',
331 b'perf',
332 b'presleep',
332 b'presleep',
333 default=mercurial.configitems.dynamicdefault,
333 default=mercurial.configitems.dynamicdefault,
334 )
334 )
335 configitem(
335 configitem(
336 b'perf',
336 b'perf',
337 b'stub',
337 b'stub',
338 default=mercurial.configitems.dynamicdefault,
338 default=mercurial.configitems.dynamicdefault,
339 )
339 )
340 configitem(
340 configitem(
341 b'perf',
341 b'perf',
342 b'parentscount',
342 b'parentscount',
343 default=mercurial.configitems.dynamicdefault,
343 default=mercurial.configitems.dynamicdefault,
344 )
344 )
345 configitem(
345 configitem(
346 b'perf',
346 b'perf',
347 b'all-timing',
347 b'all-timing',
348 default=mercurial.configitems.dynamicdefault,
348 default=mercurial.configitems.dynamicdefault,
349 )
349 )
350 configitem(
350 configitem(
351 b'perf',
351 b'perf',
352 b'pre-run',
352 b'pre-run',
353 default=mercurial.configitems.dynamicdefault,
353 default=mercurial.configitems.dynamicdefault,
354 )
354 )
355 configitem(
355 configitem(
356 b'perf',
356 b'perf',
357 b'profile-benchmark',
357 b'profile-benchmark',
358 default=mercurial.configitems.dynamicdefault,
358 default=mercurial.configitems.dynamicdefault,
359 )
359 )
360 configitem(
360 configitem(
361 b'perf',
361 b'perf',
362 b'run-limits',
362 b'run-limits',
363 default=mercurial.configitems.dynamicdefault,
363 default=mercurial.configitems.dynamicdefault,
364 )
364 )
365
365
366
366
367 def getlen(ui):
367 def getlen(ui):
368 if ui.configbool(b"perf", b"stub", False):
368 if ui.configbool(b"perf", b"stub", False):
369 return lambda x: 1
369 return lambda x: 1
370 return len
370 return len
371
371
372
372
373 class noop:
373 class noop:
374 """dummy context manager"""
374 """dummy context manager"""
375
375
376 def __enter__(self):
376 def __enter__(self):
377 pass
377 pass
378
378
379 def __exit__(self, *args):
379 def __exit__(self, *args):
380 pass
380 pass
381
381
382
382
383 NOOPCTX = noop()
383 NOOPCTX = noop()
384
384
385
385
386 def gettimer(ui, opts=None):
386 def gettimer(ui, opts=None):
387 """return a timer function and formatter: (timer, formatter)
387 """return a timer function and formatter: (timer, formatter)
388
388
389 This function exists to gather the creation of formatter in a single
389 This function exists to gather the creation of formatter in a single
390 place instead of duplicating it in all performance commands."""
390 place instead of duplicating it in all performance commands."""
391
391
392 # enforce an idle period before execution to counteract power management
392 # enforce an idle period before execution to counteract power management
393 # experimental config: perf.presleep
393 # experimental config: perf.presleep
394 time.sleep(getint(ui, b"perf", b"presleep", 1))
394 time.sleep(getint(ui, b"perf", b"presleep", 1))
395
395
396 if opts is None:
396 if opts is None:
397 opts = {}
397 opts = {}
398 # redirect all to stderr unless buffer api is in use
398 # redirect all to stderr unless buffer api is in use
399 if not ui._buffers:
399 if not ui._buffers:
400 ui = ui.copy()
400 ui = ui.copy()
401 uifout = safeattrsetter(ui, b'fout', ignoremissing=True)
401 uifout = safeattrsetter(ui, b'fout', ignoremissing=True)
402 if uifout:
402 if uifout:
403 # for "historical portability":
403 # for "historical portability":
404 # ui.fout/ferr have been available since 1.9 (or 4e1ccd4c2b6d)
404 # ui.fout/ferr have been available since 1.9 (or 4e1ccd4c2b6d)
405 uifout.set(ui.ferr)
405 uifout.set(ui.ferr)
406
406
407 # get a formatter
407 # get a formatter
408 uiformatter = getattr(ui, 'formatter', None)
408 uiformatter = getattr(ui, 'formatter', None)
409 if uiformatter:
409 if uiformatter:
410 fm = uiformatter(b'perf', opts)
410 fm = uiformatter(b'perf', opts)
411 else:
411 else:
412 # for "historical portability":
412 # for "historical portability":
413 # define formatter locally, because ui.formatter has been
413 # define formatter locally, because ui.formatter has been
414 # available since 2.2 (or ae5f92e154d3)
414 # available since 2.2 (or ae5f92e154d3)
415 from mercurial import node
415 from mercurial import node
416
416
417 class defaultformatter:
417 class defaultformatter:
418 """Minimized composition of baseformatter and plainformatter"""
418 """Minimized composition of baseformatter and plainformatter"""
419
419
420 def __init__(self, ui, topic, opts):
420 def __init__(self, ui, topic, opts):
421 self._ui = ui
421 self._ui = ui
422 if ui.debugflag:
422 if ui.debugflag:
423 self.hexfunc = node.hex
423 self.hexfunc = node.hex
424 else:
424 else:
425 self.hexfunc = node.short
425 self.hexfunc = node.short
426
426
427 def __nonzero__(self):
427 def __nonzero__(self):
428 return False
428 return False
429
429
430 __bool__ = __nonzero__
430 __bool__ = __nonzero__
431
431
432 def startitem(self):
432 def startitem(self):
433 pass
433 pass
434
434
435 def data(self, **data):
435 def data(self, **data):
436 pass
436 pass
437
437
438 def write(self, fields, deftext, *fielddata, **opts):
438 def write(self, fields, deftext, *fielddata, **opts):
439 self._ui.write(deftext % fielddata, **opts)
439 self._ui.write(deftext % fielddata, **opts)
440
440
441 def condwrite(self, cond, fields, deftext, *fielddata, **opts):
441 def condwrite(self, cond, fields, deftext, *fielddata, **opts):
442 if cond:
442 if cond:
443 self._ui.write(deftext % fielddata, **opts)
443 self._ui.write(deftext % fielddata, **opts)
444
444
445 def plain(self, text, **opts):
445 def plain(self, text, **opts):
446 self._ui.write(text, **opts)
446 self._ui.write(text, **opts)
447
447
448 def end(self):
448 def end(self):
449 pass
449 pass
450
450
451 fm = defaultformatter(ui, b'perf', opts)
451 fm = defaultformatter(ui, b'perf', opts)
452
452
453 # stub function, runs code only once instead of in a loop
453 # stub function, runs code only once instead of in a loop
454 # experimental config: perf.stub
454 # experimental config: perf.stub
455 if ui.configbool(b"perf", b"stub", False):
455 if ui.configbool(b"perf", b"stub", False):
456 return functools.partial(stub_timer, fm), fm
456 return functools.partial(stub_timer, fm), fm
457
457
458 # experimental config: perf.all-timing
458 # experimental config: perf.all-timing
459 displayall = ui.configbool(b"perf", b"all-timing", True)
459 displayall = ui.configbool(b"perf", b"all-timing", True)
460
460
461 # experimental config: perf.run-limits
461 # experimental config: perf.run-limits
462 limitspec = ui.configlist(b"perf", b"run-limits", [])
462 limitspec = ui.configlist(b"perf", b"run-limits", [])
463 limits = []
463 limits = []
464 for item in limitspec:
464 for item in limitspec:
465 parts = item.split(b'-', 1)
465 parts = item.split(b'-', 1)
466 if len(parts) < 2:
466 if len(parts) < 2:
467 ui.warn((b'malformatted run limit entry, missing "-": %s\n' % item))
467 ui.warn((b'malformatted run limit entry, missing "-": %s\n' % item))
468 continue
468 continue
469 try:
469 try:
470 time_limit = float(_sysstr(parts[0]))
470 time_limit = float(_sysstr(parts[0]))
471 except ValueError as e:
471 except ValueError as e:
472 ui.warn(
472 ui.warn(
473 (
473 (
474 b'malformatted run limit entry, %s: %s\n'
474 b'malformatted run limit entry, %s: %s\n'
475 % (_bytestr(e), item)
475 % (_bytestr(e), item)
476 )
476 )
477 )
477 )
478 continue
478 continue
479 try:
479 try:
480 run_limit = int(_sysstr(parts[1]))
480 run_limit = int(_sysstr(parts[1]))
481 except ValueError as e:
481 except ValueError as e:
482 ui.warn(
482 ui.warn(
483 (
483 (
484 b'malformatted run limit entry, %s: %s\n'
484 b'malformatted run limit entry, %s: %s\n'
485 % (_bytestr(e), item)
485 % (_bytestr(e), item)
486 )
486 )
487 )
487 )
488 continue
488 continue
489 limits.append((time_limit, run_limit))
489 limits.append((time_limit, run_limit))
490 if not limits:
490 if not limits:
491 limits = DEFAULTLIMITS
491 limits = DEFAULTLIMITS
492
492
493 profiler = None
493 profiler = None
494 if profiling is not None:
494 if profiling is not None:
495 if ui.configbool(b"perf", b"profile-benchmark", False):
495 if ui.configbool(b"perf", b"profile-benchmark", False):
496 profiler = profiling.profile(ui)
496 profiler = profiling.profile(ui)
497
497
498 prerun = getint(ui, b"perf", b"pre-run", 0)
498 prerun = getint(ui, b"perf", b"pre-run", 0)
499 t = functools.partial(
499 t = functools.partial(
500 _timer,
500 _timer,
501 fm,
501 fm,
502 displayall=displayall,
502 displayall=displayall,
503 limits=limits,
503 limits=limits,
504 prerun=prerun,
504 prerun=prerun,
505 profiler=profiler,
505 profiler=profiler,
506 )
506 )
507 return t, fm
507 return t, fm
508
508
509
509
510 def stub_timer(fm, func, setup=None, title=None):
510 def stub_timer(fm, func, setup=None, title=None):
511 if setup is not None:
511 if setup is not None:
512 setup()
512 setup()
513 func()
513 func()
514
514
515
515
516 @contextlib.contextmanager
516 @contextlib.contextmanager
517 def timeone():
517 def timeone():
518 r = []
518 r = []
519 ostart = os.times()
519 ostart = os.times()
520 cstart = util.timer()
520 cstart = util.timer()
521 yield r
521 yield r
522 cstop = util.timer()
522 cstop = util.timer()
523 ostop = os.times()
523 ostop = os.times()
524 a, b = ostart, ostop
524 a, b = ostart, ostop
525 r.append((cstop - cstart, b[0] - a[0], b[1] - a[1]))
525 r.append((cstop - cstart, b[0] - a[0], b[1] - a[1]))
526
526
527
527
528 # list of stop condition (elapsed time, minimal run count)
528 # list of stop condition (elapsed time, minimal run count)
529 DEFAULTLIMITS = (
529 DEFAULTLIMITS = (
530 (3.0, 100),
530 (3.0, 100),
531 (10.0, 3),
531 (10.0, 3),
532 )
532 )
533
533
534
534
535 @contextlib.contextmanager
535 @contextlib.contextmanager
536 def noop_context():
536 def noop_context():
537 yield
537 yield
538
538
539
539
540 def _timer(
540 def _timer(
541 fm,
541 fm,
542 func,
542 func,
543 setup=None,
543 setup=None,
544 context=noop_context,
544 context=noop_context,
545 title=None,
545 title=None,
546 displayall=False,
546 displayall=False,
547 limits=DEFAULTLIMITS,
547 limits=DEFAULTLIMITS,
548 prerun=0,
548 prerun=0,
549 profiler=None,
549 profiler=None,
550 ):
550 ):
551 gc.collect()
551 gc.collect()
552 results = []
552 results = []
553 begin = util.timer()
553 begin = util.timer()
554 count = 0
554 count = 0
555 if profiler is None:
555 if profiler is None:
556 profiler = NOOPCTX
556 profiler = NOOPCTX
557 for i in range(prerun):
557 for i in range(prerun):
558 if setup is not None:
558 if setup is not None:
559 setup()
559 setup()
560 with context():
560 with context():
561 func()
561 func()
562 keepgoing = True
562 keepgoing = True
563 while keepgoing:
563 while keepgoing:
564 if setup is not None:
564 if setup is not None:
565 setup()
565 setup()
566 with context():
566 with context():
567 with profiler:
567 with profiler:
568 with timeone() as item:
568 with timeone() as item:
569 r = func()
569 r = func()
570 profiler = NOOPCTX
570 profiler = NOOPCTX
571 count += 1
571 count += 1
572 results.append(item[0])
572 results.append(item[0])
573 cstop = util.timer()
573 cstop = util.timer()
574 # Look for a stop condition.
574 # Look for a stop condition.
575 elapsed = cstop - begin
575 elapsed = cstop - begin
576 for t, mincount in limits:
576 for t, mincount in limits:
577 if elapsed >= t and count >= mincount:
577 if elapsed >= t and count >= mincount:
578 keepgoing = False
578 keepgoing = False
579 break
579 break
580
580
581 formatone(fm, results, title=title, result=r, displayall=displayall)
581 formatone(fm, results, title=title, result=r, displayall=displayall)
582
582
583
583
584 def formatone(fm, timings, title=None, result=None, displayall=False):
584 def formatone(fm, timings, title=None, result=None, displayall=False):
585 count = len(timings)
585 count = len(timings)
586
586
587 fm.startitem()
587 fm.startitem()
588
588
589 if title:
589 if title:
590 fm.write(b'title', b'! %s\n', title)
590 fm.write(b'title', b'! %s\n', title)
591 if result:
591 if result:
592 fm.write(b'result', b'! result: %s\n', result)
592 fm.write(b'result', b'! result: %s\n', result)
593
593
594 def display(role, entry):
594 def display(role, entry):
595 prefix = b''
595 prefix = b''
596 if role != b'best':
596 if role != b'best':
597 prefix = b'%s.' % role
597 prefix = b'%s.' % role
598 fm.plain(b'!')
598 fm.plain(b'!')
599 fm.write(prefix + b'wall', b' wall %f', entry[0])
599 fm.write(prefix + b'wall', b' wall %f', entry[0])
600 fm.write(prefix + b'comb', b' comb %f', entry[1] + entry[2])
600 fm.write(prefix + b'comb', b' comb %f', entry[1] + entry[2])
601 fm.write(prefix + b'user', b' user %f', entry[1])
601 fm.write(prefix + b'user', b' user %f', entry[1])
602 fm.write(prefix + b'sys', b' sys %f', entry[2])
602 fm.write(prefix + b'sys', b' sys %f', entry[2])
603 fm.write(prefix + b'count', b' (%s of %%d)' % role, count)
603 fm.write(prefix + b'count', b' (%s of %%d)' % role, count)
604 fm.plain(b'\n')
604 fm.plain(b'\n')
605
605
606 timings.sort()
606 timings.sort()
607 min_val = timings[0]
607 min_val = timings[0]
608 display(b'best', min_val)
608 display(b'best', min_val)
609 if displayall:
609 if displayall:
610 max_val = timings[-1]
610 max_val = timings[-1]
611 display(b'max', max_val)
611 display(b'max', max_val)
612 avg = tuple([sum(x) / count for x in zip(*timings)])
612 avg = tuple([sum(x) / count for x in zip(*timings)])
613 display(b'avg', avg)
613 display(b'avg', avg)
614 median = timings[len(timings) // 2]
614 median = timings[len(timings) // 2]
615 display(b'median', median)
615 display(b'median', median)
616
616
617
617
618 # utilities for historical portability
618 # utilities for historical portability
619
619
620
620
621 def getint(ui, section, name, default):
621 def getint(ui, section, name, default):
622 # for "historical portability":
622 # for "historical portability":
623 # ui.configint has been available since 1.9 (or fa2b596db182)
623 # ui.configint has been available since 1.9 (or fa2b596db182)
624 v = ui.config(section, name, None)
624 v = ui.config(section, name, None)
625 if v is None:
625 if v is None:
626 return default
626 return default
627 try:
627 try:
628 return int(v)
628 return int(v)
629 except ValueError:
629 except ValueError:
630 raise error.ConfigError(
630 raise error.ConfigError(
631 b"%s.%s is not an integer ('%s')" % (section, name, v)
631 b"%s.%s is not an integer ('%s')" % (section, name, v)
632 )
632 )
633
633
634
634
635 def safeattrsetter(obj, name, ignoremissing=False):
635 def safeattrsetter(obj, name, ignoremissing=False):
636 """Ensure that 'obj' has 'name' attribute before subsequent setattr
636 """Ensure that 'obj' has 'name' attribute before subsequent setattr
637
637
638 This function is aborted, if 'obj' doesn't have 'name' attribute
638 This function is aborted, if 'obj' doesn't have 'name' attribute
639 at runtime. This avoids overlooking removal of an attribute, which
639 at runtime. This avoids overlooking removal of an attribute, which
640 breaks assumption of performance measurement, in the future.
640 breaks assumption of performance measurement, in the future.
641
641
642 This function returns the object to (1) assign a new value, and
642 This function returns the object to (1) assign a new value, and
643 (2) restore an original value to the attribute.
643 (2) restore an original value to the attribute.
644
644
645 If 'ignoremissing' is true, missing 'name' attribute doesn't cause
645 If 'ignoremissing' is true, missing 'name' attribute doesn't cause
646 abortion, and this function returns None. This is useful to
646 abortion, and this function returns None. This is useful to
647 examine an attribute, which isn't ensured in all Mercurial
647 examine an attribute, which isn't ensured in all Mercurial
648 versions.
648 versions.
649 """
649 """
650 if not util.safehasattr(obj, name):
650 if not util.safehasattr(obj, name):
651 if ignoremissing:
651 if ignoremissing:
652 return None
652 return None
653 raise error.Abort(
653 raise error.Abort(
654 (
654 (
655 b"missing attribute %s of %s might break assumption"
655 b"missing attribute %s of %s might break assumption"
656 b" of performance measurement"
656 b" of performance measurement"
657 )
657 )
658 % (name, obj)
658 % (name, obj)
659 )
659 )
660
660
661 origvalue = getattr(obj, _sysstr(name))
661 origvalue = getattr(obj, _sysstr(name))
662
662
663 class attrutil:
663 class attrutil:
664 def set(self, newvalue):
664 def set(self, newvalue):
665 setattr(obj, _sysstr(name), newvalue)
665 setattr(obj, _sysstr(name), newvalue)
666
666
667 def restore(self):
667 def restore(self):
668 setattr(obj, _sysstr(name), origvalue)
668 setattr(obj, _sysstr(name), origvalue)
669
669
670 return attrutil()
670 return attrutil()
671
671
672
672
673 # utilities to examine each internal API changes
673 # utilities to examine each internal API changes
674
674
675
675
676 def getbranchmapsubsettable():
676 def getbranchmapsubsettable():
677 # for "historical portability":
677 # for "historical portability":
678 # subsettable is defined in:
678 # subsettable is defined in:
679 # - branchmap since 2.9 (or 175c6fd8cacc)
679 # - branchmap since 2.9 (or 175c6fd8cacc)
680 # - repoview since 2.5 (or 59a9f18d4587)
680 # - repoview since 2.5 (or 59a9f18d4587)
681 # - repoviewutil since 5.0
681 # - repoviewutil since 5.0
682 for mod in (branchmap, repoview, repoviewutil):
682 for mod in (branchmap, repoview, repoviewutil):
683 subsettable = getattr(mod, 'subsettable', None)
683 subsettable = getattr(mod, 'subsettable', None)
684 if subsettable:
684 if subsettable:
685 return subsettable
685 return subsettable
686
686
687 # bisecting in bcee63733aad::59a9f18d4587 can reach here (both
687 # bisecting in bcee63733aad::59a9f18d4587 can reach here (both
688 # branchmap and repoview modules exist, but subsettable attribute
688 # branchmap and repoview modules exist, but subsettable attribute
689 # doesn't)
689 # doesn't)
690 raise error.Abort(
690 raise error.Abort(
691 b"perfbranchmap not available with this Mercurial",
691 b"perfbranchmap not available with this Mercurial",
692 hint=b"use 2.5 or later",
692 hint=b"use 2.5 or later",
693 )
693 )
694
694
695
695
696 def getsvfs(repo):
696 def getsvfs(repo):
697 """Return appropriate object to access files under .hg/store"""
697 """Return appropriate object to access files under .hg/store"""
698 # for "historical portability":
698 # for "historical portability":
699 # repo.svfs has been available since 2.3 (or 7034365089bf)
699 # repo.svfs has been available since 2.3 (or 7034365089bf)
700 svfs = getattr(repo, 'svfs', None)
700 svfs = getattr(repo, 'svfs', None)
701 if svfs:
701 if svfs:
702 return svfs
702 return svfs
703 else:
703 else:
704 return getattr(repo, 'sopener')
704 return getattr(repo, 'sopener')
705
705
706
706
707 def getvfs(repo):
707 def getvfs(repo):
708 """Return appropriate object to access files under .hg"""
708 """Return appropriate object to access files under .hg"""
709 # for "historical portability":
709 # for "historical portability":
710 # repo.vfs has been available since 2.3 (or 7034365089bf)
710 # repo.vfs has been available since 2.3 (or 7034365089bf)
711 vfs = getattr(repo, 'vfs', None)
711 vfs = getattr(repo, 'vfs', None)
712 if vfs:
712 if vfs:
713 return vfs
713 return vfs
714 else:
714 else:
715 return getattr(repo, 'opener')
715 return getattr(repo, 'opener')
716
716
717
717
718 def repocleartagscachefunc(repo):
718 def repocleartagscachefunc(repo):
719 """Return the function to clear tags cache according to repo internal API"""
719 """Return the function to clear tags cache according to repo internal API"""
720 if util.safehasattr(repo, b'_tagscache'): # since 2.0 (or 9dca7653b525)
720 if util.safehasattr(repo, b'_tagscache'): # since 2.0 (or 9dca7653b525)
721 # in this case, setattr(repo, '_tagscache', None) or so isn't
721 # in this case, setattr(repo, '_tagscache', None) or so isn't
722 # correct way to clear tags cache, because existing code paths
722 # correct way to clear tags cache, because existing code paths
723 # expect _tagscache to be a structured object.
723 # expect _tagscache to be a structured object.
724 def clearcache():
724 def clearcache():
725 # _tagscache has been filteredpropertycache since 2.5 (or
725 # _tagscache has been filteredpropertycache since 2.5 (or
726 # 98c867ac1330), and delattr() can't work in such case
726 # 98c867ac1330), and delattr() can't work in such case
727 if '_tagscache' in vars(repo):
727 if '_tagscache' in vars(repo):
728 del repo.__dict__['_tagscache']
728 del repo.__dict__['_tagscache']
729
729
730 return clearcache
730 return clearcache
731
731
732 repotags = safeattrsetter(repo, b'_tags', ignoremissing=True)
732 repotags = safeattrsetter(repo, b'_tags', ignoremissing=True)
733 if repotags: # since 1.4 (or 5614a628d173)
733 if repotags: # since 1.4 (or 5614a628d173)
734 return lambda: repotags.set(None)
734 return lambda: repotags.set(None)
735
735
736 repotagscache = safeattrsetter(repo, b'tagscache', ignoremissing=True)
736 repotagscache = safeattrsetter(repo, b'tagscache', ignoremissing=True)
737 if repotagscache: # since 0.6 (or d7df759d0e97)
737 if repotagscache: # since 0.6 (or d7df759d0e97)
738 return lambda: repotagscache.set(None)
738 return lambda: repotagscache.set(None)
739
739
740 # Mercurial earlier than 0.6 (or d7df759d0e97) logically reaches
740 # Mercurial earlier than 0.6 (or d7df759d0e97) logically reaches
741 # this point, but it isn't so problematic, because:
741 # this point, but it isn't so problematic, because:
742 # - repo.tags of such Mercurial isn't "callable", and repo.tags()
742 # - repo.tags of such Mercurial isn't "callable", and repo.tags()
743 # in perftags() causes failure soon
743 # in perftags() causes failure soon
744 # - perf.py itself has been available since 1.1 (or eb240755386d)
744 # - perf.py itself has been available since 1.1 (or eb240755386d)
745 raise error.Abort(b"tags API of this hg command is unknown")
745 raise error.Abort(b"tags API of this hg command is unknown")
746
746
747
747
748 # utilities to clear cache
748 # utilities to clear cache
749
749
750
750
751 def clearfilecache(obj, attrname):
751 def clearfilecache(obj, attrname):
752 unfiltered = getattr(obj, 'unfiltered', None)
752 unfiltered = getattr(obj, 'unfiltered', None)
753 if unfiltered is not None:
753 if unfiltered is not None:
754 obj = obj.unfiltered()
754 obj = obj.unfiltered()
755 if attrname in vars(obj):
755 if attrname in vars(obj):
756 delattr(obj, attrname)
756 delattr(obj, attrname)
757 obj._filecache.pop(attrname, None)
757 obj._filecache.pop(attrname, None)
758
758
759
759
760 def clearchangelog(repo):
760 def clearchangelog(repo):
761 if repo is not repo.unfiltered():
761 if repo is not repo.unfiltered():
762 object.__setattr__(repo, '_clcachekey', None)
762 object.__setattr__(repo, '_clcachekey', None)
763 object.__setattr__(repo, '_clcache', None)
763 object.__setattr__(repo, '_clcache', None)
764 clearfilecache(repo.unfiltered(), 'changelog')
764 clearfilecache(repo.unfiltered(), 'changelog')
765
765
766
766
767 # perf commands
767 # perf commands
768
768
769
769
770 @command(b'perf::walk|perfwalk', formatteropts)
770 @command(b'perf::walk|perfwalk', formatteropts)
771 def perfwalk(ui, repo, *pats, **opts):
771 def perfwalk(ui, repo, *pats, **opts):
772 opts = _byteskwargs(opts)
772 opts = _byteskwargs(opts)
773 timer, fm = gettimer(ui, opts)
773 timer, fm = gettimer(ui, opts)
774 m = scmutil.match(repo[None], pats, {})
774 m = scmutil.match(repo[None], pats, {})
775 timer(
775 timer(
776 lambda: len(
776 lambda: len(
777 list(
777 list(
778 repo.dirstate.walk(m, subrepos=[], unknown=True, ignored=False)
778 repo.dirstate.walk(m, subrepos=[], unknown=True, ignored=False)
779 )
779 )
780 )
780 )
781 )
781 )
782 fm.end()
782 fm.end()
783
783
784
784
785 @command(b'perf::annotate|perfannotate', formatteropts)
785 @command(b'perf::annotate|perfannotate', formatteropts)
786 def perfannotate(ui, repo, f, **opts):
786 def perfannotate(ui, repo, f, **opts):
787 opts = _byteskwargs(opts)
787 opts = _byteskwargs(opts)
788 timer, fm = gettimer(ui, opts)
788 timer, fm = gettimer(ui, opts)
789 fc = repo[b'.'][f]
789 fc = repo[b'.'][f]
790 timer(lambda: len(fc.annotate(True)))
790 timer(lambda: len(fc.annotate(True)))
791 fm.end()
791 fm.end()
792
792
793
793
794 @command(
794 @command(
795 b'perf::status|perfstatus',
795 b'perf::status|perfstatus',
796 [
796 [
797 (b'u', b'unknown', False, b'ask status to look for unknown files'),
797 (b'u', b'unknown', False, b'ask status to look for unknown files'),
798 (b'', b'dirstate', False, b'benchmark the internal dirstate call'),
798 (b'', b'dirstate', False, b'benchmark the internal dirstate call'),
799 ]
799 ]
800 + formatteropts,
800 + formatteropts,
801 )
801 )
802 def perfstatus(ui, repo, **opts):
802 def perfstatus(ui, repo, **opts):
803 """benchmark the performance of a single status call
803 """benchmark the performance of a single status call
804
804
805 The repository data are preserved between each call.
805 The repository data are preserved between each call.
806
806
807 By default, only the status of the tracked file are requested. If
807 By default, only the status of the tracked file are requested. If
808 `--unknown` is passed, the "unknown" files are also tracked.
808 `--unknown` is passed, the "unknown" files are also tracked.
809 """
809 """
810 opts = _byteskwargs(opts)
810 opts = _byteskwargs(opts)
811 # m = match.always(repo.root, repo.getcwd())
811 # m = match.always(repo.root, repo.getcwd())
812 # timer(lambda: sum(map(len, repo.dirstate.status(m, [], False, False,
812 # timer(lambda: sum(map(len, repo.dirstate.status(m, [], False, False,
813 # False))))
813 # False))))
814 timer, fm = gettimer(ui, opts)
814 timer, fm = gettimer(ui, opts)
815 if opts[b'dirstate']:
815 if opts[b'dirstate']:
816 dirstate = repo.dirstate
816 dirstate = repo.dirstate
817 m = scmutil.matchall(repo)
817 m = scmutil.matchall(repo)
818 unknown = opts[b'unknown']
818 unknown = opts[b'unknown']
819
819
820 def status_dirstate():
820 def status_dirstate():
821 s = dirstate.status(
821 s = dirstate.status(
822 m, subrepos=[], ignored=False, clean=False, unknown=unknown
822 m, subrepos=[], ignored=False, clean=False, unknown=unknown
823 )
823 )
824 sum(map(bool, s))
824 sum(map(bool, s))
825
825
826 if util.safehasattr(dirstate, 'running_status'):
826 if util.safehasattr(dirstate, 'running_status'):
827 with dirstate.running_status(repo):
827 with dirstate.running_status(repo):
828 timer(status_dirstate)
828 timer(status_dirstate)
829 dirstate.invalidate()
829 dirstate.invalidate()
830 else:
830 else:
831 timer(status_dirstate)
831 timer(status_dirstate)
832 else:
832 else:
833 timer(lambda: sum(map(len, repo.status(unknown=opts[b'unknown']))))
833 timer(lambda: sum(map(len, repo.status(unknown=opts[b'unknown']))))
834 fm.end()
834 fm.end()
835
835
836
836
837 @command(b'perf::addremove|perfaddremove', formatteropts)
837 @command(b'perf::addremove|perfaddremove', formatteropts)
838 def perfaddremove(ui, repo, **opts):
838 def perfaddremove(ui, repo, **opts):
839 opts = _byteskwargs(opts)
839 opts = _byteskwargs(opts)
840 timer, fm = gettimer(ui, opts)
840 timer, fm = gettimer(ui, opts)
841 try:
841 try:
842 oldquiet = repo.ui.quiet
842 oldquiet = repo.ui.quiet
843 repo.ui.quiet = True
843 repo.ui.quiet = True
844 matcher = scmutil.match(repo[None])
844 matcher = scmutil.match(repo[None])
845 opts[b'dry_run'] = True
845 opts[b'dry_run'] = True
846 if 'uipathfn' in getargspec(scmutil.addremove).args:
846 if 'uipathfn' in getargspec(scmutil.addremove).args:
847 uipathfn = scmutil.getuipathfn(repo)
847 uipathfn = scmutil.getuipathfn(repo)
848 timer(lambda: scmutil.addremove(repo, matcher, b"", uipathfn, opts))
848 timer(lambda: scmutil.addremove(repo, matcher, b"", uipathfn, opts))
849 else:
849 else:
850 timer(lambda: scmutil.addremove(repo, matcher, b"", opts))
850 timer(lambda: scmutil.addremove(repo, matcher, b"", opts))
851 finally:
851 finally:
852 repo.ui.quiet = oldquiet
852 repo.ui.quiet = oldquiet
853 fm.end()
853 fm.end()
854
854
855
855
856 def clearcaches(cl):
856 def clearcaches(cl):
857 # behave somewhat consistently across internal API changes
857 # behave somewhat consistently across internal API changes
858 if util.safehasattr(cl, b'clearcaches'):
858 if util.safehasattr(cl, b'clearcaches'):
859 cl.clearcaches()
859 cl.clearcaches()
860 elif util.safehasattr(cl, b'_nodecache'):
860 elif util.safehasattr(cl, b'_nodecache'):
861 # <= hg-5.2
861 # <= hg-5.2
862 from mercurial.node import nullid, nullrev
862 from mercurial.node import nullid, nullrev
863
863
864 cl._nodecache = {nullid: nullrev}
864 cl._nodecache = {nullid: nullrev}
865 cl._nodepos = None
865 cl._nodepos = None
866
866
867
867
868 @command(b'perf::heads|perfheads', formatteropts)
868 @command(b'perf::heads|perfheads', formatteropts)
869 def perfheads(ui, repo, **opts):
869 def perfheads(ui, repo, **opts):
870 """benchmark the computation of a changelog heads"""
870 """benchmark the computation of a changelog heads"""
871 opts = _byteskwargs(opts)
871 opts = _byteskwargs(opts)
872 timer, fm = gettimer(ui, opts)
872 timer, fm = gettimer(ui, opts)
873 cl = repo.changelog
873 cl = repo.changelog
874
874
875 def s():
875 def s():
876 clearcaches(cl)
876 clearcaches(cl)
877
877
878 def d():
878 def d():
879 len(cl.headrevs())
879 len(cl.headrevs())
880
880
881 timer(d, setup=s)
881 timer(d, setup=s)
882 fm.end()
882 fm.end()
883
883
884
884
885 def _default_clear_on_disk_tags_cache(repo):
885 def _default_clear_on_disk_tags_cache(repo):
886 from mercurial import tags
886 from mercurial import tags
887
887
888 repo.cachevfs.tryunlink(tags._filename(repo))
888 repo.cachevfs.tryunlink(tags._filename(repo))
889
889
890
890
891 def _default_clear_on_disk_tags_fnodes_cache(repo):
891 def _default_clear_on_disk_tags_fnodes_cache(repo):
892 from mercurial import tags
892 from mercurial import tags
893
893
894 repo.cachevfs.tryunlink(tags._fnodescachefile)
894 repo.cachevfs.tryunlink(tags._fnodescachefile)
895
895
896
896
897 def _default_forget_fnodes(repo, revs):
897 def _default_forget_fnodes(repo, revs):
898 """function used by the perf extension to prune some entries from the
898 """function used by the perf extension to prune some entries from the
899 fnodes cache"""
899 fnodes cache"""
900 from mercurial import tags
900 from mercurial import tags
901
901
902 missing_1 = b'\xff' * 4
902 missing_1 = b'\xff' * 4
903 missing_2 = b'\xff' * 20
903 missing_2 = b'\xff' * 20
904 cache = tags.hgtagsfnodescache(repo.unfiltered())
904 cache = tags.hgtagsfnodescache(repo.unfiltered())
905 for r in revs:
905 for r in revs:
906 cache._writeentry(r * tags._fnodesrecsize, missing_1, missing_2)
906 cache._writeentry(r * tags._fnodesrecsize, missing_1, missing_2)
907 cache.write()
907 cache.write()
908
908
909
909
910 @command(
910 @command(
911 b'perf::tags|perftags',
911 b'perf::tags|perftags',
912 formatteropts
912 formatteropts
913 + [
913 + [
914 (b'', b'clear-revlogs', False, b'refresh changelog and manifest'),
914 (b'', b'clear-revlogs', False, b'refresh changelog and manifest'),
915 (
915 (
916 b'',
916 b'',
917 b'clear-on-disk-cache',
917 b'clear-on-disk-cache',
918 False,
918 False,
919 b'clear on disk tags cache (DESTRUCTIVE)',
919 b'clear on disk tags cache (DESTRUCTIVE)',
920 ),
920 ),
921 (
921 (
922 b'',
922 b'',
923 b'clear-fnode-cache-all',
923 b'clear-fnode-cache-all',
924 False,
924 False,
925 b'clear on disk file node cache (DESTRUCTIVE),',
925 b'clear on disk file node cache (DESTRUCTIVE),',
926 ),
926 ),
927 (
927 (
928 b'',
928 b'',
929 b'clear-fnode-cache-rev',
929 b'clear-fnode-cache-rev',
930 [],
930 [],
931 b'clear on disk file node cache (DESTRUCTIVE),',
931 b'clear on disk file node cache (DESTRUCTIVE),',
932 b'REVS',
932 b'REVS',
933 ),
933 ),
934 (
934 (
935 b'',
935 b'',
936 b'update-last',
936 b'update-last',
937 b'',
937 b'',
938 b'simulate an update over the last N revisions (DESTRUCTIVE),',
938 b'simulate an update over the last N revisions (DESTRUCTIVE),',
939 b'N',
939 b'N',
940 ),
940 ),
941 ],
941 ],
942 )
942 )
943 def perftags(ui, repo, **opts):
943 def perftags(ui, repo, **opts):
944 """Benchmark tags retrieval in various situation
944 """Benchmark tags retrieval in various situation
945
945
946 The option marked as (DESTRUCTIVE) will alter the on-disk cache, possibly
946 The option marked as (DESTRUCTIVE) will alter the on-disk cache, possibly
947 altering performance after the command was run. However, it does not
947 altering performance after the command was run. However, it does not
948 destroy any stored data.
948 destroy any stored data.
949 """
949 """
950 from mercurial import tags
950 from mercurial import tags
951
951
952 opts = _byteskwargs(opts)
952 opts = _byteskwargs(opts)
953 timer, fm = gettimer(ui, opts)
953 timer, fm = gettimer(ui, opts)
954 repocleartagscache = repocleartagscachefunc(repo)
954 repocleartagscache = repocleartagscachefunc(repo)
955 clearrevlogs = opts[b'clear_revlogs']
955 clearrevlogs = opts[b'clear_revlogs']
956 clear_disk = opts[b'clear_on_disk_cache']
956 clear_disk = opts[b'clear_on_disk_cache']
957 clear_fnode = opts[b'clear_fnode_cache_all']
957 clear_fnode = opts[b'clear_fnode_cache_all']
958
958
959 clear_fnode_revs = opts[b'clear_fnode_cache_rev']
959 clear_fnode_revs = opts[b'clear_fnode_cache_rev']
960 update_last_str = opts[b'update_last']
960 update_last_str = opts[b'update_last']
961 update_last = None
961 update_last = None
962 if update_last_str:
962 if update_last_str:
963 try:
963 try:
964 update_last = int(update_last_str)
964 update_last = int(update_last_str)
965 except ValueError:
965 except ValueError:
966 msg = b'could not parse value for update-last: "%s"'
966 msg = b'could not parse value for update-last: "%s"'
967 msg %= update_last_str
967 msg %= update_last_str
968 hint = b'value should be an integer'
968 hint = b'value should be an integer'
969 raise error.Abort(msg, hint=hint)
969 raise error.Abort(msg, hint=hint)
970
970
971 clear_disk_fn = getattr(
971 clear_disk_fn = getattr(
972 tags,
972 tags,
973 "clear_cache_on_disk",
973 "clear_cache_on_disk",
974 _default_clear_on_disk_tags_cache,
974 _default_clear_on_disk_tags_cache,
975 )
975 )
976 clear_fnodes_fn = getattr(
976 clear_fnodes_fn = getattr(
977 tags,
977 tags,
978 "clear_cache_fnodes",
978 "clear_cache_fnodes",
979 _default_clear_on_disk_tags_fnodes_cache,
979 _default_clear_on_disk_tags_fnodes_cache,
980 )
980 )
981 clear_fnodes_rev_fn = getattr(
981 clear_fnodes_rev_fn = getattr(
982 tags,
982 tags,
983 "forget_fnodes",
983 "forget_fnodes",
984 _default_forget_fnodes,
984 _default_forget_fnodes,
985 )
985 )
986
986
987 clear_revs = []
987 clear_revs = []
988 if clear_fnode_revs:
988 if clear_fnode_revs:
989 clear_revs.extends(scmutil.revrange(repo, clear_fnode_revs))
989 clear_revs.extends(scmutil.revrange(repo, clear_fnode_revs))
990
990
991 if update_last:
991 if update_last:
992 revset = b'last(all(), %d)' % update_last
992 revset = b'last(all(), %d)' % update_last
993 last_revs = repo.unfiltered().revs(revset)
993 last_revs = repo.unfiltered().revs(revset)
994 clear_revs.extend(last_revs)
994 clear_revs.extend(last_revs)
995
995
996 from mercurial import repoview
996 from mercurial import repoview
997
997
998 rev_filter = {(b'experimental', b'extra-filter-revs'): revset}
998 rev_filter = {(b'experimental', b'extra-filter-revs'): revset}
999 with repo.ui.configoverride(rev_filter, source=b"perf"):
999 with repo.ui.configoverride(rev_filter, source=b"perf"):
1000 filter_id = repoview.extrafilter(repo.ui)
1000 filter_id = repoview.extrafilter(repo.ui)
1001
1001
1002 filter_name = b'%s%%%s' % (repo.filtername, filter_id)
1002 filter_name = b'%s%%%s' % (repo.filtername, filter_id)
1003 pre_repo = repo.filtered(filter_name)
1003 pre_repo = repo.filtered(filter_name)
1004 pre_repo.tags() # warm the cache
1004 pre_repo.tags() # warm the cache
1005 old_tags_path = repo.cachevfs.join(tags._filename(pre_repo))
1005 old_tags_path = repo.cachevfs.join(tags._filename(pre_repo))
1006 new_tags_path = repo.cachevfs.join(tags._filename(repo))
1006 new_tags_path = repo.cachevfs.join(tags._filename(repo))
1007
1007
1008 clear_revs = sorted(set(clear_revs))
1008 clear_revs = sorted(set(clear_revs))
1009
1009
1010 def s():
1010 def s():
1011 if update_last:
1011 if update_last:
1012 util.copyfile(old_tags_path, new_tags_path)
1012 util.copyfile(old_tags_path, new_tags_path)
1013 if clearrevlogs:
1013 if clearrevlogs:
1014 clearchangelog(repo)
1014 clearchangelog(repo)
1015 clearfilecache(repo.unfiltered(), 'manifest')
1015 clearfilecache(repo.unfiltered(), 'manifest')
1016 if clear_disk:
1016 if clear_disk:
1017 clear_disk_fn(repo)
1017 clear_disk_fn(repo)
1018 if clear_fnode:
1018 if clear_fnode:
1019 clear_fnodes_fn(repo)
1019 clear_fnodes_fn(repo)
1020 elif clear_revs:
1020 elif clear_revs:
1021 clear_fnodes_rev_fn(repo, clear_revs)
1021 clear_fnodes_rev_fn(repo, clear_revs)
1022 repocleartagscache()
1022 repocleartagscache()
1023
1023
1024 def t():
1024 def t():
1025 len(repo.tags())
1025 len(repo.tags())
1026
1026
1027 timer(t, setup=s)
1027 timer(t, setup=s)
1028 fm.end()
1028 fm.end()
1029
1029
1030
1030
1031 @command(b'perf::ancestors|perfancestors', formatteropts)
1031 @command(b'perf::ancestors|perfancestors', formatteropts)
1032 def perfancestors(ui, repo, **opts):
1032 def perfancestors(ui, repo, **opts):
1033 opts = _byteskwargs(opts)
1033 opts = _byteskwargs(opts)
1034 timer, fm = gettimer(ui, opts)
1034 timer, fm = gettimer(ui, opts)
1035 heads = repo.changelog.headrevs()
1035 heads = repo.changelog.headrevs()
1036
1036
1037 def d():
1037 def d():
1038 for a in repo.changelog.ancestors(heads):
1038 for a in repo.changelog.ancestors(heads):
1039 pass
1039 pass
1040
1040
1041 timer(d)
1041 timer(d)
1042 fm.end()
1042 fm.end()
1043
1043
1044
1044
1045 @command(b'perf::ancestorset|perfancestorset', formatteropts)
1045 @command(b'perf::ancestorset|perfancestorset', formatteropts)
1046 def perfancestorset(ui, repo, revset, **opts):
1046 def perfancestorset(ui, repo, revset, **opts):
1047 opts = _byteskwargs(opts)
1047 opts = _byteskwargs(opts)
1048 timer, fm = gettimer(ui, opts)
1048 timer, fm = gettimer(ui, opts)
1049 revs = repo.revs(revset)
1049 revs = repo.revs(revset)
1050 heads = repo.changelog.headrevs()
1050 heads = repo.changelog.headrevs()
1051
1051
1052 def d():
1052 def d():
1053 s = repo.changelog.ancestors(heads)
1053 s = repo.changelog.ancestors(heads)
1054 for rev in revs:
1054 for rev in revs:
1055 rev in s
1055 rev in s
1056
1056
1057 timer(d)
1057 timer(d)
1058 fm.end()
1058 fm.end()
1059
1059
1060
1060
1061 @command(
1061 @command(
1062 b'perf::delta-find',
1062 b'perf::delta-find',
1063 revlogopts + formatteropts,
1063 revlogopts + formatteropts,
1064 b'-c|-m|FILE REV',
1064 b'-c|-m|FILE REV',
1065 )
1065 )
1066 def perf_delta_find(ui, repo, arg_1, arg_2=None, **opts):
1066 def perf_delta_find(ui, repo, arg_1, arg_2=None, **opts):
1067 """benchmark the process of finding a valid delta for a revlog revision
1067 """benchmark the process of finding a valid delta for a revlog revision
1068
1068
1069 When a revlog receives a new revision (e.g. from a commit, or from an
1069 When a revlog receives a new revision (e.g. from a commit, or from an
1070 incoming bundle), it searches for a suitable delta-base to produce a delta.
1070 incoming bundle), it searches for a suitable delta-base to produce a delta.
1071 This perf command measures how much time we spend in this process. It
1071 This perf command measures how much time we spend in this process. It
1072 operates on an already stored revision.
1072 operates on an already stored revision.
1073
1073
1074 See `hg help debug-delta-find` for another related command.
1074 See `hg help debug-delta-find` for another related command.
1075 """
1075 """
1076 from mercurial import revlogutils
1076 from mercurial import revlogutils
1077 import mercurial.revlogutils.deltas as deltautil
1077 import mercurial.revlogutils.deltas as deltautil
1078
1078
1079 opts = _byteskwargs(opts)
1079 opts = _byteskwargs(opts)
1080 if arg_2 is None:
1080 if arg_2 is None:
1081 file_ = None
1081 file_ = None
1082 rev = arg_1
1082 rev = arg_1
1083 else:
1083 else:
1084 file_ = arg_1
1084 file_ = arg_1
1085 rev = arg_2
1085 rev = arg_2
1086
1086
1087 repo = repo.unfiltered()
1087 repo = repo.unfiltered()
1088
1088
1089 timer, fm = gettimer(ui, opts)
1089 timer, fm = gettimer(ui, opts)
1090
1090
1091 rev = int(rev)
1091 rev = int(rev)
1092
1092
1093 revlog = cmdutil.openrevlog(repo, b'perf::delta-find', file_, opts)
1093 revlog = cmdutil.openrevlog(repo, b'perf::delta-find', file_, opts)
1094
1094
1095 deltacomputer = deltautil.deltacomputer(revlog)
1095 deltacomputer = deltautil.deltacomputer(revlog)
1096
1096
1097 node = revlog.node(rev)
1097 node = revlog.node(rev)
1098 p1r, p2r = revlog.parentrevs(rev)
1098 p1r, p2r = revlog.parentrevs(rev)
1099 p1 = revlog.node(p1r)
1099 p1 = revlog.node(p1r)
1100 p2 = revlog.node(p2r)
1100 p2 = revlog.node(p2r)
1101 full_text = revlog.revision(rev)
1101 full_text = revlog.revision(rev)
1102 textlen = len(full_text)
1102 textlen = len(full_text)
1103 cachedelta = None
1103 cachedelta = None
1104 flags = revlog.flags(rev)
1104 flags = revlog.flags(rev)
1105
1105
1106 revinfo = revlogutils.revisioninfo(
1106 revinfo = revlogutils.revisioninfo(
1107 node,
1107 node,
1108 p1,
1108 p1,
1109 p2,
1109 p2,
1110 [full_text], # btext
1110 [full_text], # btext
1111 textlen,
1111 textlen,
1112 cachedelta,
1112 cachedelta,
1113 flags,
1113 flags,
1114 )
1114 )
1115
1115
1116 # Note: we should probably purge the potential caches (like the full
1116 # Note: we should probably purge the potential caches (like the full
1117 # manifest cache) between runs.
1117 # manifest cache) between runs.
1118 def find_one():
1118 def find_one():
1119 with revlog._datafp() as fh:
1119 with revlog._datafp() as fh:
1120 deltacomputer.finddeltainfo(revinfo, fh, target_rev=rev)
1120 deltacomputer.finddeltainfo(revinfo, fh, target_rev=rev)
1121
1121
1122 timer(find_one)
1122 timer(find_one)
1123 fm.end()
1123 fm.end()
1124
1124
1125
1125
1126 @command(b'perf::discovery|perfdiscovery', formatteropts, b'PATH')
1126 @command(b'perf::discovery|perfdiscovery', formatteropts, b'PATH')
1127 def perfdiscovery(ui, repo, path, **opts):
1127 def perfdiscovery(ui, repo, path, **opts):
1128 """benchmark discovery between local repo and the peer at given path"""
1128 """benchmark discovery between local repo and the peer at given path"""
1129 repos = [repo, None]
1129 repos = [repo, None]
1130 timer, fm = gettimer(ui, opts)
1130 timer, fm = gettimer(ui, opts)
1131
1131
1132 try:
1132 try:
1133 from mercurial.utils.urlutil import get_unique_pull_path_obj
1133 from mercurial.utils.urlutil import get_unique_pull_path_obj
1134
1134
1135 path = get_unique_pull_path_obj(b'perfdiscovery', ui, path)
1135 path = get_unique_pull_path_obj(b'perfdiscovery', ui, path)
1136 except ImportError:
1136 except ImportError:
1137 try:
1137 try:
1138 from mercurial.utils.urlutil import get_unique_pull_path
1138 from mercurial.utils.urlutil import get_unique_pull_path
1139
1139
1140 path = get_unique_pull_path(b'perfdiscovery', repo, ui, path)[0]
1140 path = get_unique_pull_path(b'perfdiscovery', repo, ui, path)[0]
1141 except ImportError:
1141 except ImportError:
1142 path = ui.expandpath(path)
1142 path = ui.expandpath(path)
1143
1143
1144 def s():
1144 def s():
1145 repos[1] = hg.peer(ui, opts, path)
1145 repos[1] = hg.peer(ui, opts, path)
1146
1146
1147 def d():
1147 def d():
1148 setdiscovery.findcommonheads(ui, *repos)
1148 setdiscovery.findcommonheads(ui, *repos)
1149
1149
1150 timer(d, setup=s)
1150 timer(d, setup=s)
1151 fm.end()
1151 fm.end()
1152
1152
1153
1153
1154 @command(
1154 @command(
1155 b'perf::bookmarks|perfbookmarks',
1155 b'perf::bookmarks|perfbookmarks',
1156 formatteropts
1156 formatteropts
1157 + [
1157 + [
1158 (b'', b'clear-revlogs', False, b'refresh changelog and manifest'),
1158 (b'', b'clear-revlogs', False, b'refresh changelog and manifest'),
1159 ],
1159 ],
1160 )
1160 )
1161 def perfbookmarks(ui, repo, **opts):
1161 def perfbookmarks(ui, repo, **opts):
1162 """benchmark parsing bookmarks from disk to memory"""
1162 """benchmark parsing bookmarks from disk to memory"""
1163 opts = _byteskwargs(opts)
1163 opts = _byteskwargs(opts)
1164 timer, fm = gettimer(ui, opts)
1164 timer, fm = gettimer(ui, opts)
1165
1165
1166 clearrevlogs = opts[b'clear_revlogs']
1166 clearrevlogs = opts[b'clear_revlogs']
1167
1167
1168 def s():
1168 def s():
1169 if clearrevlogs:
1169 if clearrevlogs:
1170 clearchangelog(repo)
1170 clearchangelog(repo)
1171 clearfilecache(repo, b'_bookmarks')
1171 clearfilecache(repo, b'_bookmarks')
1172
1172
1173 def d():
1173 def d():
1174 repo._bookmarks
1174 repo._bookmarks
1175
1175
1176 timer(d, setup=s)
1176 timer(d, setup=s)
1177 fm.end()
1177 fm.end()
1178
1178
1179
1179
1180 @command(
1180 @command(
1181 b'perf::bundle',
1181 b'perf::bundle',
1182 [
1182 [
1183 (
1183 (
1184 b'r',
1184 b'r',
1185 b'rev',
1185 b'rev',
1186 [],
1186 [],
1187 b'changesets to bundle',
1187 b'changesets to bundle',
1188 b'REV',
1188 b'REV',
1189 ),
1189 ),
1190 (
1190 (
1191 b't',
1191 b't',
1192 b'type',
1192 b'type',
1193 b'none',
1193 b'none',
1194 b'bundlespec to use (see `hg help bundlespec`)',
1194 b'bundlespec to use (see `hg help bundlespec`)',
1195 b'TYPE',
1195 b'TYPE',
1196 ),
1196 ),
1197 ]
1197 ]
1198 + formatteropts,
1198 + formatteropts,
1199 b'REVS',
1199 b'REVS',
1200 )
1200 )
1201 def perfbundle(ui, repo, *revs, **opts):
1201 def perfbundle(ui, repo, *revs, **opts):
1202 """benchmark the creation of a bundle from a repository
1202 """benchmark the creation of a bundle from a repository
1203
1203
1204 For now, this only supports "none" compression.
1204 For now, this only supports "none" compression.
1205 """
1205 """
1206 try:
1206 try:
1207 from mercurial import bundlecaches
1207 from mercurial import bundlecaches
1208
1208
1209 parsebundlespec = bundlecaches.parsebundlespec
1209 parsebundlespec = bundlecaches.parsebundlespec
1210 except ImportError:
1210 except ImportError:
1211 from mercurial import exchange
1211 from mercurial import exchange
1212
1212
1213 parsebundlespec = exchange.parsebundlespec
1213 parsebundlespec = exchange.parsebundlespec
1214
1214
1215 from mercurial import discovery
1215 from mercurial import discovery
1216 from mercurial import bundle2
1216 from mercurial import bundle2
1217
1217
1218 opts = _byteskwargs(opts)
1218 opts = _byteskwargs(opts)
1219 timer, fm = gettimer(ui, opts)
1219 timer, fm = gettimer(ui, opts)
1220
1220
1221 cl = repo.changelog
1221 cl = repo.changelog
1222 revs = list(revs)
1222 revs = list(revs)
1223 revs.extend(opts.get(b'rev', ()))
1223 revs.extend(opts.get(b'rev', ()))
1224 revs = scmutil.revrange(repo, revs)
1224 revs = scmutil.revrange(repo, revs)
1225 if not revs:
1225 if not revs:
1226 raise error.Abort(b"not revision specified")
1226 raise error.Abort(b"not revision specified")
1227 # make it a consistent set (ie: without topological gaps)
1227 # make it a consistent set (ie: without topological gaps)
1228 old_len = len(revs)
1228 old_len = len(revs)
1229 revs = list(repo.revs(b"%ld::%ld", revs, revs))
1229 revs = list(repo.revs(b"%ld::%ld", revs, revs))
1230 if old_len != len(revs):
1230 if old_len != len(revs):
1231 new_count = len(revs) - old_len
1231 new_count = len(revs) - old_len
1232 msg = b"add %d new revisions to make it a consistent set\n"
1232 msg = b"add %d new revisions to make it a consistent set\n"
1233 ui.write_err(msg % new_count)
1233 ui.write_err(msg % new_count)
1234
1234
1235 targets = [cl.node(r) for r in repo.revs(b"heads(::%ld)", revs)]
1235 targets = [cl.node(r) for r in repo.revs(b"heads(::%ld)", revs)]
1236 bases = [cl.node(r) for r in repo.revs(b"heads(::%ld - %ld)", revs, revs)]
1236 bases = [cl.node(r) for r in repo.revs(b"heads(::%ld - %ld)", revs, revs)]
1237 outgoing = discovery.outgoing(repo, bases, targets)
1237 outgoing = discovery.outgoing(repo, bases, targets)
1238
1238
1239 bundle_spec = opts.get(b'type')
1239 bundle_spec = opts.get(b'type')
1240
1240
1241 bundle_spec = parsebundlespec(repo, bundle_spec, strict=False)
1241 bundle_spec = parsebundlespec(repo, bundle_spec, strict=False)
1242
1242
1243 cgversion = bundle_spec.params.get(b"cg.version")
1243 cgversion = bundle_spec.params.get(b"cg.version")
1244 if cgversion is None:
1244 if cgversion is None:
1245 if bundle_spec.version == b'v1':
1245 if bundle_spec.version == b'v1':
1246 cgversion = b'01'
1246 cgversion = b'01'
1247 if bundle_spec.version == b'v2':
1247 if bundle_spec.version == b'v2':
1248 cgversion = b'02'
1248 cgversion = b'02'
1249 if cgversion not in changegroup.supportedoutgoingversions(repo):
1249 if cgversion not in changegroup.supportedoutgoingversions(repo):
1250 err = b"repository does not support bundle version %s"
1250 err = b"repository does not support bundle version %s"
1251 raise error.Abort(err % cgversion)
1251 raise error.Abort(err % cgversion)
1252
1252
1253 if cgversion == b'01': # bundle1
1253 if cgversion == b'01': # bundle1
1254 bversion = b'HG10' + bundle_spec.wirecompression
1254 bversion = b'HG10' + bundle_spec.wirecompression
1255 bcompression = None
1255 bcompression = None
1256 elif cgversion in (b'02', b'03'):
1256 elif cgversion in (b'02', b'03'):
1257 bversion = b'HG20'
1257 bversion = b'HG20'
1258 bcompression = bundle_spec.wirecompression
1258 bcompression = bundle_spec.wirecompression
1259 else:
1259 else:
1260 err = b'perf::bundle: unexpected changegroup version %s'
1260 err = b'perf::bundle: unexpected changegroup version %s'
1261 raise error.ProgrammingError(err % cgversion)
1261 raise error.ProgrammingError(err % cgversion)
1262
1262
1263 if bcompression is None:
1263 if bcompression is None:
1264 bcompression = b'UN'
1264 bcompression = b'UN'
1265
1265
1266 if bcompression != b'UN':
1266 if bcompression != b'UN':
1267 err = b'perf::bundle: compression currently unsupported: %s'
1267 err = b'perf::bundle: compression currently unsupported: %s'
1268 raise error.ProgrammingError(err % bcompression)
1268 raise error.ProgrammingError(err % bcompression)
1269
1269
1270 def do_bundle():
1270 def do_bundle():
1271 bundle2.writenewbundle(
1271 bundle2.writenewbundle(
1272 ui,
1272 ui,
1273 repo,
1273 repo,
1274 b'perf::bundle',
1274 b'perf::bundle',
1275 os.devnull,
1275 os.devnull,
1276 bversion,
1276 bversion,
1277 outgoing,
1277 outgoing,
1278 bundle_spec.params,
1278 bundle_spec.params,
1279 )
1279 )
1280
1280
1281 timer(do_bundle)
1281 timer(do_bundle)
1282 fm.end()
1282 fm.end()
1283
1283
1284
1284
1285 @command(b'perf::bundleread|perfbundleread', formatteropts, b'BUNDLE')
1285 @command(b'perf::bundleread|perfbundleread', formatteropts, b'BUNDLE')
1286 def perfbundleread(ui, repo, bundlepath, **opts):
1286 def perfbundleread(ui, repo, bundlepath, **opts):
1287 """Benchmark reading of bundle files.
1287 """Benchmark reading of bundle files.
1288
1288
1289 This command is meant to isolate the I/O part of bundle reading as
1289 This command is meant to isolate the I/O part of bundle reading as
1290 much as possible.
1290 much as possible.
1291 """
1291 """
1292 from mercurial import (
1292 from mercurial import (
1293 bundle2,
1293 bundle2,
1294 exchange,
1294 exchange,
1295 streamclone,
1295 streamclone,
1296 )
1296 )
1297
1297
1298 opts = _byteskwargs(opts)
1298 opts = _byteskwargs(opts)
1299
1299
1300 def makebench(fn):
1300 def makebench(fn):
1301 def run():
1301 def run():
1302 with open(bundlepath, b'rb') as fh:
1302 with open(bundlepath, b'rb') as fh:
1303 bundle = exchange.readbundle(ui, fh, bundlepath)
1303 bundle = exchange.readbundle(ui, fh, bundlepath)
1304 fn(bundle)
1304 fn(bundle)
1305
1305
1306 return run
1306 return run
1307
1307
1308 def makereadnbytes(size):
1308 def makereadnbytes(size):
1309 def run():
1309 def run():
1310 with open(bundlepath, b'rb') as fh:
1310 with open(bundlepath, b'rb') as fh:
1311 bundle = exchange.readbundle(ui, fh, bundlepath)
1311 bundle = exchange.readbundle(ui, fh, bundlepath)
1312 while bundle.read(size):
1312 while bundle.read(size):
1313 pass
1313 pass
1314
1314
1315 return run
1315 return run
1316
1316
1317 def makestdioread(size):
1317 def makestdioread(size):
1318 def run():
1318 def run():
1319 with open(bundlepath, b'rb') as fh:
1319 with open(bundlepath, b'rb') as fh:
1320 while fh.read(size):
1320 while fh.read(size):
1321 pass
1321 pass
1322
1322
1323 return run
1323 return run
1324
1324
1325 # bundle1
1325 # bundle1
1326
1326
1327 def deltaiter(bundle):
1327 def deltaiter(bundle):
1328 for delta in bundle.deltaiter():
1328 for delta in bundle.deltaiter():
1329 pass
1329 pass
1330
1330
1331 def iterchunks(bundle):
1331 def iterchunks(bundle):
1332 for chunk in bundle.getchunks():
1332 for chunk in bundle.getchunks():
1333 pass
1333 pass
1334
1334
1335 # bundle2
1335 # bundle2
1336
1336
1337 def forwardchunks(bundle):
1337 def forwardchunks(bundle):
1338 for chunk in bundle._forwardchunks():
1338 for chunk in bundle._forwardchunks():
1339 pass
1339 pass
1340
1340
1341 def iterparts(bundle):
1341 def iterparts(bundle):
1342 for part in bundle.iterparts():
1342 for part in bundle.iterparts():
1343 pass
1343 pass
1344
1344
1345 def iterpartsseekable(bundle):
1345 def iterpartsseekable(bundle):
1346 for part in bundle.iterparts(seekable=True):
1346 for part in bundle.iterparts(seekable=True):
1347 pass
1347 pass
1348
1348
1349 def seek(bundle):
1349 def seek(bundle):
1350 for part in bundle.iterparts(seekable=True):
1350 for part in bundle.iterparts(seekable=True):
1351 part.seek(0, os.SEEK_END)
1351 part.seek(0, os.SEEK_END)
1352
1352
1353 def makepartreadnbytes(size):
1353 def makepartreadnbytes(size):
1354 def run():
1354 def run():
1355 with open(bundlepath, b'rb') as fh:
1355 with open(bundlepath, b'rb') as fh:
1356 bundle = exchange.readbundle(ui, fh, bundlepath)
1356 bundle = exchange.readbundle(ui, fh, bundlepath)
1357 for part in bundle.iterparts():
1357 for part in bundle.iterparts():
1358 while part.read(size):
1358 while part.read(size):
1359 pass
1359 pass
1360
1360
1361 return run
1361 return run
1362
1362
1363 benches = [
1363 benches = [
1364 (makestdioread(8192), b'read(8k)'),
1364 (makestdioread(8192), b'read(8k)'),
1365 (makestdioread(16384), b'read(16k)'),
1365 (makestdioread(16384), b'read(16k)'),
1366 (makestdioread(32768), b'read(32k)'),
1366 (makestdioread(32768), b'read(32k)'),
1367 (makestdioread(131072), b'read(128k)'),
1367 (makestdioread(131072), b'read(128k)'),
1368 ]
1368 ]
1369
1369
1370 with open(bundlepath, b'rb') as fh:
1370 with open(bundlepath, b'rb') as fh:
1371 bundle = exchange.readbundle(ui, fh, bundlepath)
1371 bundle = exchange.readbundle(ui, fh, bundlepath)
1372
1372
1373 if isinstance(bundle, changegroup.cg1unpacker):
1373 if isinstance(bundle, changegroup.cg1unpacker):
1374 benches.extend(
1374 benches.extend(
1375 [
1375 [
1376 (makebench(deltaiter), b'cg1 deltaiter()'),
1376 (makebench(deltaiter), b'cg1 deltaiter()'),
1377 (makebench(iterchunks), b'cg1 getchunks()'),
1377 (makebench(iterchunks), b'cg1 getchunks()'),
1378 (makereadnbytes(8192), b'cg1 read(8k)'),
1378 (makereadnbytes(8192), b'cg1 read(8k)'),
1379 (makereadnbytes(16384), b'cg1 read(16k)'),
1379 (makereadnbytes(16384), b'cg1 read(16k)'),
1380 (makereadnbytes(32768), b'cg1 read(32k)'),
1380 (makereadnbytes(32768), b'cg1 read(32k)'),
1381 (makereadnbytes(131072), b'cg1 read(128k)'),
1381 (makereadnbytes(131072), b'cg1 read(128k)'),
1382 ]
1382 ]
1383 )
1383 )
1384 elif isinstance(bundle, bundle2.unbundle20):
1384 elif isinstance(bundle, bundle2.unbundle20):
1385 benches.extend(
1385 benches.extend(
1386 [
1386 [
1387 (makebench(forwardchunks), b'bundle2 forwardchunks()'),
1387 (makebench(forwardchunks), b'bundle2 forwardchunks()'),
1388 (makebench(iterparts), b'bundle2 iterparts()'),
1388 (makebench(iterparts), b'bundle2 iterparts()'),
1389 (
1389 (
1390 makebench(iterpartsseekable),
1390 makebench(iterpartsseekable),
1391 b'bundle2 iterparts() seekable',
1391 b'bundle2 iterparts() seekable',
1392 ),
1392 ),
1393 (makebench(seek), b'bundle2 part seek()'),
1393 (makebench(seek), b'bundle2 part seek()'),
1394 (makepartreadnbytes(8192), b'bundle2 part read(8k)'),
1394 (makepartreadnbytes(8192), b'bundle2 part read(8k)'),
1395 (makepartreadnbytes(16384), b'bundle2 part read(16k)'),
1395 (makepartreadnbytes(16384), b'bundle2 part read(16k)'),
1396 (makepartreadnbytes(32768), b'bundle2 part read(32k)'),
1396 (makepartreadnbytes(32768), b'bundle2 part read(32k)'),
1397 (makepartreadnbytes(131072), b'bundle2 part read(128k)'),
1397 (makepartreadnbytes(131072), b'bundle2 part read(128k)'),
1398 ]
1398 ]
1399 )
1399 )
1400 elif isinstance(bundle, streamclone.streamcloneapplier):
1400 elif isinstance(bundle, streamclone.streamcloneapplier):
1401 raise error.Abort(b'stream clone bundles not supported')
1401 raise error.Abort(b'stream clone bundles not supported')
1402 else:
1402 else:
1403 raise error.Abort(b'unhandled bundle type: %s' % type(bundle))
1403 raise error.Abort(b'unhandled bundle type: %s' % type(bundle))
1404
1404
1405 for fn, title in benches:
1405 for fn, title in benches:
1406 timer, fm = gettimer(ui, opts)
1406 timer, fm = gettimer(ui, opts)
1407 timer(fn, title=title)
1407 timer(fn, title=title)
1408 fm.end()
1408 fm.end()
1409
1409
1410
1410
1411 @command(
1411 @command(
1412 b'perf::changegroupchangelog|perfchangegroupchangelog',
1412 b'perf::changegroupchangelog|perfchangegroupchangelog',
1413 formatteropts
1413 formatteropts
1414 + [
1414 + [
1415 (b'', b'cgversion', b'02', b'changegroup version'),
1415 (b'', b'cgversion', b'02', b'changegroup version'),
1416 (b'r', b'rev', b'', b'revisions to add to changegroup'),
1416 (b'r', b'rev', b'', b'revisions to add to changegroup'),
1417 ],
1417 ],
1418 )
1418 )
1419 def perfchangegroupchangelog(ui, repo, cgversion=b'02', rev=None, **opts):
1419 def perfchangegroupchangelog(ui, repo, cgversion=b'02', rev=None, **opts):
1420 """Benchmark producing a changelog group for a changegroup.
1420 """Benchmark producing a changelog group for a changegroup.
1421
1421
1422 This measures the time spent processing the changelog during a
1422 This measures the time spent processing the changelog during a
1423 bundle operation. This occurs during `hg bundle` and on a server
1423 bundle operation. This occurs during `hg bundle` and on a server
1424 processing a `getbundle` wire protocol request (handles clones
1424 processing a `getbundle` wire protocol request (handles clones
1425 and pull requests).
1425 and pull requests).
1426
1426
1427 By default, all revisions are added to the changegroup.
1427 By default, all revisions are added to the changegroup.
1428 """
1428 """
1429 opts = _byteskwargs(opts)
1429 opts = _byteskwargs(opts)
1430 cl = repo.changelog
1430 cl = repo.changelog
1431 nodes = [cl.lookup(r) for r in repo.revs(rev or b'all()')]
1431 nodes = [cl.lookup(r) for r in repo.revs(rev or b'all()')]
1432 bundler = changegroup.getbundler(cgversion, repo)
1432 bundler = changegroup.getbundler(cgversion, repo)
1433
1433
1434 def d():
1434 def d():
1435 state, chunks = bundler._generatechangelog(cl, nodes)
1435 state, chunks = bundler._generatechangelog(cl, nodes)
1436 for chunk in chunks:
1436 for chunk in chunks:
1437 pass
1437 pass
1438
1438
1439 timer, fm = gettimer(ui, opts)
1439 timer, fm = gettimer(ui, opts)
1440
1440
1441 # Terminal printing can interfere with timing. So disable it.
1441 # Terminal printing can interfere with timing. So disable it.
1442 with ui.configoverride({(b'progress', b'disable'): True}):
1442 with ui.configoverride({(b'progress', b'disable'): True}):
1443 timer(d)
1443 timer(d)
1444
1444
1445 fm.end()
1445 fm.end()
1446
1446
1447
1447
1448 @command(b'perf::dirs|perfdirs', formatteropts)
1448 @command(b'perf::dirs|perfdirs', formatteropts)
1449 def perfdirs(ui, repo, **opts):
1449 def perfdirs(ui, repo, **opts):
1450 opts = _byteskwargs(opts)
1450 opts = _byteskwargs(opts)
1451 timer, fm = gettimer(ui, opts)
1451 timer, fm = gettimer(ui, opts)
1452 dirstate = repo.dirstate
1452 dirstate = repo.dirstate
1453 b'a' in dirstate
1453 b'a' in dirstate
1454
1454
1455 def d():
1455 def d():
1456 dirstate.hasdir(b'a')
1456 dirstate.hasdir(b'a')
1457 try:
1457 try:
1458 del dirstate._map._dirs
1458 del dirstate._map._dirs
1459 except AttributeError:
1459 except AttributeError:
1460 pass
1460 pass
1461
1461
1462 timer(d)
1462 timer(d)
1463 fm.end()
1463 fm.end()
1464
1464
1465
1465
1466 @command(
1466 @command(
1467 b'perf::dirstate|perfdirstate',
1467 b'perf::dirstate|perfdirstate',
1468 [
1468 [
1469 (
1469 (
1470 b'',
1470 b'',
1471 b'iteration',
1471 b'iteration',
1472 None,
1472 None,
1473 b'benchmark a full iteration for the dirstate',
1473 b'benchmark a full iteration for the dirstate',
1474 ),
1474 ),
1475 (
1475 (
1476 b'',
1476 b'',
1477 b'contains',
1477 b'contains',
1478 None,
1478 None,
1479 b'benchmark a large amount of `nf in dirstate` calls',
1479 b'benchmark a large amount of `nf in dirstate` calls',
1480 ),
1480 ),
1481 ]
1481 ]
1482 + formatteropts,
1482 + formatteropts,
1483 )
1483 )
1484 def perfdirstate(ui, repo, **opts):
1484 def perfdirstate(ui, repo, **opts):
1485 """benchmap the time of various distate operations
1485 """benchmap the time of various distate operations
1486
1486
1487 By default benchmark the time necessary to load a dirstate from scratch.
1487 By default benchmark the time necessary to load a dirstate from scratch.
1488 The dirstate is loaded to the point were a "contains" request can be
1488 The dirstate is loaded to the point were a "contains" request can be
1489 answered.
1489 answered.
1490 """
1490 """
1491 opts = _byteskwargs(opts)
1491 opts = _byteskwargs(opts)
1492 timer, fm = gettimer(ui, opts)
1492 timer, fm = gettimer(ui, opts)
1493 b"a" in repo.dirstate
1493 b"a" in repo.dirstate
1494
1494
1495 if opts[b'iteration'] and opts[b'contains']:
1495 if opts[b'iteration'] and opts[b'contains']:
1496 msg = b'only specify one of --iteration or --contains'
1496 msg = b'only specify one of --iteration or --contains'
1497 raise error.Abort(msg)
1497 raise error.Abort(msg)
1498
1498
1499 if opts[b'iteration']:
1499 if opts[b'iteration']:
1500 setup = None
1500 setup = None
1501 dirstate = repo.dirstate
1501 dirstate = repo.dirstate
1502
1502
1503 def d():
1503 def d():
1504 for f in dirstate:
1504 for f in dirstate:
1505 pass
1505 pass
1506
1506
1507 elif opts[b'contains']:
1507 elif opts[b'contains']:
1508 setup = None
1508 setup = None
1509 dirstate = repo.dirstate
1509 dirstate = repo.dirstate
1510 allfiles = list(dirstate)
1510 allfiles = list(dirstate)
1511 # also add file path that will be "missing" from the dirstate
1511 # also add file path that will be "missing" from the dirstate
1512 allfiles.extend([f[::-1] for f in allfiles])
1512 allfiles.extend([f[::-1] for f in allfiles])
1513
1513
1514 def d():
1514 def d():
1515 for f in allfiles:
1515 for f in allfiles:
1516 f in dirstate
1516 f in dirstate
1517
1517
1518 else:
1518 else:
1519
1519
1520 def setup():
1520 def setup():
1521 repo.dirstate.invalidate()
1521 repo.dirstate.invalidate()
1522
1522
1523 def d():
1523 def d():
1524 b"a" in repo.dirstate
1524 b"a" in repo.dirstate
1525
1525
1526 timer(d, setup=setup)
1526 timer(d, setup=setup)
1527 fm.end()
1527 fm.end()
1528
1528
1529
1529
1530 @command(b'perf::dirstatedirs|perfdirstatedirs', formatteropts)
1530 @command(b'perf::dirstatedirs|perfdirstatedirs', formatteropts)
1531 def perfdirstatedirs(ui, repo, **opts):
1531 def perfdirstatedirs(ui, repo, **opts):
1532 """benchmap a 'dirstate.hasdir' call from an empty `dirs` cache"""
1532 """benchmap a 'dirstate.hasdir' call from an empty `dirs` cache"""
1533 opts = _byteskwargs(opts)
1533 opts = _byteskwargs(opts)
1534 timer, fm = gettimer(ui, opts)
1534 timer, fm = gettimer(ui, opts)
1535 repo.dirstate.hasdir(b"a")
1535 repo.dirstate.hasdir(b"a")
1536
1536
1537 def setup():
1537 def setup():
1538 try:
1538 try:
1539 del repo.dirstate._map._dirs
1539 del repo.dirstate._map._dirs
1540 except AttributeError:
1540 except AttributeError:
1541 pass
1541 pass
1542
1542
1543 def d():
1543 def d():
1544 repo.dirstate.hasdir(b"a")
1544 repo.dirstate.hasdir(b"a")
1545
1545
1546 timer(d, setup=setup)
1546 timer(d, setup=setup)
1547 fm.end()
1547 fm.end()
1548
1548
1549
1549
1550 @command(b'perf::dirstatefoldmap|perfdirstatefoldmap', formatteropts)
1550 @command(b'perf::dirstatefoldmap|perfdirstatefoldmap', formatteropts)
1551 def perfdirstatefoldmap(ui, repo, **opts):
1551 def perfdirstatefoldmap(ui, repo, **opts):
1552 """benchmap a `dirstate._map.filefoldmap.get()` request
1552 """benchmap a `dirstate._map.filefoldmap.get()` request
1553
1553
1554 The dirstate filefoldmap cache is dropped between every request.
1554 The dirstate filefoldmap cache is dropped between every request.
1555 """
1555 """
1556 opts = _byteskwargs(opts)
1556 opts = _byteskwargs(opts)
1557 timer, fm = gettimer(ui, opts)
1557 timer, fm = gettimer(ui, opts)
1558 dirstate = repo.dirstate
1558 dirstate = repo.dirstate
1559 dirstate._map.filefoldmap.get(b'a')
1559 dirstate._map.filefoldmap.get(b'a')
1560
1560
1561 def setup():
1561 def setup():
1562 del dirstate._map.filefoldmap
1562 del dirstate._map.filefoldmap
1563
1563
1564 def d():
1564 def d():
1565 dirstate._map.filefoldmap.get(b'a')
1565 dirstate._map.filefoldmap.get(b'a')
1566
1566
1567 timer(d, setup=setup)
1567 timer(d, setup=setup)
1568 fm.end()
1568 fm.end()
1569
1569
1570
1570
1571 @command(b'perf::dirfoldmap|perfdirfoldmap', formatteropts)
1571 @command(b'perf::dirfoldmap|perfdirfoldmap', formatteropts)
1572 def perfdirfoldmap(ui, repo, **opts):
1572 def perfdirfoldmap(ui, repo, **opts):
1573 """benchmap a `dirstate._map.dirfoldmap.get()` request
1573 """benchmap a `dirstate._map.dirfoldmap.get()` request
1574
1574
1575 The dirstate dirfoldmap cache is dropped between every request.
1575 The dirstate dirfoldmap cache is dropped between every request.
1576 """
1576 """
1577 opts = _byteskwargs(opts)
1577 opts = _byteskwargs(opts)
1578 timer, fm = gettimer(ui, opts)
1578 timer, fm = gettimer(ui, opts)
1579 dirstate = repo.dirstate
1579 dirstate = repo.dirstate
1580 dirstate._map.dirfoldmap.get(b'a')
1580 dirstate._map.dirfoldmap.get(b'a')
1581
1581
1582 def setup():
1582 def setup():
1583 del dirstate._map.dirfoldmap
1583 del dirstate._map.dirfoldmap
1584 try:
1584 try:
1585 del dirstate._map._dirs
1585 del dirstate._map._dirs
1586 except AttributeError:
1586 except AttributeError:
1587 pass
1587 pass
1588
1588
1589 def d():
1589 def d():
1590 dirstate._map.dirfoldmap.get(b'a')
1590 dirstate._map.dirfoldmap.get(b'a')
1591
1591
1592 timer(d, setup=setup)
1592 timer(d, setup=setup)
1593 fm.end()
1593 fm.end()
1594
1594
1595
1595
1596 @command(b'perf::dirstatewrite|perfdirstatewrite', formatteropts)
1596 @command(b'perf::dirstatewrite|perfdirstatewrite', formatteropts)
1597 def perfdirstatewrite(ui, repo, **opts):
1597 def perfdirstatewrite(ui, repo, **opts):
1598 """benchmap the time it take to write a dirstate on disk"""
1598 """benchmap the time it take to write a dirstate on disk"""
1599 opts = _byteskwargs(opts)
1599 opts = _byteskwargs(opts)
1600 timer, fm = gettimer(ui, opts)
1600 timer, fm = gettimer(ui, opts)
1601 ds = repo.dirstate
1601 ds = repo.dirstate
1602 b"a" in ds
1602 b"a" in ds
1603
1603
1604 def setup():
1604 def setup():
1605 ds._dirty = True
1605 ds._dirty = True
1606
1606
1607 def d():
1607 def d():
1608 ds.write(repo.currenttransaction())
1608 ds.write(repo.currenttransaction())
1609
1609
1610 with repo.wlock():
1610 with repo.wlock():
1611 timer(d, setup=setup)
1611 timer(d, setup=setup)
1612 fm.end()
1612 fm.end()
1613
1613
1614
1614
1615 def _getmergerevs(repo, opts):
1615 def _getmergerevs(repo, opts):
1616 """parse command argument to return rev involved in merge
1616 """parse command argument to return rev involved in merge
1617
1617
1618 input: options dictionnary with `rev`, `from` and `bse`
1618 input: options dictionnary with `rev`, `from` and `bse`
1619 output: (localctx, otherctx, basectx)
1619 output: (localctx, otherctx, basectx)
1620 """
1620 """
1621 if opts[b'from']:
1621 if opts[b'from']:
1622 fromrev = scmutil.revsingle(repo, opts[b'from'])
1622 fromrev = scmutil.revsingle(repo, opts[b'from'])
1623 wctx = repo[fromrev]
1623 wctx = repo[fromrev]
1624 else:
1624 else:
1625 wctx = repo[None]
1625 wctx = repo[None]
1626 # we don't want working dir files to be stat'd in the benchmark, so
1626 # we don't want working dir files to be stat'd in the benchmark, so
1627 # prime that cache
1627 # prime that cache
1628 wctx.dirty()
1628 wctx.dirty()
1629 rctx = scmutil.revsingle(repo, opts[b'rev'], opts[b'rev'])
1629 rctx = scmutil.revsingle(repo, opts[b'rev'], opts[b'rev'])
1630 if opts[b'base']:
1630 if opts[b'base']:
1631 fromrev = scmutil.revsingle(repo, opts[b'base'])
1631 fromrev = scmutil.revsingle(repo, opts[b'base'])
1632 ancestor = repo[fromrev]
1632 ancestor = repo[fromrev]
1633 else:
1633 else:
1634 ancestor = wctx.ancestor(rctx)
1634 ancestor = wctx.ancestor(rctx)
1635 return (wctx, rctx, ancestor)
1635 return (wctx, rctx, ancestor)
1636
1636
1637
1637
1638 @command(
1638 @command(
1639 b'perf::mergecalculate|perfmergecalculate',
1639 b'perf::mergecalculate|perfmergecalculate',
1640 [
1640 [
1641 (b'r', b'rev', b'.', b'rev to merge against'),
1641 (b'r', b'rev', b'.', b'rev to merge against'),
1642 (b'', b'from', b'', b'rev to merge from'),
1642 (b'', b'from', b'', b'rev to merge from'),
1643 (b'', b'base', b'', b'the revision to use as base'),
1643 (b'', b'base', b'', b'the revision to use as base'),
1644 ]
1644 ]
1645 + formatteropts,
1645 + formatteropts,
1646 )
1646 )
1647 def perfmergecalculate(ui, repo, **opts):
1647 def perfmergecalculate(ui, repo, **opts):
1648 opts = _byteskwargs(opts)
1648 opts = _byteskwargs(opts)
1649 timer, fm = gettimer(ui, opts)
1649 timer, fm = gettimer(ui, opts)
1650
1650
1651 wctx, rctx, ancestor = _getmergerevs(repo, opts)
1651 wctx, rctx, ancestor = _getmergerevs(repo, opts)
1652
1652
1653 def d():
1653 def d():
1654 # acceptremote is True because we don't want prompts in the middle of
1654 # acceptremote is True because we don't want prompts in the middle of
1655 # our benchmark
1655 # our benchmark
1656 merge.calculateupdates(
1656 merge.calculateupdates(
1657 repo,
1657 repo,
1658 wctx,
1658 wctx,
1659 rctx,
1659 rctx,
1660 [ancestor],
1660 [ancestor],
1661 branchmerge=False,
1661 branchmerge=False,
1662 force=False,
1662 force=False,
1663 acceptremote=True,
1663 acceptremote=True,
1664 followcopies=True,
1664 followcopies=True,
1665 )
1665 )
1666
1666
1667 timer(d)
1667 timer(d)
1668 fm.end()
1668 fm.end()
1669
1669
1670
1670
1671 @command(
1671 @command(
1672 b'perf::mergecopies|perfmergecopies',
1672 b'perf::mergecopies|perfmergecopies',
1673 [
1673 [
1674 (b'r', b'rev', b'.', b'rev to merge against'),
1674 (b'r', b'rev', b'.', b'rev to merge against'),
1675 (b'', b'from', b'', b'rev to merge from'),
1675 (b'', b'from', b'', b'rev to merge from'),
1676 (b'', b'base', b'', b'the revision to use as base'),
1676 (b'', b'base', b'', b'the revision to use as base'),
1677 ]
1677 ]
1678 + formatteropts,
1678 + formatteropts,
1679 )
1679 )
1680 def perfmergecopies(ui, repo, **opts):
1680 def perfmergecopies(ui, repo, **opts):
1681 """measure runtime of `copies.mergecopies`"""
1681 """measure runtime of `copies.mergecopies`"""
1682 opts = _byteskwargs(opts)
1682 opts = _byteskwargs(opts)
1683 timer, fm = gettimer(ui, opts)
1683 timer, fm = gettimer(ui, opts)
1684 wctx, rctx, ancestor = _getmergerevs(repo, opts)
1684 wctx, rctx, ancestor = _getmergerevs(repo, opts)
1685
1685
1686 def d():
1686 def d():
1687 # acceptremote is True because we don't want prompts in the middle of
1687 # acceptremote is True because we don't want prompts in the middle of
1688 # our benchmark
1688 # our benchmark
1689 copies.mergecopies(repo, wctx, rctx, ancestor)
1689 copies.mergecopies(repo, wctx, rctx, ancestor)
1690
1690
1691 timer(d)
1691 timer(d)
1692 fm.end()
1692 fm.end()
1693
1693
1694
1694
1695 @command(b'perf::pathcopies|perfpathcopies', [], b"REV REV")
1695 @command(b'perf::pathcopies|perfpathcopies', [], b"REV REV")
1696 def perfpathcopies(ui, repo, rev1, rev2, **opts):
1696 def perfpathcopies(ui, repo, rev1, rev2, **opts):
1697 """benchmark the copy tracing logic"""
1697 """benchmark the copy tracing logic"""
1698 opts = _byteskwargs(opts)
1698 opts = _byteskwargs(opts)
1699 timer, fm = gettimer(ui, opts)
1699 timer, fm = gettimer(ui, opts)
1700 ctx1 = scmutil.revsingle(repo, rev1, rev1)
1700 ctx1 = scmutil.revsingle(repo, rev1, rev1)
1701 ctx2 = scmutil.revsingle(repo, rev2, rev2)
1701 ctx2 = scmutil.revsingle(repo, rev2, rev2)
1702
1702
1703 def d():
1703 def d():
1704 copies.pathcopies(ctx1, ctx2)
1704 copies.pathcopies(ctx1, ctx2)
1705
1705
1706 timer(d)
1706 timer(d)
1707 fm.end()
1707 fm.end()
1708
1708
1709
1709
1710 @command(
1710 @command(
1711 b'perf::phases|perfphases',
1711 b'perf::phases|perfphases',
1712 [
1712 [
1713 (b'', b'full', False, b'include file reading time too'),
1713 (b'', b'full', False, b'include file reading time too'),
1714 ],
1714 ],
1715 b"",
1715 b"",
1716 )
1716 )
1717 def perfphases(ui, repo, **opts):
1717 def perfphases(ui, repo, **opts):
1718 """benchmark phasesets computation"""
1718 """benchmark phasesets computation"""
1719 opts = _byteskwargs(opts)
1719 opts = _byteskwargs(opts)
1720 timer, fm = gettimer(ui, opts)
1720 timer, fm = gettimer(ui, opts)
1721 _phases = repo._phasecache
1721 _phases = repo._phasecache
1722 full = opts.get(b'full')
1722 full = opts.get(b'full')
1723
1723
1724 def d():
1724 def d():
1725 phases = _phases
1725 phases = _phases
1726 if full:
1726 if full:
1727 clearfilecache(repo, b'_phasecache')
1727 clearfilecache(repo, b'_phasecache')
1728 phases = repo._phasecache
1728 phases = repo._phasecache
1729 phases.invalidate()
1729 phases.invalidate()
1730 phases.loadphaserevs(repo)
1730 phases.loadphaserevs(repo)
1731
1731
1732 timer(d)
1732 timer(d)
1733 fm.end()
1733 fm.end()
1734
1734
1735
1735
1736 @command(b'perf::phasesremote|perfphasesremote', [], b"[DEST]")
1736 @command(b'perf::phasesremote|perfphasesremote', [], b"[DEST]")
1737 def perfphasesremote(ui, repo, dest=None, **opts):
1737 def perfphasesremote(ui, repo, dest=None, **opts):
1738 """benchmark time needed to analyse phases of the remote server"""
1738 """benchmark time needed to analyse phases of the remote server"""
1739 from mercurial.node import bin
1739 from mercurial.node import bin
1740 from mercurial import (
1740 from mercurial import (
1741 exchange,
1741 exchange,
1742 hg,
1742 hg,
1743 phases,
1743 phases,
1744 )
1744 )
1745
1745
1746 opts = _byteskwargs(opts)
1746 opts = _byteskwargs(opts)
1747 timer, fm = gettimer(ui, opts)
1747 timer, fm = gettimer(ui, opts)
1748
1748
1749 path = ui.getpath(dest, default=(b'default-push', b'default'))
1749 path = ui.getpath(dest, default=(b'default-push', b'default'))
1750 if not path:
1750 if not path:
1751 raise error.Abort(
1751 raise error.Abort(
1752 b'default repository not configured!',
1752 b'default repository not configured!',
1753 hint=b"see 'hg help config.paths'",
1753 hint=b"see 'hg help config.paths'",
1754 )
1754 )
1755 if util.safehasattr(path, 'main_path'):
1755 if util.safehasattr(path, 'main_path'):
1756 path = path.get_push_variant()
1756 path = path.get_push_variant()
1757 dest = path.loc
1757 dest = path.loc
1758 else:
1758 else:
1759 dest = path.pushloc or path.loc
1759 dest = path.pushloc or path.loc
1760 ui.statusnoi18n(b'analysing phase of %s\n' % util.hidepassword(dest))
1760 ui.statusnoi18n(b'analysing phase of %s\n' % util.hidepassword(dest))
1761 other = hg.peer(repo, opts, dest)
1761 other = hg.peer(repo, opts, dest)
1762
1762
1763 # easier to perform discovery through the operation
1763 # easier to perform discovery through the operation
1764 op = exchange.pushoperation(repo, other)
1764 op = exchange.pushoperation(repo, other)
1765 exchange._pushdiscoverychangeset(op)
1765 exchange._pushdiscoverychangeset(op)
1766
1766
1767 remotesubset = op.fallbackheads
1767 remotesubset = op.fallbackheads
1768
1768
1769 with other.commandexecutor() as e:
1769 with other.commandexecutor() as e:
1770 remotephases = e.callcommand(
1770 remotephases = e.callcommand(
1771 b'listkeys', {b'namespace': b'phases'}
1771 b'listkeys', {b'namespace': b'phases'}
1772 ).result()
1772 ).result()
1773 del other
1773 del other
1774 publishing = remotephases.get(b'publishing', False)
1774 publishing = remotephases.get(b'publishing', False)
1775 if publishing:
1775 if publishing:
1776 ui.statusnoi18n(b'publishing: yes\n')
1776 ui.statusnoi18n(b'publishing: yes\n')
1777 else:
1777 else:
1778 ui.statusnoi18n(b'publishing: no\n')
1778 ui.statusnoi18n(b'publishing: no\n')
1779
1779
1780 has_node = getattr(repo.changelog.index, 'has_node', None)
1780 has_node = getattr(repo.changelog.index, 'has_node', None)
1781 if has_node is None:
1781 if has_node is None:
1782 has_node = repo.changelog.nodemap.__contains__
1782 has_node = repo.changelog.nodemap.__contains__
1783 nonpublishroots = 0
1783 nonpublishroots = 0
1784 for nhex, phase in remotephases.iteritems():
1784 for nhex, phase in remotephases.iteritems():
1785 if nhex == b'publishing': # ignore data related to publish option
1785 if nhex == b'publishing': # ignore data related to publish option
1786 continue
1786 continue
1787 node = bin(nhex)
1787 node = bin(nhex)
1788 if has_node(node) and int(phase):
1788 if has_node(node) and int(phase):
1789 nonpublishroots += 1
1789 nonpublishroots += 1
1790 ui.statusnoi18n(b'number of roots: %d\n' % len(remotephases))
1790 ui.statusnoi18n(b'number of roots: %d\n' % len(remotephases))
1791 ui.statusnoi18n(b'number of known non public roots: %d\n' % nonpublishroots)
1791 ui.statusnoi18n(b'number of known non public roots: %d\n' % nonpublishroots)
1792
1792
1793 def d():
1793 def d():
1794 phases.remotephasessummary(repo, remotesubset, remotephases)
1794 phases.remotephasessummary(repo, remotesubset, remotephases)
1795
1795
1796 timer(d)
1796 timer(d)
1797 fm.end()
1797 fm.end()
1798
1798
1799
1799
1800 @command(
1800 @command(
1801 b'perf::manifest|perfmanifest',
1801 b'perf::manifest|perfmanifest',
1802 [
1802 [
1803 (b'm', b'manifest-rev', False, b'Look up a manifest node revision'),
1803 (b'm', b'manifest-rev', False, b'Look up a manifest node revision'),
1804 (b'', b'clear-disk', False, b'clear on-disk caches too'),
1804 (b'', b'clear-disk', False, b'clear on-disk caches too'),
1805 ]
1805 ]
1806 + formatteropts,
1806 + formatteropts,
1807 b'REV|NODE',
1807 b'REV|NODE',
1808 )
1808 )
1809 def perfmanifest(ui, repo, rev, manifest_rev=False, clear_disk=False, **opts):
1809 def perfmanifest(ui, repo, rev, manifest_rev=False, clear_disk=False, **opts):
1810 """benchmark the time to read a manifest from disk and return a usable
1810 """benchmark the time to read a manifest from disk and return a usable
1811 dict-like object
1811 dict-like object
1812
1812
1813 Manifest caches are cleared before retrieval."""
1813 Manifest caches are cleared before retrieval."""
1814 opts = _byteskwargs(opts)
1814 opts = _byteskwargs(opts)
1815 timer, fm = gettimer(ui, opts)
1815 timer, fm = gettimer(ui, opts)
1816 if not manifest_rev:
1816 if not manifest_rev:
1817 ctx = scmutil.revsingle(repo, rev, rev)
1817 ctx = scmutil.revsingle(repo, rev, rev)
1818 t = ctx.manifestnode()
1818 t = ctx.manifestnode()
1819 else:
1819 else:
1820 from mercurial.node import bin
1820 from mercurial.node import bin
1821
1821
1822 if len(rev) == 40:
1822 if len(rev) == 40:
1823 t = bin(rev)
1823 t = bin(rev)
1824 else:
1824 else:
1825 try:
1825 try:
1826 rev = int(rev)
1826 rev = int(rev)
1827
1827
1828 if util.safehasattr(repo.manifestlog, b'getstorage'):
1828 if util.safehasattr(repo.manifestlog, b'getstorage'):
1829 t = repo.manifestlog.getstorage(b'').node(rev)
1829 t = repo.manifestlog.getstorage(b'').node(rev)
1830 else:
1830 else:
1831 t = repo.manifestlog._revlog.lookup(rev)
1831 t = repo.manifestlog._revlog.lookup(rev)
1832 except ValueError:
1832 except ValueError:
1833 raise error.Abort(
1833 raise error.Abort(
1834 b'manifest revision must be integer or full node'
1834 b'manifest revision must be integer or full node'
1835 )
1835 )
1836
1836
1837 def d():
1837 def d():
1838 repo.manifestlog.clearcaches(clear_persisted_data=clear_disk)
1838 repo.manifestlog.clearcaches(clear_persisted_data=clear_disk)
1839 repo.manifestlog[t].read()
1839 repo.manifestlog[t].read()
1840
1840
1841 timer(d)
1841 timer(d)
1842 fm.end()
1842 fm.end()
1843
1843
1844
1844
1845 @command(b'perf::changeset|perfchangeset', formatteropts)
1845 @command(b'perf::changeset|perfchangeset', formatteropts)
1846 def perfchangeset(ui, repo, rev, **opts):
1846 def perfchangeset(ui, repo, rev, **opts):
1847 opts = _byteskwargs(opts)
1847 opts = _byteskwargs(opts)
1848 timer, fm = gettimer(ui, opts)
1848 timer, fm = gettimer(ui, opts)
1849 n = scmutil.revsingle(repo, rev).node()
1849 n = scmutil.revsingle(repo, rev).node()
1850
1850
1851 def d():
1851 def d():
1852 repo.changelog.read(n)
1852 repo.changelog.read(n)
1853 # repo.changelog._cache = None
1853 # repo.changelog._cache = None
1854
1854
1855 timer(d)
1855 timer(d)
1856 fm.end()
1856 fm.end()
1857
1857
1858
1858
1859 @command(b'perf::ignore|perfignore', formatteropts)
1859 @command(b'perf::ignore|perfignore', formatteropts)
1860 def perfignore(ui, repo, **opts):
1860 def perfignore(ui, repo, **opts):
1861 """benchmark operation related to computing ignore"""
1861 """benchmark operation related to computing ignore"""
1862 opts = _byteskwargs(opts)
1862 opts = _byteskwargs(opts)
1863 timer, fm = gettimer(ui, opts)
1863 timer, fm = gettimer(ui, opts)
1864 dirstate = repo.dirstate
1864 dirstate = repo.dirstate
1865
1865
1866 def setupone():
1866 def setupone():
1867 dirstate.invalidate()
1867 dirstate.invalidate()
1868 clearfilecache(dirstate, b'_ignore')
1868 clearfilecache(dirstate, b'_ignore')
1869
1869
1870 def runone():
1870 def runone():
1871 dirstate._ignore
1871 dirstate._ignore
1872
1872
1873 timer(runone, setup=setupone, title=b"load")
1873 timer(runone, setup=setupone, title=b"load")
1874 fm.end()
1874 fm.end()
1875
1875
1876
1876
1877 @command(
1877 @command(
1878 b'perf::index|perfindex',
1878 b'perf::index|perfindex',
1879 [
1879 [
1880 (b'', b'rev', [], b'revision to be looked up (default tip)'),
1880 (b'', b'rev', [], b'revision to be looked up (default tip)'),
1881 (b'', b'no-lookup', None, b'do not revision lookup post creation'),
1881 (b'', b'no-lookup', None, b'do not revision lookup post creation'),
1882 ]
1882 ]
1883 + formatteropts,
1883 + formatteropts,
1884 )
1884 )
1885 def perfindex(ui, repo, **opts):
1885 def perfindex(ui, repo, **opts):
1886 """benchmark index creation time followed by a lookup
1886 """benchmark index creation time followed by a lookup
1887
1887
1888 The default is to look `tip` up. Depending on the index implementation,
1888 The default is to look `tip` up. Depending on the index implementation,
1889 the revision looked up can matters. For example, an implementation
1889 the revision looked up can matters. For example, an implementation
1890 scanning the index will have a faster lookup time for `--rev tip` than for
1890 scanning the index will have a faster lookup time for `--rev tip` than for
1891 `--rev 0`. The number of looked up revisions and their order can also
1891 `--rev 0`. The number of looked up revisions and their order can also
1892 matters.
1892 matters.
1893
1893
1894 Example of useful set to test:
1894 Example of useful set to test:
1895
1895
1896 * tip
1896 * tip
1897 * 0
1897 * 0
1898 * -10:
1898 * -10:
1899 * :10
1899 * :10
1900 * -10: + :10
1900 * -10: + :10
1901 * :10: + -10:
1901 * :10: + -10:
1902 * -10000:
1902 * -10000:
1903 * -10000: + 0
1903 * -10000: + 0
1904
1904
1905 It is not currently possible to check for lookup of a missing node. For
1905 It is not currently possible to check for lookup of a missing node. For
1906 deeper lookup benchmarking, checkout the `perfnodemap` command."""
1906 deeper lookup benchmarking, checkout the `perfnodemap` command."""
1907 import mercurial.revlog
1907 import mercurial.revlog
1908
1908
1909 opts = _byteskwargs(opts)
1909 opts = _byteskwargs(opts)
1910 timer, fm = gettimer(ui, opts)
1910 timer, fm = gettimer(ui, opts)
1911 mercurial.revlog._prereadsize = 2 ** 24 # disable lazy parser in old hg
1911 mercurial.revlog._prereadsize = 2 ** 24 # disable lazy parser in old hg
1912 if opts[b'no_lookup']:
1912 if opts[b'no_lookup']:
1913 if opts['rev']:
1913 if opts['rev']:
1914 raise error.Abort('--no-lookup and --rev are mutually exclusive')
1914 raise error.Abort('--no-lookup and --rev are mutually exclusive')
1915 nodes = []
1915 nodes = []
1916 elif not opts[b'rev']:
1916 elif not opts[b'rev']:
1917 nodes = [repo[b"tip"].node()]
1917 nodes = [repo[b"tip"].node()]
1918 else:
1918 else:
1919 revs = scmutil.revrange(repo, opts[b'rev'])
1919 revs = scmutil.revrange(repo, opts[b'rev'])
1920 cl = repo.changelog
1920 cl = repo.changelog
1921 nodes = [cl.node(r) for r in revs]
1921 nodes = [cl.node(r) for r in revs]
1922
1922
1923 unfi = repo.unfiltered()
1923 unfi = repo.unfiltered()
1924 # find the filecache func directly
1924 # find the filecache func directly
1925 # This avoid polluting the benchmark with the filecache logic
1925 # This avoid polluting the benchmark with the filecache logic
1926 makecl = unfi.__class__.changelog.func
1926 makecl = unfi.__class__.changelog.func
1927
1927
1928 def setup():
1928 def setup():
1929 # probably not necessary, but for good measure
1929 # probably not necessary, but for good measure
1930 clearchangelog(unfi)
1930 clearchangelog(unfi)
1931
1931
1932 def d():
1932 def d():
1933 cl = makecl(unfi)
1933 cl = makecl(unfi)
1934 for n in nodes:
1934 for n in nodes:
1935 cl.rev(n)
1935 cl.rev(n)
1936
1936
1937 timer(d, setup=setup)
1937 timer(d, setup=setup)
1938 fm.end()
1938 fm.end()
1939
1939
1940
1940
1941 @command(
1941 @command(
1942 b'perf::nodemap|perfnodemap',
1942 b'perf::nodemap|perfnodemap',
1943 [
1943 [
1944 (b'', b'rev', [], b'revision to be looked up (default tip)'),
1944 (b'', b'rev', [], b'revision to be looked up (default tip)'),
1945 (b'', b'clear-caches', True, b'clear revlog cache between calls'),
1945 (b'', b'clear-caches', True, b'clear revlog cache between calls'),
1946 ]
1946 ]
1947 + formatteropts,
1947 + formatteropts,
1948 )
1948 )
1949 def perfnodemap(ui, repo, **opts):
1949 def perfnodemap(ui, repo, **opts):
1950 """benchmark the time necessary to look up revision from a cold nodemap
1950 """benchmark the time necessary to look up revision from a cold nodemap
1951
1951
1952 Depending on the implementation, the amount and order of revision we look
1952 Depending on the implementation, the amount and order of revision we look
1953 up can varies. Example of useful set to test:
1953 up can varies. Example of useful set to test:
1954 * tip
1954 * tip
1955 * 0
1955 * 0
1956 * -10:
1956 * -10:
1957 * :10
1957 * :10
1958 * -10: + :10
1958 * -10: + :10
1959 * :10: + -10:
1959 * :10: + -10:
1960 * -10000:
1960 * -10000:
1961 * -10000: + 0
1961 * -10000: + 0
1962
1962
1963 The command currently focus on valid binary lookup. Benchmarking for
1963 The command currently focus on valid binary lookup. Benchmarking for
1964 hexlookup, prefix lookup and missing lookup would also be valuable.
1964 hexlookup, prefix lookup and missing lookup would also be valuable.
1965 """
1965 """
1966 import mercurial.revlog
1966 import mercurial.revlog
1967
1967
1968 opts = _byteskwargs(opts)
1968 opts = _byteskwargs(opts)
1969 timer, fm = gettimer(ui, opts)
1969 timer, fm = gettimer(ui, opts)
1970 mercurial.revlog._prereadsize = 2 ** 24 # disable lazy parser in old hg
1970 mercurial.revlog._prereadsize = 2 ** 24 # disable lazy parser in old hg
1971
1971
1972 unfi = repo.unfiltered()
1972 unfi = repo.unfiltered()
1973 clearcaches = opts[b'clear_caches']
1973 clearcaches = opts[b'clear_caches']
1974 # find the filecache func directly
1974 # find the filecache func directly
1975 # This avoid polluting the benchmark with the filecache logic
1975 # This avoid polluting the benchmark with the filecache logic
1976 makecl = unfi.__class__.changelog.func
1976 makecl = unfi.__class__.changelog.func
1977 if not opts[b'rev']:
1977 if not opts[b'rev']:
1978 raise error.Abort(b'use --rev to specify revisions to look up')
1978 raise error.Abort(b'use --rev to specify revisions to look up')
1979 revs = scmutil.revrange(repo, opts[b'rev'])
1979 revs = scmutil.revrange(repo, opts[b'rev'])
1980 cl = repo.changelog
1980 cl = repo.changelog
1981 nodes = [cl.node(r) for r in revs]
1981 nodes = [cl.node(r) for r in revs]
1982
1982
1983 # use a list to pass reference to a nodemap from one closure to the next
1983 # use a list to pass reference to a nodemap from one closure to the next
1984 nodeget = [None]
1984 nodeget = [None]
1985
1985
1986 def setnodeget():
1986 def setnodeget():
1987 # probably not necessary, but for good measure
1987 # probably not necessary, but for good measure
1988 clearchangelog(unfi)
1988 clearchangelog(unfi)
1989 cl = makecl(unfi)
1989 cl = makecl(unfi)
1990 if util.safehasattr(cl.index, 'get_rev'):
1990 if util.safehasattr(cl.index, 'get_rev'):
1991 nodeget[0] = cl.index.get_rev
1991 nodeget[0] = cl.index.get_rev
1992 else:
1992 else:
1993 nodeget[0] = cl.nodemap.get
1993 nodeget[0] = cl.nodemap.get
1994
1994
1995 def d():
1995 def d():
1996 get = nodeget[0]
1996 get = nodeget[0]
1997 for n in nodes:
1997 for n in nodes:
1998 get(n)
1998 get(n)
1999
1999
2000 setup = None
2000 setup = None
2001 if clearcaches:
2001 if clearcaches:
2002
2002
2003 def setup():
2003 def setup():
2004 setnodeget()
2004 setnodeget()
2005
2005
2006 else:
2006 else:
2007 setnodeget()
2007 setnodeget()
2008 d() # prewarm the data structure
2008 d() # prewarm the data structure
2009 timer(d, setup=setup)
2009 timer(d, setup=setup)
2010 fm.end()
2010 fm.end()
2011
2011
2012
2012
2013 @command(b'perf::startup|perfstartup', formatteropts)
2013 @command(b'perf::startup|perfstartup', formatteropts)
2014 def perfstartup(ui, repo, **opts):
2014 def perfstartup(ui, repo, **opts):
2015 opts = _byteskwargs(opts)
2015 opts = _byteskwargs(opts)
2016 timer, fm = gettimer(ui, opts)
2016 timer, fm = gettimer(ui, opts)
2017
2017
2018 def d():
2018 def d():
2019 if os.name != 'nt':
2019 if os.name != 'nt':
2020 os.system(
2020 os.system(
2021 b"HGRCPATH= %s version -q > /dev/null" % fsencode(sys.argv[0])
2021 b"HGRCPATH= %s version -q > /dev/null" % fsencode(sys.argv[0])
2022 )
2022 )
2023 else:
2023 else:
2024 os.environ['HGRCPATH'] = r' '
2024 os.environ['HGRCPATH'] = r' '
2025 os.system("%s version -q > NUL" % sys.argv[0])
2025 os.system("%s version -q > NUL" % sys.argv[0])
2026
2026
2027 timer(d)
2027 timer(d)
2028 fm.end()
2028 fm.end()
2029
2029
2030
2030
2031 def _find_stream_generator(version):
2031 def _find_stream_generator(version):
2032 """find the proper generator function for this stream version"""
2032 """find the proper generator function for this stream version"""
2033 import mercurial.streamclone
2033 import mercurial.streamclone
2034
2034
2035 available = {}
2035 available = {}
2036
2036
2037 # try to fetch a v1 generator
2037 # try to fetch a v1 generator
2038 generatev1 = getattr(mercurial.streamclone, "generatev1", None)
2038 generatev1 = getattr(mercurial.streamclone, "generatev1", None)
2039 if generatev1 is not None:
2039 if generatev1 is not None:
2040
2040
2041 def generate(repo):
2041 def generate(repo):
2042 entries, bytes, data = generatev2(repo, None, None, True)
2042 entries, bytes, data = generatev2(repo, None, None, True)
2043 return data
2043 return data
2044
2044
2045 available[b'v1'] = generatev1
2045 available[b'v1'] = generatev1
2046 # try to fetch a v2 generator
2046 # try to fetch a v2 generator
2047 generatev2 = getattr(mercurial.streamclone, "generatev2", None)
2047 generatev2 = getattr(mercurial.streamclone, "generatev2", None)
2048 if generatev2 is not None:
2048 if generatev2 is not None:
2049
2049
2050 def generate(repo):
2050 def generate(repo):
2051 entries, bytes, data = generatev2(repo, None, None, True)
2051 entries, bytes, data = generatev2(repo, None, None, True)
2052 return data
2052 return data
2053
2053
2054 available[b'v2'] = generate
2054 available[b'v2'] = generate
2055 # try to fetch a v3 generator
2055 # try to fetch a v3 generator
2056 generatev3 = getattr(mercurial.streamclone, "generatev3", None)
2056 generatev3 = getattr(mercurial.streamclone, "generatev3", None)
2057 if generatev3 is not None:
2057 if generatev3 is not None:
2058
2058
2059 def generate(repo):
2059 def generate(repo):
2060 entries, bytes, data = generatev3(repo, None, None, True)
2060 entries, bytes, data = generatev3(repo, None, None, True)
2061 return data
2061 return data
2062
2062
2063 available[b'v3-exp'] = generate
2063 available[b'v3-exp'] = generate
2064
2064
2065 # resolve the request
2065 # resolve the request
2066 if version == b"latest":
2066 if version == b"latest":
2067 # latest is the highest non experimental version
2067 # latest is the highest non experimental version
2068 latest_key = max(v for v in available if b'-exp' not in v)
2068 latest_key = max(v for v in available if b'-exp' not in v)
2069 return available[latest_key]
2069 return available[latest_key]
2070 elif version in available:
2070 elif version in available:
2071 return available[version]
2071 return available[version]
2072 else:
2072 else:
2073 msg = b"unkown or unavailable version: %s"
2073 msg = b"unkown or unavailable version: %s"
2074 msg %= version
2074 msg %= version
2075 hint = b"available versions: %s"
2075 hint = b"available versions: %s"
2076 hint %= b', '.join(sorted(available))
2076 hint %= b', '.join(sorted(available))
2077 raise error.Abort(msg, hint=hint)
2077 raise error.Abort(msg, hint=hint)
2078
2078
2079
2079
2080 @command(
2080 @command(
2081 b'perf::stream-locked-section',
2081 b'perf::stream-locked-section',
2082 [
2082 [
2083 (
2083 (
2084 b'',
2084 b'',
2085 b'stream-version',
2085 b'stream-version',
2086 b'latest',
2086 b'latest',
2087 b'stream version to use ("v1", "v2", "v3" or "latest", (the default))',
2087 b'stream version to use ("v1", "v2", "v3" or "latest", (the default))',
2088 ),
2088 ),
2089 ]
2089 ]
2090 + formatteropts,
2090 + formatteropts,
2091 )
2091 )
2092 def perf_stream_clone_scan(ui, repo, stream_version, **opts):
2092 def perf_stream_clone_scan(ui, repo, stream_version, **opts):
2093 """benchmark the initial, repo-locked, section of a stream-clone"""
2093 """benchmark the initial, repo-locked, section of a stream-clone"""
2094
2094
2095 opts = _byteskwargs(opts)
2095 opts = _byteskwargs(opts)
2096 timer, fm = gettimer(ui, opts)
2096 timer, fm = gettimer(ui, opts)
2097
2097
2098 # deletion of the generator may trigger some cleanup that we do not want to
2098 # deletion of the generator may trigger some cleanup that we do not want to
2099 # measure
2099 # measure
2100 result_holder = [None]
2100 result_holder = [None]
2101
2101
2102 def setupone():
2102 def setupone():
2103 result_holder[0] = None
2103 result_holder[0] = None
2104
2104
2105 generate = _find_stream_generator(stream_version)
2105 generate = _find_stream_generator(stream_version)
2106
2106
2107 def runone():
2107 def runone():
2108 # the lock is held for the duration the initialisation
2108 # the lock is held for the duration the initialisation
2109 result_holder[0] = generate(repo)
2109 result_holder[0] = generate(repo)
2110
2110
2111 timer(runone, setup=setupone, title=b"load")
2111 timer(runone, setup=setupone, title=b"load")
2112 fm.end()
2112 fm.end()
2113
2113
2114
2114
2115 @command(
2115 @command(
2116 b'perf::stream-generate',
2116 b'perf::stream-generate',
2117 [
2117 [
2118 (
2118 (
2119 b'',
2119 b'',
2120 b'stream-version',
2120 b'stream-version',
2121 b'latest',
2121 b'latest',
2122 b'stream version to us ("v1", "v2" or "latest", (the default))',
2122 b'stream version to us ("v1", "v2" or "latest", (the default))',
2123 ),
2123 ),
2124 ]
2124 ]
2125 + formatteropts,
2125 + formatteropts,
2126 )
2126 )
2127 def perf_stream_clone_generate(ui, repo, stream_version, **opts):
2127 def perf_stream_clone_generate(ui, repo, stream_version, **opts):
2128 """benchmark the full generation of a stream clone"""
2128 """benchmark the full generation of a stream clone"""
2129
2129
2130 opts = _byteskwargs(opts)
2130 opts = _byteskwargs(opts)
2131 timer, fm = gettimer(ui, opts)
2131 timer, fm = gettimer(ui, opts)
2132
2132
2133 # deletion of the generator may trigger some cleanup that we do not want to
2133 # deletion of the generator may trigger some cleanup that we do not want to
2134 # measure
2134 # measure
2135
2135
2136 generate = _find_stream_generator(stream_version)
2136 generate = _find_stream_generator(stream_version)
2137
2137
2138 def runone():
2138 def runone():
2139 # the lock is held for the duration the initialisation
2139 # the lock is held for the duration the initialisation
2140 for chunk in generate(repo):
2140 for chunk in generate(repo):
2141 pass
2141 pass
2142
2142
2143 timer(runone, title=b"generate")
2143 timer(runone, title=b"generate")
2144 fm.end()
2144 fm.end()
2145
2145
2146
2146
2147 @command(
2147 @command(
2148 b'perf::stream-consume',
2148 b'perf::stream-consume',
2149 formatteropts,
2149 formatteropts,
2150 )
2150 )
2151 def perf_stream_clone_consume(ui, repo, filename, **opts):
2151 def perf_stream_clone_consume(ui, repo, filename, **opts):
2152 """benchmark the full application of a stream clone
2152 """benchmark the full application of a stream clone
2153
2153
2154 This include the creation of the repository
2154 This include the creation of the repository
2155 """
2155 """
2156 # try except to appease check code
2156 # try except to appease check code
2157 msg = b"mercurial too old, missing necessary module: %s"
2157 msg = b"mercurial too old, missing necessary module: %s"
2158 try:
2158 try:
2159 from mercurial import bundle2
2159 from mercurial import bundle2
2160 except ImportError as exc:
2160 except ImportError as exc:
2161 msg %= _bytestr(exc)
2161 msg %= _bytestr(exc)
2162 raise error.Abort(msg)
2162 raise error.Abort(msg)
2163 try:
2163 try:
2164 from mercurial import exchange
2164 from mercurial import exchange
2165 except ImportError as exc:
2165 except ImportError as exc:
2166 msg %= _bytestr(exc)
2166 msg %= _bytestr(exc)
2167 raise error.Abort(msg)
2167 raise error.Abort(msg)
2168 try:
2168 try:
2169 from mercurial import hg
2169 from mercurial import hg
2170 except ImportError as exc:
2170 except ImportError as exc:
2171 msg %= _bytestr(exc)
2171 msg %= _bytestr(exc)
2172 raise error.Abort(msg)
2172 raise error.Abort(msg)
2173 try:
2173 try:
2174 from mercurial import localrepo
2174 from mercurial import localrepo
2175 except ImportError as exc:
2175 except ImportError as exc:
2176 msg %= _bytestr(exc)
2176 msg %= _bytestr(exc)
2177 raise error.Abort(msg)
2177 raise error.Abort(msg)
2178
2178
2179 opts = _byteskwargs(opts)
2179 opts = _byteskwargs(opts)
2180 timer, fm = gettimer(ui, opts)
2180 timer, fm = gettimer(ui, opts)
2181
2181
2182 # deletion of the generator may trigger some cleanup that we do not want to
2182 # deletion of the generator may trigger some cleanup that we do not want to
2183 # measure
2183 # measure
2184 if not (os.path.isfile(filename) and os.access(filename, os.R_OK)):
2184 if not (os.path.isfile(filename) and os.access(filename, os.R_OK)):
2185 raise error.Abort("not a readable file: %s" % filename)
2185 raise error.Abort("not a readable file: %s" % filename)
2186
2186
2187 run_variables = [None, None]
2187 run_variables = [None, None]
2188
2188
2189 @contextlib.contextmanager
2189 @contextlib.contextmanager
2190 def context():
2190 def context():
2191 with open(filename, mode='rb') as bundle:
2191 with open(filename, mode='rb') as bundle:
2192 with tempfile.TemporaryDirectory() as tmp_dir:
2192 with tempfile.TemporaryDirectory() as tmp_dir:
2193 tmp_dir = fsencode(tmp_dir)
2193 tmp_dir = fsencode(tmp_dir)
2194 run_variables[0] = bundle
2194 run_variables[0] = bundle
2195 run_variables[1] = tmp_dir
2195 run_variables[1] = tmp_dir
2196 yield
2196 yield
2197 run_variables[0] = None
2197 run_variables[0] = None
2198 run_variables[1] = None
2198 run_variables[1] = None
2199
2199
2200 def runone():
2200 def runone():
2201 bundle = run_variables[0]
2201 bundle = run_variables[0]
2202 tmp_dir = run_variables[1]
2202 tmp_dir = run_variables[1]
2203 # only pass ui when no srcrepo
2203 # only pass ui when no srcrepo
2204 localrepo.createrepository(
2204 localrepo.createrepository(
2205 repo.ui, tmp_dir, requirements=repo.requirements
2205 repo.ui, tmp_dir, requirements=repo.requirements
2206 )
2206 )
2207 target = hg.repository(repo.ui, tmp_dir)
2207 target = hg.repository(repo.ui, tmp_dir)
2208 gen = exchange.readbundle(target.ui, bundle, bundle.name)
2208 gen = exchange.readbundle(target.ui, bundle, bundle.name)
2209 # stream v1
2209 # stream v1
2210 if util.safehasattr(gen, 'apply'):
2210 if util.safehasattr(gen, 'apply'):
2211 gen.apply(target)
2211 gen.apply(target)
2212 else:
2212 else:
2213 with target.transaction(b"perf::stream-consume") as tr:
2213 with target.transaction(b"perf::stream-consume") as tr:
2214 bundle2.applybundle(
2214 bundle2.applybundle(
2215 target,
2215 target,
2216 gen,
2216 gen,
2217 tr,
2217 tr,
2218 source=b'unbundle',
2218 source=b'unbundle',
2219 url=filename,
2219 url=filename,
2220 )
2220 )
2221
2221
2222 timer(runone, context=context, title=b"consume")
2222 timer(runone, context=context, title=b"consume")
2223 fm.end()
2223 fm.end()
2224
2224
2225
2225
2226 @command(b'perf::parents|perfparents', formatteropts)
2226 @command(b'perf::parents|perfparents', formatteropts)
2227 def perfparents(ui, repo, **opts):
2227 def perfparents(ui, repo, **opts):
2228 """benchmark the time necessary to fetch one changeset's parents.
2228 """benchmark the time necessary to fetch one changeset's parents.
2229
2229
2230 The fetch is done using the `node identifier`, traversing all object layers
2230 The fetch is done using the `node identifier`, traversing all object layers
2231 from the repository object. The first N revisions will be used for this
2231 from the repository object. The first N revisions will be used for this
2232 benchmark. N is controlled by the ``perf.parentscount`` config option
2232 benchmark. N is controlled by the ``perf.parentscount`` config option
2233 (default: 1000).
2233 (default: 1000).
2234 """
2234 """
2235 opts = _byteskwargs(opts)
2235 opts = _byteskwargs(opts)
2236 timer, fm = gettimer(ui, opts)
2236 timer, fm = gettimer(ui, opts)
2237 # control the number of commits perfparents iterates over
2237 # control the number of commits perfparents iterates over
2238 # experimental config: perf.parentscount
2238 # experimental config: perf.parentscount
2239 count = getint(ui, b"perf", b"parentscount", 1000)
2239 count = getint(ui, b"perf", b"parentscount", 1000)
2240 if len(repo.changelog) < count:
2240 if len(repo.changelog) < count:
2241 raise error.Abort(b"repo needs %d commits for this test" % count)
2241 raise error.Abort(b"repo needs %d commits for this test" % count)
2242 repo = repo.unfiltered()
2242 repo = repo.unfiltered()
2243 nl = [repo.changelog.node(i) for i in _xrange(count)]
2243 nl = [repo.changelog.node(i) for i in _xrange(count)]
2244
2244
2245 def d():
2245 def d():
2246 for n in nl:
2246 for n in nl:
2247 repo.changelog.parents(n)
2247 repo.changelog.parents(n)
2248
2248
2249 timer(d)
2249 timer(d)
2250 fm.end()
2250 fm.end()
2251
2251
2252
2252
2253 @command(b'perf::ctxfiles|perfctxfiles', formatteropts)
2253 @command(b'perf::ctxfiles|perfctxfiles', formatteropts)
2254 def perfctxfiles(ui, repo, x, **opts):
2254 def perfctxfiles(ui, repo, x, **opts):
2255 opts = _byteskwargs(opts)
2255 opts = _byteskwargs(opts)
2256 x = int(x)
2256 x = int(x)
2257 timer, fm = gettimer(ui, opts)
2257 timer, fm = gettimer(ui, opts)
2258
2258
2259 def d():
2259 def d():
2260 len(repo[x].files())
2260 len(repo[x].files())
2261
2261
2262 timer(d)
2262 timer(d)
2263 fm.end()
2263 fm.end()
2264
2264
2265
2265
2266 @command(b'perf::rawfiles|perfrawfiles', formatteropts)
2266 @command(b'perf::rawfiles|perfrawfiles', formatteropts)
2267 def perfrawfiles(ui, repo, x, **opts):
2267 def perfrawfiles(ui, repo, x, **opts):
2268 opts = _byteskwargs(opts)
2268 opts = _byteskwargs(opts)
2269 x = int(x)
2269 x = int(x)
2270 timer, fm = gettimer(ui, opts)
2270 timer, fm = gettimer(ui, opts)
2271 cl = repo.changelog
2271 cl = repo.changelog
2272
2272
2273 def d():
2273 def d():
2274 len(cl.read(x)[3])
2274 len(cl.read(x)[3])
2275
2275
2276 timer(d)
2276 timer(d)
2277 fm.end()
2277 fm.end()
2278
2278
2279
2279
2280 @command(b'perf::lookup|perflookup', formatteropts)
2280 @command(b'perf::lookup|perflookup', formatteropts)
2281 def perflookup(ui, repo, rev, **opts):
2281 def perflookup(ui, repo, rev, **opts):
2282 opts = _byteskwargs(opts)
2282 opts = _byteskwargs(opts)
2283 timer, fm = gettimer(ui, opts)
2283 timer, fm = gettimer(ui, opts)
2284 timer(lambda: len(repo.lookup(rev)))
2284 timer(lambda: len(repo.lookup(rev)))
2285 fm.end()
2285 fm.end()
2286
2286
2287
2287
2288 @command(
2288 @command(
2289 b'perf::linelogedits|perflinelogedits',
2289 b'perf::linelogedits|perflinelogedits',
2290 [
2290 [
2291 (b'n', b'edits', 10000, b'number of edits'),
2291 (b'n', b'edits', 10000, b'number of edits'),
2292 (b'', b'max-hunk-lines', 10, b'max lines in a hunk'),
2292 (b'', b'max-hunk-lines', 10, b'max lines in a hunk'),
2293 ],
2293 ],
2294 norepo=True,
2294 norepo=True,
2295 )
2295 )
2296 def perflinelogedits(ui, **opts):
2296 def perflinelogedits(ui, **opts):
2297 from mercurial import linelog
2297 from mercurial import linelog
2298
2298
2299 opts = _byteskwargs(opts)
2299 opts = _byteskwargs(opts)
2300
2300
2301 edits = opts[b'edits']
2301 edits = opts[b'edits']
2302 maxhunklines = opts[b'max_hunk_lines']
2302 maxhunklines = opts[b'max_hunk_lines']
2303
2303
2304 maxb1 = 100000
2304 maxb1 = 100000
2305 random.seed(0)
2305 random.seed(0)
2306 randint = random.randint
2306 randint = random.randint
2307 currentlines = 0
2307 currentlines = 0
2308 arglist = []
2308 arglist = []
2309 for rev in _xrange(edits):
2309 for rev in _xrange(edits):
2310 a1 = randint(0, currentlines)
2310 a1 = randint(0, currentlines)
2311 a2 = randint(a1, min(currentlines, a1 + maxhunklines))
2311 a2 = randint(a1, min(currentlines, a1 + maxhunklines))
2312 b1 = randint(0, maxb1)
2312 b1 = randint(0, maxb1)
2313 b2 = randint(b1, b1 + maxhunklines)
2313 b2 = randint(b1, b1 + maxhunklines)
2314 currentlines += (b2 - b1) - (a2 - a1)
2314 currentlines += (b2 - b1) - (a2 - a1)
2315 arglist.append((rev, a1, a2, b1, b2))
2315 arglist.append((rev, a1, a2, b1, b2))
2316
2316
2317 def d():
2317 def d():
2318 ll = linelog.linelog()
2318 ll = linelog.linelog()
2319 for args in arglist:
2319 for args in arglist:
2320 ll.replacelines(*args)
2320 ll.replacelines(*args)
2321
2321
2322 timer, fm = gettimer(ui, opts)
2322 timer, fm = gettimer(ui, opts)
2323 timer(d)
2323 timer(d)
2324 fm.end()
2324 fm.end()
2325
2325
2326
2326
2327 @command(b'perf::revrange|perfrevrange', formatteropts)
2327 @command(b'perf::revrange|perfrevrange', formatteropts)
2328 def perfrevrange(ui, repo, *specs, **opts):
2328 def perfrevrange(ui, repo, *specs, **opts):
2329 opts = _byteskwargs(opts)
2329 opts = _byteskwargs(opts)
2330 timer, fm = gettimer(ui, opts)
2330 timer, fm = gettimer(ui, opts)
2331 revrange = scmutil.revrange
2331 revrange = scmutil.revrange
2332 timer(lambda: len(revrange(repo, specs)))
2332 timer(lambda: len(revrange(repo, specs)))
2333 fm.end()
2333 fm.end()
2334
2334
2335
2335
2336 @command(b'perf::nodelookup|perfnodelookup', formatteropts)
2336 @command(b'perf::nodelookup|perfnodelookup', formatteropts)
2337 def perfnodelookup(ui, repo, rev, **opts):
2337 def perfnodelookup(ui, repo, rev, **opts):
2338 opts = _byteskwargs(opts)
2338 opts = _byteskwargs(opts)
2339 timer, fm = gettimer(ui, opts)
2339 timer, fm = gettimer(ui, opts)
2340 import mercurial.revlog
2340 import mercurial.revlog
2341
2341
2342 mercurial.revlog._prereadsize = 2 ** 24 # disable lazy parser in old hg
2342 mercurial.revlog._prereadsize = 2 ** 24 # disable lazy parser in old hg
2343 n = scmutil.revsingle(repo, rev).node()
2343 n = scmutil.revsingle(repo, rev).node()
2344
2344
2345 try:
2345 try:
2346 cl = revlog(getsvfs(repo), radix=b"00changelog")
2346 cl = revlog(getsvfs(repo), radix=b"00changelog")
2347 except TypeError:
2347 except TypeError:
2348 cl = revlog(getsvfs(repo), indexfile=b"00changelog.i")
2348 cl = revlog(getsvfs(repo), indexfile=b"00changelog.i")
2349
2349
2350 def d():
2350 def d():
2351 cl.rev(n)
2351 cl.rev(n)
2352 clearcaches(cl)
2352 clearcaches(cl)
2353
2353
2354 timer(d)
2354 timer(d)
2355 fm.end()
2355 fm.end()
2356
2356
2357
2357
2358 @command(
2358 @command(
2359 b'perf::log|perflog',
2359 b'perf::log|perflog',
2360 [(b'', b'rename', False, b'ask log to follow renames')] + formatteropts,
2360 [(b'', b'rename', False, b'ask log to follow renames')] + formatteropts,
2361 )
2361 )
2362 def perflog(ui, repo, rev=None, **opts):
2362 def perflog(ui, repo, rev=None, **opts):
2363 opts = _byteskwargs(opts)
2363 opts = _byteskwargs(opts)
2364 if rev is None:
2364 if rev is None:
2365 rev = []
2365 rev = []
2366 timer, fm = gettimer(ui, opts)
2366 timer, fm = gettimer(ui, opts)
2367 ui.pushbuffer()
2367 ui.pushbuffer()
2368 timer(
2368 timer(
2369 lambda: commands.log(
2369 lambda: commands.log(
2370 ui, repo, rev=rev, date=b'', user=b'', copies=opts.get(b'rename')
2370 ui, repo, rev=rev, date=b'', user=b'', copies=opts.get(b'rename')
2371 )
2371 )
2372 )
2372 )
2373 ui.popbuffer()
2373 ui.popbuffer()
2374 fm.end()
2374 fm.end()
2375
2375
2376
2376
2377 @command(b'perf::moonwalk|perfmoonwalk', formatteropts)
2377 @command(b'perf::moonwalk|perfmoonwalk', formatteropts)
2378 def perfmoonwalk(ui, repo, **opts):
2378 def perfmoonwalk(ui, repo, **opts):
2379 """benchmark walking the changelog backwards
2379 """benchmark walking the changelog backwards
2380
2380
2381 This also loads the changelog data for each revision in the changelog.
2381 This also loads the changelog data for each revision in the changelog.
2382 """
2382 """
2383 opts = _byteskwargs(opts)
2383 opts = _byteskwargs(opts)
2384 timer, fm = gettimer(ui, opts)
2384 timer, fm = gettimer(ui, opts)
2385
2385
2386 def moonwalk():
2386 def moonwalk():
2387 for i in repo.changelog.revs(start=(len(repo) - 1), stop=-1):
2387 for i in repo.changelog.revs(start=(len(repo) - 1), stop=-1):
2388 ctx = repo[i]
2388 ctx = repo[i]
2389 ctx.branch() # read changelog data (in addition to the index)
2389 ctx.branch() # read changelog data (in addition to the index)
2390
2390
2391 timer(moonwalk)
2391 timer(moonwalk)
2392 fm.end()
2392 fm.end()
2393
2393
2394
2394
2395 @command(
2395 @command(
2396 b'perf::templating|perftemplating',
2396 b'perf::templating|perftemplating',
2397 [
2397 [
2398 (b'r', b'rev', [], b'revisions to run the template on'),
2398 (b'r', b'rev', [], b'revisions to run the template on'),
2399 ]
2399 ]
2400 + formatteropts,
2400 + formatteropts,
2401 )
2401 )
2402 def perftemplating(ui, repo, testedtemplate=None, **opts):
2402 def perftemplating(ui, repo, testedtemplate=None, **opts):
2403 """test the rendering time of a given template"""
2403 """test the rendering time of a given template"""
2404 if makelogtemplater is None:
2404 if makelogtemplater is None:
2405 raise error.Abort(
2405 raise error.Abort(
2406 b"perftemplating not available with this Mercurial",
2406 b"perftemplating not available with this Mercurial",
2407 hint=b"use 4.3 or later",
2407 hint=b"use 4.3 or later",
2408 )
2408 )
2409
2409
2410 opts = _byteskwargs(opts)
2410 opts = _byteskwargs(opts)
2411
2411
2412 nullui = ui.copy()
2412 nullui = ui.copy()
2413 nullui.fout = open(os.devnull, 'wb')
2413 nullui.fout = open(os.devnull, 'wb')
2414 nullui.disablepager()
2414 nullui.disablepager()
2415 revs = opts.get(b'rev')
2415 revs = opts.get(b'rev')
2416 if not revs:
2416 if not revs:
2417 revs = [b'all()']
2417 revs = [b'all()']
2418 revs = list(scmutil.revrange(repo, revs))
2418 revs = list(scmutil.revrange(repo, revs))
2419
2419
2420 defaulttemplate = (
2420 defaulttemplate = (
2421 b'{date|shortdate} [{rev}:{node|short}]'
2421 b'{date|shortdate} [{rev}:{node|short}]'
2422 b' {author|person}: {desc|firstline}\n'
2422 b' {author|person}: {desc|firstline}\n'
2423 )
2423 )
2424 if testedtemplate is None:
2424 if testedtemplate is None:
2425 testedtemplate = defaulttemplate
2425 testedtemplate = defaulttemplate
2426 displayer = makelogtemplater(nullui, repo, testedtemplate)
2426 displayer = makelogtemplater(nullui, repo, testedtemplate)
2427
2427
2428 def format():
2428 def format():
2429 for r in revs:
2429 for r in revs:
2430 ctx = repo[r]
2430 ctx = repo[r]
2431 displayer.show(ctx)
2431 displayer.show(ctx)
2432 displayer.flush(ctx)
2432 displayer.flush(ctx)
2433
2433
2434 timer, fm = gettimer(ui, opts)
2434 timer, fm = gettimer(ui, opts)
2435 timer(format)
2435 timer(format)
2436 fm.end()
2436 fm.end()
2437
2437
2438
2438
2439 def _displaystats(ui, opts, entries, data):
2439 def _displaystats(ui, opts, entries, data):
2440 # use a second formatter because the data are quite different, not sure
2440 # use a second formatter because the data are quite different, not sure
2441 # how it flies with the templater.
2441 # how it flies with the templater.
2442 fm = ui.formatter(b'perf-stats', opts)
2442 fm = ui.formatter(b'perf-stats', opts)
2443 for key, title in entries:
2443 for key, title in entries:
2444 values = data[key]
2444 values = data[key]
2445 nbvalues = len(data)
2445 nbvalues = len(data)
2446 values.sort()
2446 values.sort()
2447 stats = {
2447 stats = {
2448 'key': key,
2448 'key': key,
2449 'title': title,
2449 'title': title,
2450 'nbitems': len(values),
2450 'nbitems': len(values),
2451 'min': values[0][0],
2451 'min': values[0][0],
2452 '10%': values[(nbvalues * 10) // 100][0],
2452 '10%': values[(nbvalues * 10) // 100][0],
2453 '25%': values[(nbvalues * 25) // 100][0],
2453 '25%': values[(nbvalues * 25) // 100][0],
2454 '50%': values[(nbvalues * 50) // 100][0],
2454 '50%': values[(nbvalues * 50) // 100][0],
2455 '75%': values[(nbvalues * 75) // 100][0],
2455 '75%': values[(nbvalues * 75) // 100][0],
2456 '80%': values[(nbvalues * 80) // 100][0],
2456 '80%': values[(nbvalues * 80) // 100][0],
2457 '85%': values[(nbvalues * 85) // 100][0],
2457 '85%': values[(nbvalues * 85) // 100][0],
2458 '90%': values[(nbvalues * 90) // 100][0],
2458 '90%': values[(nbvalues * 90) // 100][0],
2459 '95%': values[(nbvalues * 95) // 100][0],
2459 '95%': values[(nbvalues * 95) // 100][0],
2460 '99%': values[(nbvalues * 99) // 100][0],
2460 '99%': values[(nbvalues * 99) // 100][0],
2461 'max': values[-1][0],
2461 'max': values[-1][0],
2462 }
2462 }
2463 fm.startitem()
2463 fm.startitem()
2464 fm.data(**stats)
2464 fm.data(**stats)
2465 # make node pretty for the human output
2465 # make node pretty for the human output
2466 fm.plain('### %s (%d items)\n' % (title, len(values)))
2466 fm.plain('### %s (%d items)\n' % (title, len(values)))
2467 lines = [
2467 lines = [
2468 'min',
2468 'min',
2469 '10%',
2469 '10%',
2470 '25%',
2470 '25%',
2471 '50%',
2471 '50%',
2472 '75%',
2472 '75%',
2473 '80%',
2473 '80%',
2474 '85%',
2474 '85%',
2475 '90%',
2475 '90%',
2476 '95%',
2476 '95%',
2477 '99%',
2477 '99%',
2478 'max',
2478 'max',
2479 ]
2479 ]
2480 for l in lines:
2480 for l in lines:
2481 fm.plain('%s: %s\n' % (l, stats[l]))
2481 fm.plain('%s: %s\n' % (l, stats[l]))
2482 fm.end()
2482 fm.end()
2483
2483
2484
2484
2485 @command(
2485 @command(
2486 b'perf::helper-mergecopies|perfhelper-mergecopies',
2486 b'perf::helper-mergecopies|perfhelper-mergecopies',
2487 formatteropts
2487 formatteropts
2488 + [
2488 + [
2489 (b'r', b'revs', [], b'restrict search to these revisions'),
2489 (b'r', b'revs', [], b'restrict search to these revisions'),
2490 (b'', b'timing', False, b'provides extra data (costly)'),
2490 (b'', b'timing', False, b'provides extra data (costly)'),
2491 (b'', b'stats', False, b'provides statistic about the measured data'),
2491 (b'', b'stats', False, b'provides statistic about the measured data'),
2492 ],
2492 ],
2493 )
2493 )
2494 def perfhelpermergecopies(ui, repo, revs=[], **opts):
2494 def perfhelpermergecopies(ui, repo, revs=[], **opts):
2495 """find statistics about potential parameters for `perfmergecopies`
2495 """find statistics about potential parameters for `perfmergecopies`
2496
2496
2497 This command find (base, p1, p2) triplet relevant for copytracing
2497 This command find (base, p1, p2) triplet relevant for copytracing
2498 benchmarking in the context of a merge. It reports values for some of the
2498 benchmarking in the context of a merge. It reports values for some of the
2499 parameters that impact merge copy tracing time during merge.
2499 parameters that impact merge copy tracing time during merge.
2500
2500
2501 If `--timing` is set, rename detection is run and the associated timing
2501 If `--timing` is set, rename detection is run and the associated timing
2502 will be reported. The extra details come at the cost of slower command
2502 will be reported. The extra details come at the cost of slower command
2503 execution.
2503 execution.
2504
2504
2505 Since rename detection is only run once, other factors might easily
2505 Since rename detection is only run once, other factors might easily
2506 affect the precision of the timing. However it should give a good
2506 affect the precision of the timing. However it should give a good
2507 approximation of which revision triplets are very costly.
2507 approximation of which revision triplets are very costly.
2508 """
2508 """
2509 opts = _byteskwargs(opts)
2509 opts = _byteskwargs(opts)
2510 fm = ui.formatter(b'perf', opts)
2510 fm = ui.formatter(b'perf', opts)
2511 dotiming = opts[b'timing']
2511 dotiming = opts[b'timing']
2512 dostats = opts[b'stats']
2512 dostats = opts[b'stats']
2513
2513
2514 output_template = [
2514 output_template = [
2515 ("base", "%(base)12s"),
2515 ("base", "%(base)12s"),
2516 ("p1", "%(p1.node)12s"),
2516 ("p1", "%(p1.node)12s"),
2517 ("p2", "%(p2.node)12s"),
2517 ("p2", "%(p2.node)12s"),
2518 ("p1.nb-revs", "%(p1.nbrevs)12d"),
2518 ("p1.nb-revs", "%(p1.nbrevs)12d"),
2519 ("p1.nb-files", "%(p1.nbmissingfiles)12d"),
2519 ("p1.nb-files", "%(p1.nbmissingfiles)12d"),
2520 ("p1.renames", "%(p1.renamedfiles)12d"),
2520 ("p1.renames", "%(p1.renamedfiles)12d"),
2521 ("p1.time", "%(p1.time)12.3f"),
2521 ("p1.time", "%(p1.time)12.3f"),
2522 ("p2.nb-revs", "%(p2.nbrevs)12d"),
2522 ("p2.nb-revs", "%(p2.nbrevs)12d"),
2523 ("p2.nb-files", "%(p2.nbmissingfiles)12d"),
2523 ("p2.nb-files", "%(p2.nbmissingfiles)12d"),
2524 ("p2.renames", "%(p2.renamedfiles)12d"),
2524 ("p2.renames", "%(p2.renamedfiles)12d"),
2525 ("p2.time", "%(p2.time)12.3f"),
2525 ("p2.time", "%(p2.time)12.3f"),
2526 ("renames", "%(nbrenamedfiles)12d"),
2526 ("renames", "%(nbrenamedfiles)12d"),
2527 ("total.time", "%(time)12.3f"),
2527 ("total.time", "%(time)12.3f"),
2528 ]
2528 ]
2529 if not dotiming:
2529 if not dotiming:
2530 output_template = [
2530 output_template = [
2531 i
2531 i
2532 for i in output_template
2532 for i in output_template
2533 if not ('time' in i[0] or 'renames' in i[0])
2533 if not ('time' in i[0] or 'renames' in i[0])
2534 ]
2534 ]
2535 header_names = [h for (h, v) in output_template]
2535 header_names = [h for (h, v) in output_template]
2536 output = ' '.join([v for (h, v) in output_template]) + '\n'
2536 output = ' '.join([v for (h, v) in output_template]) + '\n'
2537 header = ' '.join(['%12s'] * len(header_names)) + '\n'
2537 header = ' '.join(['%12s'] * len(header_names)) + '\n'
2538 fm.plain(header % tuple(header_names))
2538 fm.plain(header % tuple(header_names))
2539
2539
2540 if not revs:
2540 if not revs:
2541 revs = ['all()']
2541 revs = ['all()']
2542 revs = scmutil.revrange(repo, revs)
2542 revs = scmutil.revrange(repo, revs)
2543
2543
2544 if dostats:
2544 if dostats:
2545 alldata = {
2545 alldata = {
2546 'nbrevs': [],
2546 'nbrevs': [],
2547 'nbmissingfiles': [],
2547 'nbmissingfiles': [],
2548 }
2548 }
2549 if dotiming:
2549 if dotiming:
2550 alldata['parentnbrenames'] = []
2550 alldata['parentnbrenames'] = []
2551 alldata['totalnbrenames'] = []
2551 alldata['totalnbrenames'] = []
2552 alldata['parenttime'] = []
2552 alldata['parenttime'] = []
2553 alldata['totaltime'] = []
2553 alldata['totaltime'] = []
2554
2554
2555 roi = repo.revs('merge() and %ld', revs)
2555 roi = repo.revs('merge() and %ld', revs)
2556 for r in roi:
2556 for r in roi:
2557 ctx = repo[r]
2557 ctx = repo[r]
2558 p1 = ctx.p1()
2558 p1 = ctx.p1()
2559 p2 = ctx.p2()
2559 p2 = ctx.p2()
2560 bases = repo.changelog._commonancestorsheads(p1.rev(), p2.rev())
2560 bases = repo.changelog._commonancestorsheads(p1.rev(), p2.rev())
2561 for b in bases:
2561 for b in bases:
2562 b = repo[b]
2562 b = repo[b]
2563 p1missing = copies._computeforwardmissing(b, p1)
2563 p1missing = copies._computeforwardmissing(b, p1)
2564 p2missing = copies._computeforwardmissing(b, p2)
2564 p2missing = copies._computeforwardmissing(b, p2)
2565 data = {
2565 data = {
2566 b'base': b.hex(),
2566 b'base': b.hex(),
2567 b'p1.node': p1.hex(),
2567 b'p1.node': p1.hex(),
2568 b'p1.nbrevs': len(repo.revs('only(%d, %d)', p1.rev(), b.rev())),
2568 b'p1.nbrevs': len(repo.revs('only(%d, %d)', p1.rev(), b.rev())),
2569 b'p1.nbmissingfiles': len(p1missing),
2569 b'p1.nbmissingfiles': len(p1missing),
2570 b'p2.node': p2.hex(),
2570 b'p2.node': p2.hex(),
2571 b'p2.nbrevs': len(repo.revs('only(%d, %d)', p2.rev(), b.rev())),
2571 b'p2.nbrevs': len(repo.revs('only(%d, %d)', p2.rev(), b.rev())),
2572 b'p2.nbmissingfiles': len(p2missing),
2572 b'p2.nbmissingfiles': len(p2missing),
2573 }
2573 }
2574 if dostats:
2574 if dostats:
2575 if p1missing:
2575 if p1missing:
2576 alldata['nbrevs'].append(
2576 alldata['nbrevs'].append(
2577 (data['p1.nbrevs'], b.hex(), p1.hex())
2577 (data['p1.nbrevs'], b.hex(), p1.hex())
2578 )
2578 )
2579 alldata['nbmissingfiles'].append(
2579 alldata['nbmissingfiles'].append(
2580 (data['p1.nbmissingfiles'], b.hex(), p1.hex())
2580 (data['p1.nbmissingfiles'], b.hex(), p1.hex())
2581 )
2581 )
2582 if p2missing:
2582 if p2missing:
2583 alldata['nbrevs'].append(
2583 alldata['nbrevs'].append(
2584 (data['p2.nbrevs'], b.hex(), p2.hex())
2584 (data['p2.nbrevs'], b.hex(), p2.hex())
2585 )
2585 )
2586 alldata['nbmissingfiles'].append(
2586 alldata['nbmissingfiles'].append(
2587 (data['p2.nbmissingfiles'], b.hex(), p2.hex())
2587 (data['p2.nbmissingfiles'], b.hex(), p2.hex())
2588 )
2588 )
2589 if dotiming:
2589 if dotiming:
2590 begin = util.timer()
2590 begin = util.timer()
2591 mergedata = copies.mergecopies(repo, p1, p2, b)
2591 mergedata = copies.mergecopies(repo, p1, p2, b)
2592 end = util.timer()
2592 end = util.timer()
2593 # not very stable timing since we did only one run
2593 # not very stable timing since we did only one run
2594 data['time'] = end - begin
2594 data['time'] = end - begin
2595 # mergedata contains five dicts: "copy", "movewithdir",
2595 # mergedata contains five dicts: "copy", "movewithdir",
2596 # "diverge", "renamedelete" and "dirmove".
2596 # "diverge", "renamedelete" and "dirmove".
2597 # The first 4 are about renamed file so lets count that.
2597 # The first 4 are about renamed file so lets count that.
2598 renames = len(mergedata[0])
2598 renames = len(mergedata[0])
2599 renames += len(mergedata[1])
2599 renames += len(mergedata[1])
2600 renames += len(mergedata[2])
2600 renames += len(mergedata[2])
2601 renames += len(mergedata[3])
2601 renames += len(mergedata[3])
2602 data['nbrenamedfiles'] = renames
2602 data['nbrenamedfiles'] = renames
2603 begin = util.timer()
2603 begin = util.timer()
2604 p1renames = copies.pathcopies(b, p1)
2604 p1renames = copies.pathcopies(b, p1)
2605 end = util.timer()
2605 end = util.timer()
2606 data['p1.time'] = end - begin
2606 data['p1.time'] = end - begin
2607 begin = util.timer()
2607 begin = util.timer()
2608 p2renames = copies.pathcopies(b, p2)
2608 p2renames = copies.pathcopies(b, p2)
2609 end = util.timer()
2609 end = util.timer()
2610 data['p2.time'] = end - begin
2610 data['p2.time'] = end - begin
2611 data['p1.renamedfiles'] = len(p1renames)
2611 data['p1.renamedfiles'] = len(p1renames)
2612 data['p2.renamedfiles'] = len(p2renames)
2612 data['p2.renamedfiles'] = len(p2renames)
2613
2613
2614 if dostats:
2614 if dostats:
2615 if p1missing:
2615 if p1missing:
2616 alldata['parentnbrenames'].append(
2616 alldata['parentnbrenames'].append(
2617 (data['p1.renamedfiles'], b.hex(), p1.hex())
2617 (data['p1.renamedfiles'], b.hex(), p1.hex())
2618 )
2618 )
2619 alldata['parenttime'].append(
2619 alldata['parenttime'].append(
2620 (data['p1.time'], b.hex(), p1.hex())
2620 (data['p1.time'], b.hex(), p1.hex())
2621 )
2621 )
2622 if p2missing:
2622 if p2missing:
2623 alldata['parentnbrenames'].append(
2623 alldata['parentnbrenames'].append(
2624 (data['p2.renamedfiles'], b.hex(), p2.hex())
2624 (data['p2.renamedfiles'], b.hex(), p2.hex())
2625 )
2625 )
2626 alldata['parenttime'].append(
2626 alldata['parenttime'].append(
2627 (data['p2.time'], b.hex(), p2.hex())
2627 (data['p2.time'], b.hex(), p2.hex())
2628 )
2628 )
2629 if p1missing or p2missing:
2629 if p1missing or p2missing:
2630 alldata['totalnbrenames'].append(
2630 alldata['totalnbrenames'].append(
2631 (
2631 (
2632 data['nbrenamedfiles'],
2632 data['nbrenamedfiles'],
2633 b.hex(),
2633 b.hex(),
2634 p1.hex(),
2634 p1.hex(),
2635 p2.hex(),
2635 p2.hex(),
2636 )
2636 )
2637 )
2637 )
2638 alldata['totaltime'].append(
2638 alldata['totaltime'].append(
2639 (data['time'], b.hex(), p1.hex(), p2.hex())
2639 (data['time'], b.hex(), p1.hex(), p2.hex())
2640 )
2640 )
2641 fm.startitem()
2641 fm.startitem()
2642 fm.data(**data)
2642 fm.data(**data)
2643 # make node pretty for the human output
2643 # make node pretty for the human output
2644 out = data.copy()
2644 out = data.copy()
2645 out['base'] = fm.hexfunc(b.node())
2645 out['base'] = fm.hexfunc(b.node())
2646 out['p1.node'] = fm.hexfunc(p1.node())
2646 out['p1.node'] = fm.hexfunc(p1.node())
2647 out['p2.node'] = fm.hexfunc(p2.node())
2647 out['p2.node'] = fm.hexfunc(p2.node())
2648 fm.plain(output % out)
2648 fm.plain(output % out)
2649
2649
2650 fm.end()
2650 fm.end()
2651 if dostats:
2651 if dostats:
2652 # use a second formatter because the data are quite different, not sure
2652 # use a second formatter because the data are quite different, not sure
2653 # how it flies with the templater.
2653 # how it flies with the templater.
2654 entries = [
2654 entries = [
2655 ('nbrevs', 'number of revision covered'),
2655 ('nbrevs', 'number of revision covered'),
2656 ('nbmissingfiles', 'number of missing files at head'),
2656 ('nbmissingfiles', 'number of missing files at head'),
2657 ]
2657 ]
2658 if dotiming:
2658 if dotiming:
2659 entries.append(
2659 entries.append(
2660 ('parentnbrenames', 'rename from one parent to base')
2660 ('parentnbrenames', 'rename from one parent to base')
2661 )
2661 )
2662 entries.append(('totalnbrenames', 'total number of renames'))
2662 entries.append(('totalnbrenames', 'total number of renames'))
2663 entries.append(('parenttime', 'time for one parent'))
2663 entries.append(('parenttime', 'time for one parent'))
2664 entries.append(('totaltime', 'time for both parents'))
2664 entries.append(('totaltime', 'time for both parents'))
2665 _displaystats(ui, opts, entries, alldata)
2665 _displaystats(ui, opts, entries, alldata)
2666
2666
2667
2667
2668 @command(
2668 @command(
2669 b'perf::helper-pathcopies|perfhelper-pathcopies',
2669 b'perf::helper-pathcopies|perfhelper-pathcopies',
2670 formatteropts
2670 formatteropts
2671 + [
2671 + [
2672 (b'r', b'revs', [], b'restrict search to these revisions'),
2672 (b'r', b'revs', [], b'restrict search to these revisions'),
2673 (b'', b'timing', False, b'provides extra data (costly)'),
2673 (b'', b'timing', False, b'provides extra data (costly)'),
2674 (b'', b'stats', False, b'provides statistic about the measured data'),
2674 (b'', b'stats', False, b'provides statistic about the measured data'),
2675 ],
2675 ],
2676 )
2676 )
2677 def perfhelperpathcopies(ui, repo, revs=[], **opts):
2677 def perfhelperpathcopies(ui, repo, revs=[], **opts):
2678 """find statistic about potential parameters for the `perftracecopies`
2678 """find statistic about potential parameters for the `perftracecopies`
2679
2679
2680 This command find source-destination pair relevant for copytracing testing.
2680 This command find source-destination pair relevant for copytracing testing.
2681 It report value for some of the parameters that impact copy tracing time.
2681 It report value for some of the parameters that impact copy tracing time.
2682
2682
2683 If `--timing` is set, rename detection is run and the associated timing
2683 If `--timing` is set, rename detection is run and the associated timing
2684 will be reported. The extra details comes at the cost of a slower command
2684 will be reported. The extra details comes at the cost of a slower command
2685 execution.
2685 execution.
2686
2686
2687 Since the rename detection is only run once, other factors might easily
2687 Since the rename detection is only run once, other factors might easily
2688 affect the precision of the timing. However it should give a good
2688 affect the precision of the timing. However it should give a good
2689 approximation of which revision pairs are very costly.
2689 approximation of which revision pairs are very costly.
2690 """
2690 """
2691 opts = _byteskwargs(opts)
2691 opts = _byteskwargs(opts)
2692 fm = ui.formatter(b'perf', opts)
2692 fm = ui.formatter(b'perf', opts)
2693 dotiming = opts[b'timing']
2693 dotiming = opts[b'timing']
2694 dostats = opts[b'stats']
2694 dostats = opts[b'stats']
2695
2695
2696 if dotiming:
2696 if dotiming:
2697 header = '%12s %12s %12s %12s %12s %12s\n'
2697 header = '%12s %12s %12s %12s %12s %12s\n'
2698 output = (
2698 output = (
2699 "%(source)12s %(destination)12s "
2699 "%(source)12s %(destination)12s "
2700 "%(nbrevs)12d %(nbmissingfiles)12d "
2700 "%(nbrevs)12d %(nbmissingfiles)12d "
2701 "%(nbrenamedfiles)12d %(time)18.5f\n"
2701 "%(nbrenamedfiles)12d %(time)18.5f\n"
2702 )
2702 )
2703 header_names = (
2703 header_names = (
2704 "source",
2704 "source",
2705 "destination",
2705 "destination",
2706 "nb-revs",
2706 "nb-revs",
2707 "nb-files",
2707 "nb-files",
2708 "nb-renames",
2708 "nb-renames",
2709 "time",
2709 "time",
2710 )
2710 )
2711 fm.plain(header % header_names)
2711 fm.plain(header % header_names)
2712 else:
2712 else:
2713 header = '%12s %12s %12s %12s\n'
2713 header = '%12s %12s %12s %12s\n'
2714 output = (
2714 output = (
2715 "%(source)12s %(destination)12s "
2715 "%(source)12s %(destination)12s "
2716 "%(nbrevs)12d %(nbmissingfiles)12d\n"
2716 "%(nbrevs)12d %(nbmissingfiles)12d\n"
2717 )
2717 )
2718 fm.plain(header % ("source", "destination", "nb-revs", "nb-files"))
2718 fm.plain(header % ("source", "destination", "nb-revs", "nb-files"))
2719
2719
2720 if not revs:
2720 if not revs:
2721 revs = ['all()']
2721 revs = ['all()']
2722 revs = scmutil.revrange(repo, revs)
2722 revs = scmutil.revrange(repo, revs)
2723
2723
2724 if dostats:
2724 if dostats:
2725 alldata = {
2725 alldata = {
2726 'nbrevs': [],
2726 'nbrevs': [],
2727 'nbmissingfiles': [],
2727 'nbmissingfiles': [],
2728 }
2728 }
2729 if dotiming:
2729 if dotiming:
2730 alldata['nbrenames'] = []
2730 alldata['nbrenames'] = []
2731 alldata['time'] = []
2731 alldata['time'] = []
2732
2732
2733 roi = repo.revs('merge() and %ld', revs)
2733 roi = repo.revs('merge() and %ld', revs)
2734 for r in roi:
2734 for r in roi:
2735 ctx = repo[r]
2735 ctx = repo[r]
2736 p1 = ctx.p1().rev()
2736 p1 = ctx.p1().rev()
2737 p2 = ctx.p2().rev()
2737 p2 = ctx.p2().rev()
2738 bases = repo.changelog._commonancestorsheads(p1, p2)
2738 bases = repo.changelog._commonancestorsheads(p1, p2)
2739 for p in (p1, p2):
2739 for p in (p1, p2):
2740 for b in bases:
2740 for b in bases:
2741 base = repo[b]
2741 base = repo[b]
2742 parent = repo[p]
2742 parent = repo[p]
2743 missing = copies._computeforwardmissing(base, parent)
2743 missing = copies._computeforwardmissing(base, parent)
2744 if not missing:
2744 if not missing:
2745 continue
2745 continue
2746 data = {
2746 data = {
2747 b'source': base.hex(),
2747 b'source': base.hex(),
2748 b'destination': parent.hex(),
2748 b'destination': parent.hex(),
2749 b'nbrevs': len(repo.revs('only(%d, %d)', p, b)),
2749 b'nbrevs': len(repo.revs('only(%d, %d)', p, b)),
2750 b'nbmissingfiles': len(missing),
2750 b'nbmissingfiles': len(missing),
2751 }
2751 }
2752 if dostats:
2752 if dostats:
2753 alldata['nbrevs'].append(
2753 alldata['nbrevs'].append(
2754 (
2754 (
2755 data['nbrevs'],
2755 data['nbrevs'],
2756 base.hex(),
2756 base.hex(),
2757 parent.hex(),
2757 parent.hex(),
2758 )
2758 )
2759 )
2759 )
2760 alldata['nbmissingfiles'].append(
2760 alldata['nbmissingfiles'].append(
2761 (
2761 (
2762 data['nbmissingfiles'],
2762 data['nbmissingfiles'],
2763 base.hex(),
2763 base.hex(),
2764 parent.hex(),
2764 parent.hex(),
2765 )
2765 )
2766 )
2766 )
2767 if dotiming:
2767 if dotiming:
2768 begin = util.timer()
2768 begin = util.timer()
2769 renames = copies.pathcopies(base, parent)
2769 renames = copies.pathcopies(base, parent)
2770 end = util.timer()
2770 end = util.timer()
2771 # not very stable timing since we did only one run
2771 # not very stable timing since we did only one run
2772 data['time'] = end - begin
2772 data['time'] = end - begin
2773 data['nbrenamedfiles'] = len(renames)
2773 data['nbrenamedfiles'] = len(renames)
2774 if dostats:
2774 if dostats:
2775 alldata['time'].append(
2775 alldata['time'].append(
2776 (
2776 (
2777 data['time'],
2777 data['time'],
2778 base.hex(),
2778 base.hex(),
2779 parent.hex(),
2779 parent.hex(),
2780 )
2780 )
2781 )
2781 )
2782 alldata['nbrenames'].append(
2782 alldata['nbrenames'].append(
2783 (
2783 (
2784 data['nbrenamedfiles'],
2784 data['nbrenamedfiles'],
2785 base.hex(),
2785 base.hex(),
2786 parent.hex(),
2786 parent.hex(),
2787 )
2787 )
2788 )
2788 )
2789 fm.startitem()
2789 fm.startitem()
2790 fm.data(**data)
2790 fm.data(**data)
2791 out = data.copy()
2791 out = data.copy()
2792 out['source'] = fm.hexfunc(base.node())
2792 out['source'] = fm.hexfunc(base.node())
2793 out['destination'] = fm.hexfunc(parent.node())
2793 out['destination'] = fm.hexfunc(parent.node())
2794 fm.plain(output % out)
2794 fm.plain(output % out)
2795
2795
2796 fm.end()
2796 fm.end()
2797 if dostats:
2797 if dostats:
2798 entries = [
2798 entries = [
2799 ('nbrevs', 'number of revision covered'),
2799 ('nbrevs', 'number of revision covered'),
2800 ('nbmissingfiles', 'number of missing files at head'),
2800 ('nbmissingfiles', 'number of missing files at head'),
2801 ]
2801 ]
2802 if dotiming:
2802 if dotiming:
2803 entries.append(('nbrenames', 'renamed files'))
2803 entries.append(('nbrenames', 'renamed files'))
2804 entries.append(('time', 'time'))
2804 entries.append(('time', 'time'))
2805 _displaystats(ui, opts, entries, alldata)
2805 _displaystats(ui, opts, entries, alldata)
2806
2806
2807
2807
2808 @command(b'perf::cca|perfcca', formatteropts)
2808 @command(b'perf::cca|perfcca', formatteropts)
2809 def perfcca(ui, repo, **opts):
2809 def perfcca(ui, repo, **opts):
2810 opts = _byteskwargs(opts)
2810 opts = _byteskwargs(opts)
2811 timer, fm = gettimer(ui, opts)
2811 timer, fm = gettimer(ui, opts)
2812 timer(lambda: scmutil.casecollisionauditor(ui, False, repo.dirstate))
2812 timer(lambda: scmutil.casecollisionauditor(ui, False, repo.dirstate))
2813 fm.end()
2813 fm.end()
2814
2814
2815
2815
2816 @command(b'perf::fncacheload|perffncacheload', formatteropts)
2816 @command(b'perf::fncacheload|perffncacheload', formatteropts)
2817 def perffncacheload(ui, repo, **opts):
2817 def perffncacheload(ui, repo, **opts):
2818 opts = _byteskwargs(opts)
2818 opts = _byteskwargs(opts)
2819 timer, fm = gettimer(ui, opts)
2819 timer, fm = gettimer(ui, opts)
2820 s = repo.store
2820 s = repo.store
2821
2821
2822 def d():
2822 def d():
2823 s.fncache._load()
2823 s.fncache._load()
2824
2824
2825 timer(d)
2825 timer(d)
2826 fm.end()
2826 fm.end()
2827
2827
2828
2828
2829 @command(b'perf::fncachewrite|perffncachewrite', formatteropts)
2829 @command(b'perf::fncachewrite|perffncachewrite', formatteropts)
2830 def perffncachewrite(ui, repo, **opts):
2830 def perffncachewrite(ui, repo, **opts):
2831 opts = _byteskwargs(opts)
2831 opts = _byteskwargs(opts)
2832 timer, fm = gettimer(ui, opts)
2832 timer, fm = gettimer(ui, opts)
2833 s = repo.store
2833 s = repo.store
2834 lock = repo.lock()
2834 lock = repo.lock()
2835 s.fncache._load()
2835 s.fncache._load()
2836 tr = repo.transaction(b'perffncachewrite')
2836 tr = repo.transaction(b'perffncachewrite')
2837 tr.addbackup(b'fncache')
2837 tr.addbackup(b'fncache')
2838
2838
2839 def d():
2839 def d():
2840 s.fncache._dirty = True
2840 s.fncache._dirty = True
2841 s.fncache.write(tr)
2841 s.fncache.write(tr)
2842
2842
2843 timer(d)
2843 timer(d)
2844 tr.close()
2844 tr.close()
2845 lock.release()
2845 lock.release()
2846 fm.end()
2846 fm.end()
2847
2847
2848
2848
2849 @command(b'perf::fncacheencode|perffncacheencode', formatteropts)
2849 @command(b'perf::fncacheencode|perffncacheencode', formatteropts)
2850 def perffncacheencode(ui, repo, **opts):
2850 def perffncacheencode(ui, repo, **opts):
2851 opts = _byteskwargs(opts)
2851 opts = _byteskwargs(opts)
2852 timer, fm = gettimer(ui, opts)
2852 timer, fm = gettimer(ui, opts)
2853 s = repo.store
2853 s = repo.store
2854 s.fncache._load()
2854 s.fncache._load()
2855
2855
2856 def d():
2856 def d():
2857 for p in s.fncache.entries:
2857 for p in s.fncache.entries:
2858 s.encode(p)
2858 s.encode(p)
2859
2859
2860 timer(d)
2860 timer(d)
2861 fm.end()
2861 fm.end()
2862
2862
2863
2863
2864 def _bdiffworker(q, blocks, xdiff, ready, done):
2864 def _bdiffworker(q, blocks, xdiff, ready, done):
2865 while not done.is_set():
2865 while not done.is_set():
2866 pair = q.get()
2866 pair = q.get()
2867 while pair is not None:
2867 while pair is not None:
2868 if xdiff:
2868 if xdiff:
2869 mdiff.bdiff.xdiffblocks(*pair)
2869 mdiff.bdiff.xdiffblocks(*pair)
2870 elif blocks:
2870 elif blocks:
2871 mdiff.bdiff.blocks(*pair)
2871 mdiff.bdiff.blocks(*pair)
2872 else:
2872 else:
2873 mdiff.textdiff(*pair)
2873 mdiff.textdiff(*pair)
2874 q.task_done()
2874 q.task_done()
2875 pair = q.get()
2875 pair = q.get()
2876 q.task_done() # for the None one
2876 q.task_done() # for the None one
2877 with ready:
2877 with ready:
2878 ready.wait()
2878 ready.wait()
2879
2879
2880
2880
2881 def _manifestrevision(repo, mnode):
2881 def _manifestrevision(repo, mnode):
2882 ml = repo.manifestlog
2882 ml = repo.manifestlog
2883
2883
2884 if util.safehasattr(ml, b'getstorage'):
2884 if util.safehasattr(ml, b'getstorage'):
2885 store = ml.getstorage(b'')
2885 store = ml.getstorage(b'')
2886 else:
2886 else:
2887 store = ml._revlog
2887 store = ml._revlog
2888
2888
2889 return store.revision(mnode)
2889 return store.revision(mnode)
2890
2890
2891
2891
2892 @command(
2892 @command(
2893 b'perf::bdiff|perfbdiff',
2893 b'perf::bdiff|perfbdiff',
2894 revlogopts
2894 revlogopts
2895 + formatteropts
2895 + formatteropts
2896 + [
2896 + [
2897 (
2897 (
2898 b'',
2898 b'',
2899 b'count',
2899 b'count',
2900 1,
2900 1,
2901 b'number of revisions to test (when using --startrev)',
2901 b'number of revisions to test (when using --startrev)',
2902 ),
2902 ),
2903 (b'', b'alldata', False, b'test bdiffs for all associated revisions'),
2903 (b'', b'alldata', False, b'test bdiffs for all associated revisions'),
2904 (b'', b'threads', 0, b'number of thread to use (disable with 0)'),
2904 (b'', b'threads', 0, b'number of thread to use (disable with 0)'),
2905 (b'', b'blocks', False, b'test computing diffs into blocks'),
2905 (b'', b'blocks', False, b'test computing diffs into blocks'),
2906 (b'', b'xdiff', False, b'use xdiff algorithm'),
2906 (b'', b'xdiff', False, b'use xdiff algorithm'),
2907 ],
2907 ],
2908 b'-c|-m|FILE REV',
2908 b'-c|-m|FILE REV',
2909 )
2909 )
2910 def perfbdiff(ui, repo, file_, rev=None, count=None, threads=0, **opts):
2910 def perfbdiff(ui, repo, file_, rev=None, count=None, threads=0, **opts):
2911 """benchmark a bdiff between revisions
2911 """benchmark a bdiff between revisions
2912
2912
2913 By default, benchmark a bdiff between its delta parent and itself.
2913 By default, benchmark a bdiff between its delta parent and itself.
2914
2914
2915 With ``--count``, benchmark bdiffs between delta parents and self for N
2915 With ``--count``, benchmark bdiffs between delta parents and self for N
2916 revisions starting at the specified revision.
2916 revisions starting at the specified revision.
2917
2917
2918 With ``--alldata``, assume the requested revision is a changeset and
2918 With ``--alldata``, assume the requested revision is a changeset and
2919 measure bdiffs for all changes related to that changeset (manifest
2919 measure bdiffs for all changes related to that changeset (manifest
2920 and filelogs).
2920 and filelogs).
2921 """
2921 """
2922 opts = _byteskwargs(opts)
2922 opts = _byteskwargs(opts)
2923
2923
2924 if opts[b'xdiff'] and not opts[b'blocks']:
2924 if opts[b'xdiff'] and not opts[b'blocks']:
2925 raise error.CommandError(b'perfbdiff', b'--xdiff requires --blocks')
2925 raise error.CommandError(b'perfbdiff', b'--xdiff requires --blocks')
2926
2926
2927 if opts[b'alldata']:
2927 if opts[b'alldata']:
2928 opts[b'changelog'] = True
2928 opts[b'changelog'] = True
2929
2929
2930 if opts.get(b'changelog') or opts.get(b'manifest'):
2930 if opts.get(b'changelog') or opts.get(b'manifest'):
2931 file_, rev = None, file_
2931 file_, rev = None, file_
2932 elif rev is None:
2932 elif rev is None:
2933 raise error.CommandError(b'perfbdiff', b'invalid arguments')
2933 raise error.CommandError(b'perfbdiff', b'invalid arguments')
2934
2934
2935 blocks = opts[b'blocks']
2935 blocks = opts[b'blocks']
2936 xdiff = opts[b'xdiff']
2936 xdiff = opts[b'xdiff']
2937 textpairs = []
2937 textpairs = []
2938
2938
2939 r = cmdutil.openrevlog(repo, b'perfbdiff', file_, opts)
2939 r = cmdutil.openrevlog(repo, b'perfbdiff', file_, opts)
2940
2940
2941 startrev = r.rev(r.lookup(rev))
2941 startrev = r.rev(r.lookup(rev))
2942 for rev in range(startrev, min(startrev + count, len(r) - 1)):
2942 for rev in range(startrev, min(startrev + count, len(r) - 1)):
2943 if opts[b'alldata']:
2943 if opts[b'alldata']:
2944 # Load revisions associated with changeset.
2944 # Load revisions associated with changeset.
2945 ctx = repo[rev]
2945 ctx = repo[rev]
2946 mtext = _manifestrevision(repo, ctx.manifestnode())
2946 mtext = _manifestrevision(repo, ctx.manifestnode())
2947 for pctx in ctx.parents():
2947 for pctx in ctx.parents():
2948 pman = _manifestrevision(repo, pctx.manifestnode())
2948 pman = _manifestrevision(repo, pctx.manifestnode())
2949 textpairs.append((pman, mtext))
2949 textpairs.append((pman, mtext))
2950
2950
2951 # Load filelog revisions by iterating manifest delta.
2951 # Load filelog revisions by iterating manifest delta.
2952 man = ctx.manifest()
2952 man = ctx.manifest()
2953 pman = ctx.p1().manifest()
2953 pman = ctx.p1().manifest()
2954 for filename, change in pman.diff(man).items():
2954 for filename, change in pman.diff(man).items():
2955 fctx = repo.file(filename)
2955 fctx = repo.file(filename)
2956 f1 = fctx.revision(change[0][0] or -1)
2956 f1 = fctx.revision(change[0][0] or -1)
2957 f2 = fctx.revision(change[1][0] or -1)
2957 f2 = fctx.revision(change[1][0] or -1)
2958 textpairs.append((f1, f2))
2958 textpairs.append((f1, f2))
2959 else:
2959 else:
2960 dp = r.deltaparent(rev)
2960 dp = r.deltaparent(rev)
2961 textpairs.append((r.revision(dp), r.revision(rev)))
2961 textpairs.append((r.revision(dp), r.revision(rev)))
2962
2962
2963 withthreads = threads > 0
2963 withthreads = threads > 0
2964 if not withthreads:
2964 if not withthreads:
2965
2965
2966 def d():
2966 def d():
2967 for pair in textpairs:
2967 for pair in textpairs:
2968 if xdiff:
2968 if xdiff:
2969 mdiff.bdiff.xdiffblocks(*pair)
2969 mdiff.bdiff.xdiffblocks(*pair)
2970 elif blocks:
2970 elif blocks:
2971 mdiff.bdiff.blocks(*pair)
2971 mdiff.bdiff.blocks(*pair)
2972 else:
2972 else:
2973 mdiff.textdiff(*pair)
2973 mdiff.textdiff(*pair)
2974
2974
2975 else:
2975 else:
2976 q = queue()
2976 q = queue()
2977 for i in _xrange(threads):
2977 for i in _xrange(threads):
2978 q.put(None)
2978 q.put(None)
2979 ready = threading.Condition()
2979 ready = threading.Condition()
2980 done = threading.Event()
2980 done = threading.Event()
2981 for i in _xrange(threads):
2981 for i in _xrange(threads):
2982 threading.Thread(
2982 threading.Thread(
2983 target=_bdiffworker, args=(q, blocks, xdiff, ready, done)
2983 target=_bdiffworker, args=(q, blocks, xdiff, ready, done)
2984 ).start()
2984 ).start()
2985 q.join()
2985 q.join()
2986
2986
2987 def d():
2987 def d():
2988 for pair in textpairs:
2988 for pair in textpairs:
2989 q.put(pair)
2989 q.put(pair)
2990 for i in _xrange(threads):
2990 for i in _xrange(threads):
2991 q.put(None)
2991 q.put(None)
2992 with ready:
2992 with ready:
2993 ready.notify_all()
2993 ready.notify_all()
2994 q.join()
2994 q.join()
2995
2995
2996 timer, fm = gettimer(ui, opts)
2996 timer, fm = gettimer(ui, opts)
2997 timer(d)
2997 timer(d)
2998 fm.end()
2998 fm.end()
2999
2999
3000 if withthreads:
3000 if withthreads:
3001 done.set()
3001 done.set()
3002 for i in _xrange(threads):
3002 for i in _xrange(threads):
3003 q.put(None)
3003 q.put(None)
3004 with ready:
3004 with ready:
3005 ready.notify_all()
3005 ready.notify_all()
3006
3006
3007
3007
3008 @command(
3008 @command(
3009 b'perf::unbundle',
3009 b'perf::unbundle',
3010 formatteropts,
3010 formatteropts,
3011 b'BUNDLE_FILE',
3011 b'BUNDLE_FILE',
3012 )
3012 )
3013 def perf_unbundle(ui, repo, fname, **opts):
3013 def perf_unbundle(ui, repo, fname, **opts):
3014 """benchmark application of a bundle in a repository.
3014 """benchmark application of a bundle in a repository.
3015
3015
3016 This does not include the final transaction processing"""
3016 This does not include the final transaction processing"""
3017
3017
3018 from mercurial import exchange
3018 from mercurial import exchange
3019 from mercurial import bundle2
3019 from mercurial import bundle2
3020 from mercurial import transaction
3020 from mercurial import transaction
3021
3021
3022 opts = _byteskwargs(opts)
3022 opts = _byteskwargs(opts)
3023
3023
3024 ### some compatibility hotfix
3024 ### some compatibility hotfix
3025 #
3025 #
3026 # the data attribute is dropped in 63edc384d3b7 a changeset introducing a
3026 # the data attribute is dropped in 63edc384d3b7 a changeset introducing a
3027 # critical regression that break transaction rollback for files that are
3027 # critical regression that break transaction rollback for files that are
3028 # de-inlined.
3028 # de-inlined.
3029 method = transaction.transaction._addentry
3029 method = transaction.transaction._addentry
3030 pre_63edc384d3b7 = "data" in getargspec(method).args
3030 pre_63edc384d3b7 = "data" in getargspec(method).args
3031 # the `detailed_exit_code` attribute is introduced in 33c0c25d0b0f
3031 # the `detailed_exit_code` attribute is introduced in 33c0c25d0b0f
3032 # a changeset that is a close descendant of 18415fc918a1, the changeset
3032 # a changeset that is a close descendant of 18415fc918a1, the changeset
3033 # that conclude the fix run for the bug introduced in 63edc384d3b7.
3033 # that conclude the fix run for the bug introduced in 63edc384d3b7.
3034 args = getargspec(error.Abort.__init__).args
3034 args = getargspec(error.Abort.__init__).args
3035 post_18415fc918a1 = "detailed_exit_code" in args
3035 post_18415fc918a1 = "detailed_exit_code" in args
3036
3036
3037 old_max_inline = None
3037 old_max_inline = None
3038 try:
3038 try:
3039 if not (pre_63edc384d3b7 or post_18415fc918a1):
3039 if not (pre_63edc384d3b7 or post_18415fc918a1):
3040 # disable inlining
3040 # disable inlining
3041 old_max_inline = mercurial.revlog._maxinline
3041 old_max_inline = mercurial.revlog._maxinline
3042 # large enough to never happen
3042 # large enough to never happen
3043 mercurial.revlog._maxinline = 2 ** 50
3043 mercurial.revlog._maxinline = 2 ** 50
3044
3044
3045 with repo.lock():
3045 with repo.lock():
3046 bundle = [None, None]
3046 bundle = [None, None]
3047 orig_quiet = repo.ui.quiet
3047 orig_quiet = repo.ui.quiet
3048 try:
3048 try:
3049 repo.ui.quiet = True
3049 repo.ui.quiet = True
3050 with open(fname, mode="rb") as f:
3050 with open(fname, mode="rb") as f:
3051
3051
3052 def noop_report(*args, **kwargs):
3052 def noop_report(*args, **kwargs):
3053 pass
3053 pass
3054
3054
3055 def setup():
3055 def setup():
3056 gen, tr = bundle
3056 gen, tr = bundle
3057 if tr is not None:
3057 if tr is not None:
3058 tr.abort()
3058 tr.abort()
3059 bundle[:] = [None, None]
3059 bundle[:] = [None, None]
3060 f.seek(0)
3060 f.seek(0)
3061 bundle[0] = exchange.readbundle(ui, f, fname)
3061 bundle[0] = exchange.readbundle(ui, f, fname)
3062 bundle[1] = repo.transaction(b'perf::unbundle')
3062 bundle[1] = repo.transaction(b'perf::unbundle')
3063 # silence the transaction
3063 # silence the transaction
3064 bundle[1]._report = noop_report
3064 bundle[1]._report = noop_report
3065
3065
3066 def apply():
3066 def apply():
3067 gen, tr = bundle
3067 gen, tr = bundle
3068 bundle2.applybundle(
3068 bundle2.applybundle(
3069 repo,
3069 repo,
3070 gen,
3070 gen,
3071 tr,
3071 tr,
3072 source=b'perf::unbundle',
3072 source=b'perf::unbundle',
3073 url=fname,
3073 url=fname,
3074 )
3074 )
3075
3075
3076 timer, fm = gettimer(ui, opts)
3076 timer, fm = gettimer(ui, opts)
3077 timer(apply, setup=setup)
3077 timer(apply, setup=setup)
3078 fm.end()
3078 fm.end()
3079 finally:
3079 finally:
3080 repo.ui.quiet == orig_quiet
3080 repo.ui.quiet == orig_quiet
3081 gen, tr = bundle
3081 gen, tr = bundle
3082 if tr is not None:
3082 if tr is not None:
3083 tr.abort()
3083 tr.abort()
3084 finally:
3084 finally:
3085 if old_max_inline is not None:
3085 if old_max_inline is not None:
3086 mercurial.revlog._maxinline = old_max_inline
3086 mercurial.revlog._maxinline = old_max_inline
3087
3087
3088
3088
3089 @command(
3089 @command(
3090 b'perf::unidiff|perfunidiff',
3090 b'perf::unidiff|perfunidiff',
3091 revlogopts
3091 revlogopts
3092 + formatteropts
3092 + formatteropts
3093 + [
3093 + [
3094 (
3094 (
3095 b'',
3095 b'',
3096 b'count',
3096 b'count',
3097 1,
3097 1,
3098 b'number of revisions to test (when using --startrev)',
3098 b'number of revisions to test (when using --startrev)',
3099 ),
3099 ),
3100 (b'', b'alldata', False, b'test unidiffs for all associated revisions'),
3100 (b'', b'alldata', False, b'test unidiffs for all associated revisions'),
3101 ],
3101 ],
3102 b'-c|-m|FILE REV',
3102 b'-c|-m|FILE REV',
3103 )
3103 )
3104 def perfunidiff(ui, repo, file_, rev=None, count=None, **opts):
3104 def perfunidiff(ui, repo, file_, rev=None, count=None, **opts):
3105 """benchmark a unified diff between revisions
3105 """benchmark a unified diff between revisions
3106
3106
3107 This doesn't include any copy tracing - it's just a unified diff
3107 This doesn't include any copy tracing - it's just a unified diff
3108 of the texts.
3108 of the texts.
3109
3109
3110 By default, benchmark a diff between its delta parent and itself.
3110 By default, benchmark a diff between its delta parent and itself.
3111
3111
3112 With ``--count``, benchmark diffs between delta parents and self for N
3112 With ``--count``, benchmark diffs between delta parents and self for N
3113 revisions starting at the specified revision.
3113 revisions starting at the specified revision.
3114
3114
3115 With ``--alldata``, assume the requested revision is a changeset and
3115 With ``--alldata``, assume the requested revision is a changeset and
3116 measure diffs for all changes related to that changeset (manifest
3116 measure diffs for all changes related to that changeset (manifest
3117 and filelogs).
3117 and filelogs).
3118 """
3118 """
3119 opts = _byteskwargs(opts)
3119 opts = _byteskwargs(opts)
3120 if opts[b'alldata']:
3120 if opts[b'alldata']:
3121 opts[b'changelog'] = True
3121 opts[b'changelog'] = True
3122
3122
3123 if opts.get(b'changelog') or opts.get(b'manifest'):
3123 if opts.get(b'changelog') or opts.get(b'manifest'):
3124 file_, rev = None, file_
3124 file_, rev = None, file_
3125 elif rev is None:
3125 elif rev is None:
3126 raise error.CommandError(b'perfunidiff', b'invalid arguments')
3126 raise error.CommandError(b'perfunidiff', b'invalid arguments')
3127
3127
3128 textpairs = []
3128 textpairs = []
3129
3129
3130 r = cmdutil.openrevlog(repo, b'perfunidiff', file_, opts)
3130 r = cmdutil.openrevlog(repo, b'perfunidiff', file_, opts)
3131
3131
3132 startrev = r.rev(r.lookup(rev))
3132 startrev = r.rev(r.lookup(rev))
3133 for rev in range(startrev, min(startrev + count, len(r) - 1)):
3133 for rev in range(startrev, min(startrev + count, len(r) - 1)):
3134 if opts[b'alldata']:
3134 if opts[b'alldata']:
3135 # Load revisions associated with changeset.
3135 # Load revisions associated with changeset.
3136 ctx = repo[rev]
3136 ctx = repo[rev]
3137 mtext = _manifestrevision(repo, ctx.manifestnode())
3137 mtext = _manifestrevision(repo, ctx.manifestnode())
3138 for pctx in ctx.parents():
3138 for pctx in ctx.parents():
3139 pman = _manifestrevision(repo, pctx.manifestnode())
3139 pman = _manifestrevision(repo, pctx.manifestnode())
3140 textpairs.append((pman, mtext))
3140 textpairs.append((pman, mtext))
3141
3141
3142 # Load filelog revisions by iterating manifest delta.
3142 # Load filelog revisions by iterating manifest delta.
3143 man = ctx.manifest()
3143 man = ctx.manifest()
3144 pman = ctx.p1().manifest()
3144 pman = ctx.p1().manifest()
3145 for filename, change in pman.diff(man).items():
3145 for filename, change in pman.diff(man).items():
3146 fctx = repo.file(filename)
3146 fctx = repo.file(filename)
3147 f1 = fctx.revision(change[0][0] or -1)
3147 f1 = fctx.revision(change[0][0] or -1)
3148 f2 = fctx.revision(change[1][0] or -1)
3148 f2 = fctx.revision(change[1][0] or -1)
3149 textpairs.append((f1, f2))
3149 textpairs.append((f1, f2))
3150 else:
3150 else:
3151 dp = r.deltaparent(rev)
3151 dp = r.deltaparent(rev)
3152 textpairs.append((r.revision(dp), r.revision(rev)))
3152 textpairs.append((r.revision(dp), r.revision(rev)))
3153
3153
3154 def d():
3154 def d():
3155 for left, right in textpairs:
3155 for left, right in textpairs:
3156 # The date strings don't matter, so we pass empty strings.
3156 # The date strings don't matter, so we pass empty strings.
3157 headerlines, hunks = mdiff.unidiff(
3157 headerlines, hunks = mdiff.unidiff(
3158 left, b'', right, b'', b'left', b'right', binary=False
3158 left, b'', right, b'', b'left', b'right', binary=False
3159 )
3159 )
3160 # consume iterators in roughly the way patch.py does
3160 # consume iterators in roughly the way patch.py does
3161 b'\n'.join(headerlines)
3161 b'\n'.join(headerlines)
3162 b''.join(sum((list(hlines) for hrange, hlines in hunks), []))
3162 b''.join(sum((list(hlines) for hrange, hlines in hunks), []))
3163
3163
3164 timer, fm = gettimer(ui, opts)
3164 timer, fm = gettimer(ui, opts)
3165 timer(d)
3165 timer(d)
3166 fm.end()
3166 fm.end()
3167
3167
3168
3168
3169 @command(b'perf::diffwd|perfdiffwd', formatteropts)
3169 @command(b'perf::diffwd|perfdiffwd', formatteropts)
3170 def perfdiffwd(ui, repo, **opts):
3170 def perfdiffwd(ui, repo, **opts):
3171 """Profile diff of working directory changes"""
3171 """Profile diff of working directory changes"""
3172 opts = _byteskwargs(opts)
3172 opts = _byteskwargs(opts)
3173 timer, fm = gettimer(ui, opts)
3173 timer, fm = gettimer(ui, opts)
3174 options = {
3174 options = {
3175 'w': 'ignore_all_space',
3175 'w': 'ignore_all_space',
3176 'b': 'ignore_space_change',
3176 'b': 'ignore_space_change',
3177 'B': 'ignore_blank_lines',
3177 'B': 'ignore_blank_lines',
3178 }
3178 }
3179
3179
3180 for diffopt in ('', 'w', 'b', 'B', 'wB'):
3180 for diffopt in ('', 'w', 'b', 'B', 'wB'):
3181 opts = {options[c]: b'1' for c in diffopt}
3181 opts = {options[c]: b'1' for c in diffopt}
3182
3182
3183 def d():
3183 def d():
3184 ui.pushbuffer()
3184 ui.pushbuffer()
3185 commands.diff(ui, repo, **opts)
3185 commands.diff(ui, repo, **opts)
3186 ui.popbuffer()
3186 ui.popbuffer()
3187
3187
3188 diffopt = diffopt.encode('ascii')
3188 diffopt = diffopt.encode('ascii')
3189 title = b'diffopts: %s' % (diffopt and (b'-' + diffopt) or b'none')
3189 title = b'diffopts: %s' % (diffopt and (b'-' + diffopt) or b'none')
3190 timer(d, title=title)
3190 timer(d, title=title)
3191 fm.end()
3191 fm.end()
3192
3192
3193
3193
3194 @command(
3194 @command(
3195 b'perf::revlogindex|perfrevlogindex',
3195 b'perf::revlogindex|perfrevlogindex',
3196 revlogopts + formatteropts,
3196 revlogopts + formatteropts,
3197 b'-c|-m|FILE',
3197 b'-c|-m|FILE',
3198 )
3198 )
3199 def perfrevlogindex(ui, repo, file_=None, **opts):
3199 def perfrevlogindex(ui, repo, file_=None, **opts):
3200 """Benchmark operations against a revlog index.
3200 """Benchmark operations against a revlog index.
3201
3201
3202 This tests constructing a revlog instance, reading index data,
3202 This tests constructing a revlog instance, reading index data,
3203 parsing index data, and performing various operations related to
3203 parsing index data, and performing various operations related to
3204 index data.
3204 index data.
3205 """
3205 """
3206
3206
3207 opts = _byteskwargs(opts)
3207 opts = _byteskwargs(opts)
3208
3208
3209 rl = cmdutil.openrevlog(repo, b'perfrevlogindex', file_, opts)
3209 rl = cmdutil.openrevlog(repo, b'perfrevlogindex', file_, opts)
3210
3210
3211 opener = getattr(rl, 'opener') # trick linter
3211 opener = getattr(rl, 'opener') # trick linter
3212 # compat with hg <= 5.8
3212 # compat with hg <= 5.8
3213 radix = getattr(rl, 'radix', None)
3213 radix = getattr(rl, 'radix', None)
3214 indexfile = getattr(rl, '_indexfile', None)
3214 indexfile = getattr(rl, '_indexfile', None)
3215 if indexfile is None:
3215 if indexfile is None:
3216 # compatibility with <= hg-5.8
3216 # compatibility with <= hg-5.8
3217 indexfile = getattr(rl, 'indexfile')
3217 indexfile = getattr(rl, 'indexfile')
3218 data = opener.read(indexfile)
3218 data = opener.read(indexfile)
3219
3219
3220 header = struct.unpack(b'>I', data[0:4])[0]
3220 header = struct.unpack(b'>I', data[0:4])[0]
3221 version = header & 0xFFFF
3221 version = header & 0xFFFF
3222 if version == 1:
3222 if version == 1:
3223 inline = header & (1 << 16)
3223 inline = header & (1 << 16)
3224 else:
3224 else:
3225 raise error.Abort(b'unsupported revlog version: %d' % version)
3225 raise error.Abort(b'unsupported revlog version: %d' % version)
3226
3226
3227 parse_index_v1 = getattr(mercurial.revlog, 'parse_index_v1', None)
3227 parse_index_v1 = getattr(mercurial.revlog, 'parse_index_v1', None)
3228 if parse_index_v1 is None:
3228 if parse_index_v1 is None:
3229 parse_index_v1 = mercurial.revlog.revlogio().parseindex
3229 parse_index_v1 = mercurial.revlog.revlogio().parseindex
3230
3230
3231 rllen = len(rl)
3231 rllen = len(rl)
3232
3232
3233 node0 = rl.node(0)
3233 node0 = rl.node(0)
3234 node25 = rl.node(rllen // 4)
3234 node25 = rl.node(rllen // 4)
3235 node50 = rl.node(rllen // 2)
3235 node50 = rl.node(rllen // 2)
3236 node75 = rl.node(rllen // 4 * 3)
3236 node75 = rl.node(rllen // 4 * 3)
3237 node100 = rl.node(rllen - 1)
3237 node100 = rl.node(rllen - 1)
3238
3238
3239 allrevs = range(rllen)
3239 allrevs = range(rllen)
3240 allrevsrev = list(reversed(allrevs))
3240 allrevsrev = list(reversed(allrevs))
3241 allnodes = [rl.node(rev) for rev in range(rllen)]
3241 allnodes = [rl.node(rev) for rev in range(rllen)]
3242 allnodesrev = list(reversed(allnodes))
3242 allnodesrev = list(reversed(allnodes))
3243
3243
3244 def constructor():
3244 def constructor():
3245 if radix is not None:
3245 if radix is not None:
3246 revlog(opener, radix=radix)
3246 revlog(opener, radix=radix)
3247 else:
3247 else:
3248 # hg <= 5.8
3248 # hg <= 5.8
3249 revlog(opener, indexfile=indexfile)
3249 revlog(opener, indexfile=indexfile)
3250
3250
3251 def read():
3251 def read():
3252 with opener(indexfile) as fh:
3252 with opener(indexfile) as fh:
3253 fh.read()
3253 fh.read()
3254
3254
3255 def parseindex():
3255 def parseindex():
3256 parse_index_v1(data, inline)
3256 parse_index_v1(data, inline)
3257
3257
3258 def getentry(revornode):
3258 def getentry(revornode):
3259 index = parse_index_v1(data, inline)[0]
3259 index = parse_index_v1(data, inline)[0]
3260 index[revornode]
3260 index[revornode]
3261
3261
3262 def getentries(revs, count=1):
3262 def getentries(revs, count=1):
3263 index = parse_index_v1(data, inline)[0]
3263 index = parse_index_v1(data, inline)[0]
3264
3264
3265 for i in range(count):
3265 for i in range(count):
3266 for rev in revs:
3266 for rev in revs:
3267 index[rev]
3267 index[rev]
3268
3268
3269 def resolvenode(node):
3269 def resolvenode(node):
3270 index = parse_index_v1(data, inline)[0]
3270 index = parse_index_v1(data, inline)[0]
3271 rev = getattr(index, 'rev', None)
3271 rev = getattr(index, 'rev', None)
3272 if rev is None:
3272 if rev is None:
3273 nodemap = getattr(parse_index_v1(data, inline)[0], 'nodemap', None)
3273 nodemap = getattr(parse_index_v1(data, inline)[0], 'nodemap', None)
3274 # This only works for the C code.
3274 # This only works for the C code.
3275 if nodemap is None:
3275 if nodemap is None:
3276 return
3276 return
3277 rev = nodemap.__getitem__
3277 rev = nodemap.__getitem__
3278
3278
3279 try:
3279 try:
3280 rev(node)
3280 rev(node)
3281 except error.RevlogError:
3281 except error.RevlogError:
3282 pass
3282 pass
3283
3283
3284 def resolvenodes(nodes, count=1):
3284 def resolvenodes(nodes, count=1):
3285 index = parse_index_v1(data, inline)[0]
3285 index = parse_index_v1(data, inline)[0]
3286 rev = getattr(index, 'rev', None)
3286 rev = getattr(index, 'rev', None)
3287 if rev is None:
3287 if rev is None:
3288 nodemap = getattr(parse_index_v1(data, inline)[0], 'nodemap', None)
3288 nodemap = getattr(parse_index_v1(data, inline)[0], 'nodemap', None)
3289 # This only works for the C code.
3289 # This only works for the C code.
3290 if nodemap is None:
3290 if nodemap is None:
3291 return
3291 return
3292 rev = nodemap.__getitem__
3292 rev = nodemap.__getitem__
3293
3293
3294 for i in range(count):
3294 for i in range(count):
3295 for node in nodes:
3295 for node in nodes:
3296 try:
3296 try:
3297 rev(node)
3297 rev(node)
3298 except error.RevlogError:
3298 except error.RevlogError:
3299 pass
3299 pass
3300
3300
3301 benches = [
3301 benches = [
3302 (constructor, b'revlog constructor'),
3302 (constructor, b'revlog constructor'),
3303 (read, b'read'),
3303 (read, b'read'),
3304 (parseindex, b'create index object'),
3304 (parseindex, b'create index object'),
3305 (lambda: getentry(0), b'retrieve index entry for rev 0'),
3305 (lambda: getentry(0), b'retrieve index entry for rev 0'),
3306 (lambda: resolvenode(b'a' * 20), b'look up missing node'),
3306 (lambda: resolvenode(b'a' * 20), b'look up missing node'),
3307 (lambda: resolvenode(node0), b'look up node at rev 0'),
3307 (lambda: resolvenode(node0), b'look up node at rev 0'),
3308 (lambda: resolvenode(node25), b'look up node at 1/4 len'),
3308 (lambda: resolvenode(node25), b'look up node at 1/4 len'),
3309 (lambda: resolvenode(node50), b'look up node at 1/2 len'),
3309 (lambda: resolvenode(node50), b'look up node at 1/2 len'),
3310 (lambda: resolvenode(node75), b'look up node at 3/4 len'),
3310 (lambda: resolvenode(node75), b'look up node at 3/4 len'),
3311 (lambda: resolvenode(node100), b'look up node at tip'),
3311 (lambda: resolvenode(node100), b'look up node at tip'),
3312 # 2x variation is to measure caching impact.
3312 # 2x variation is to measure caching impact.
3313 (lambda: resolvenodes(allnodes), b'look up all nodes (forward)'),
3313 (lambda: resolvenodes(allnodes), b'look up all nodes (forward)'),
3314 (lambda: resolvenodes(allnodes, 2), b'look up all nodes 2x (forward)'),
3314 (lambda: resolvenodes(allnodes, 2), b'look up all nodes 2x (forward)'),
3315 (lambda: resolvenodes(allnodesrev), b'look up all nodes (reverse)'),
3315 (lambda: resolvenodes(allnodesrev), b'look up all nodes (reverse)'),
3316 (
3316 (
3317 lambda: resolvenodes(allnodesrev, 2),
3317 lambda: resolvenodes(allnodesrev, 2),
3318 b'look up all nodes 2x (reverse)',
3318 b'look up all nodes 2x (reverse)',
3319 ),
3319 ),
3320 (lambda: getentries(allrevs), b'retrieve all index entries (forward)'),
3320 (lambda: getentries(allrevs), b'retrieve all index entries (forward)'),
3321 (
3321 (
3322 lambda: getentries(allrevs, 2),
3322 lambda: getentries(allrevs, 2),
3323 b'retrieve all index entries 2x (forward)',
3323 b'retrieve all index entries 2x (forward)',
3324 ),
3324 ),
3325 (
3325 (
3326 lambda: getentries(allrevsrev),
3326 lambda: getentries(allrevsrev),
3327 b'retrieve all index entries (reverse)',
3327 b'retrieve all index entries (reverse)',
3328 ),
3328 ),
3329 (
3329 (
3330 lambda: getentries(allrevsrev, 2),
3330 lambda: getentries(allrevsrev, 2),
3331 b'retrieve all index entries 2x (reverse)',
3331 b'retrieve all index entries 2x (reverse)',
3332 ),
3332 ),
3333 ]
3333 ]
3334
3334
3335 for fn, title in benches:
3335 for fn, title in benches:
3336 timer, fm = gettimer(ui, opts)
3336 timer, fm = gettimer(ui, opts)
3337 timer(fn, title=title)
3337 timer(fn, title=title)
3338 fm.end()
3338 fm.end()
3339
3339
3340
3340
3341 @command(
3341 @command(
3342 b'perf::revlogrevisions|perfrevlogrevisions',
3342 b'perf::revlogrevisions|perfrevlogrevisions',
3343 revlogopts
3343 revlogopts
3344 + formatteropts
3344 + formatteropts
3345 + [
3345 + [
3346 (b'd', b'dist', 100, b'distance between the revisions'),
3346 (b'd', b'dist', 100, b'distance between the revisions'),
3347 (b's', b'startrev', 0, b'revision to start reading at'),
3347 (b's', b'startrev', 0, b'revision to start reading at'),
3348 (b'', b'reverse', False, b'read in reverse'),
3348 (b'', b'reverse', False, b'read in reverse'),
3349 ],
3349 ],
3350 b'-c|-m|FILE',
3350 b'-c|-m|FILE',
3351 )
3351 )
3352 def perfrevlogrevisions(
3352 def perfrevlogrevisions(
3353 ui, repo, file_=None, startrev=0, reverse=False, **opts
3353 ui, repo, file_=None, startrev=0, reverse=False, **opts
3354 ):
3354 ):
3355 """Benchmark reading a series of revisions from a revlog.
3355 """Benchmark reading a series of revisions from a revlog.
3356
3356
3357 By default, we read every ``-d/--dist`` revision from 0 to tip of
3357 By default, we read every ``-d/--dist`` revision from 0 to tip of
3358 the specified revlog.
3358 the specified revlog.
3359
3359
3360 The start revision can be defined via ``-s/--startrev``.
3360 The start revision can be defined via ``-s/--startrev``.
3361 """
3361 """
3362 opts = _byteskwargs(opts)
3362 opts = _byteskwargs(opts)
3363
3363
3364 rl = cmdutil.openrevlog(repo, b'perfrevlogrevisions', file_, opts)
3364 rl = cmdutil.openrevlog(repo, b'perfrevlogrevisions', file_, opts)
3365 rllen = getlen(ui)(rl)
3365 rllen = getlen(ui)(rl)
3366
3366
3367 if startrev < 0:
3367 if startrev < 0:
3368 startrev = rllen + startrev
3368 startrev = rllen + startrev
3369
3369
3370 def d():
3370 def d():
3371 rl.clearcaches()
3371 rl.clearcaches()
3372
3372
3373 beginrev = startrev
3373 beginrev = startrev
3374 endrev = rllen
3374 endrev = rllen
3375 dist = opts[b'dist']
3375 dist = opts[b'dist']
3376
3376
3377 if reverse:
3377 if reverse:
3378 beginrev, endrev = endrev - 1, beginrev - 1
3378 beginrev, endrev = endrev - 1, beginrev - 1
3379 dist = -1 * dist
3379 dist = -1 * dist
3380
3380
3381 for x in _xrange(beginrev, endrev, dist):
3381 for x in _xrange(beginrev, endrev, dist):
3382 # Old revisions don't support passing int.
3382 # Old revisions don't support passing int.
3383 n = rl.node(x)
3383 n = rl.node(x)
3384 rl.revision(n)
3384 rl.revision(n)
3385
3385
3386 timer, fm = gettimer(ui, opts)
3386 timer, fm = gettimer(ui, opts)
3387 timer(d)
3387 timer(d)
3388 fm.end()
3388 fm.end()
3389
3389
3390
3390
3391 @command(
3391 @command(
3392 b'perf::revlogwrite|perfrevlogwrite',
3392 b'perf::revlogwrite|perfrevlogwrite',
3393 revlogopts
3393 revlogopts
3394 + formatteropts
3394 + formatteropts
3395 + [
3395 + [
3396 (b's', b'startrev', 1000, b'revision to start writing at'),
3396 (b's', b'startrev', 1000, b'revision to start writing at'),
3397 (b'', b'stoprev', -1, b'last revision to write'),
3397 (b'', b'stoprev', -1, b'last revision to write'),
3398 (b'', b'count', 3, b'number of passes to perform'),
3398 (b'', b'count', 3, b'number of passes to perform'),
3399 (b'', b'details', False, b'print timing for every revisions tested'),
3399 (b'', b'details', False, b'print timing for every revisions tested'),
3400 (b'', b'source', b'full', b'the kind of data feed in the revlog'),
3400 (b'', b'source', b'full', b'the kind of data feed in the revlog'),
3401 (b'', b'lazydeltabase', True, b'try the provided delta first'),
3401 (b'', b'lazydeltabase', True, b'try the provided delta first'),
3402 (b'', b'clear-caches', True, b'clear revlog cache between calls'),
3402 (b'', b'clear-caches', True, b'clear revlog cache between calls'),
3403 ],
3403 ],
3404 b'-c|-m|FILE',
3404 b'-c|-m|FILE',
3405 )
3405 )
3406 def perfrevlogwrite(ui, repo, file_=None, startrev=1000, stoprev=-1, **opts):
3406 def perfrevlogwrite(ui, repo, file_=None, startrev=1000, stoprev=-1, **opts):
3407 """Benchmark writing a series of revisions to a revlog.
3407 """Benchmark writing a series of revisions to a revlog.
3408
3408
3409 Possible source values are:
3409 Possible source values are:
3410 * `full`: add from a full text (default).
3410 * `full`: add from a full text (default).
3411 * `parent-1`: add from a delta to the first parent
3411 * `parent-1`: add from a delta to the first parent
3412 * `parent-2`: add from a delta to the second parent if it exists
3412 * `parent-2`: add from a delta to the second parent if it exists
3413 (use a delta from the first parent otherwise)
3413 (use a delta from the first parent otherwise)
3414 * `parent-smallest`: add from the smallest delta (either p1 or p2)
3414 * `parent-smallest`: add from the smallest delta (either p1 or p2)
3415 * `storage`: add from the existing precomputed deltas
3415 * `storage`: add from the existing precomputed deltas
3416
3416
3417 Note: This performance command measures performance in a custom way. As a
3417 Note: This performance command measures performance in a custom way. As a
3418 result some of the global configuration of the 'perf' command does not
3418 result some of the global configuration of the 'perf' command does not
3419 apply to it:
3419 apply to it:
3420
3420
3421 * ``pre-run``: disabled
3421 * ``pre-run``: disabled
3422
3422
3423 * ``profile-benchmark``: disabled
3423 * ``profile-benchmark``: disabled
3424
3424
3425 * ``run-limits``: disabled use --count instead
3425 * ``run-limits``: disabled use --count instead
3426 """
3426 """
3427 opts = _byteskwargs(opts)
3427 opts = _byteskwargs(opts)
3428
3428
3429 rl = cmdutil.openrevlog(repo, b'perfrevlogwrite', file_, opts)
3429 rl = cmdutil.openrevlog(repo, b'perfrevlogwrite', file_, opts)
3430 rllen = getlen(ui)(rl)
3430 rllen = getlen(ui)(rl)
3431 if startrev < 0:
3431 if startrev < 0:
3432 startrev = rllen + startrev
3432 startrev = rllen + startrev
3433 if stoprev < 0:
3433 if stoprev < 0:
3434 stoprev = rllen + stoprev
3434 stoprev = rllen + stoprev
3435
3435
3436 lazydeltabase = opts['lazydeltabase']
3436 lazydeltabase = opts['lazydeltabase']
3437 source = opts['source']
3437 source = opts['source']
3438 clearcaches = opts['clear_caches']
3438 clearcaches = opts['clear_caches']
3439 validsource = (
3439 validsource = (
3440 b'full',
3440 b'full',
3441 b'parent-1',
3441 b'parent-1',
3442 b'parent-2',
3442 b'parent-2',
3443 b'parent-smallest',
3443 b'parent-smallest',
3444 b'storage',
3444 b'storage',
3445 )
3445 )
3446 if source not in validsource:
3446 if source not in validsource:
3447 raise error.Abort('invalid source type: %s' % source)
3447 raise error.Abort('invalid source type: %s' % source)
3448
3448
3449 ### actually gather results
3449 ### actually gather results
3450 count = opts['count']
3450 count = opts['count']
3451 if count <= 0:
3451 if count <= 0:
3452 raise error.Abort('invalide run count: %d' % count)
3452 raise error.Abort('invalide run count: %d' % count)
3453 allresults = []
3453 allresults = []
3454 for c in range(count):
3454 for c in range(count):
3455 timing = _timeonewrite(
3455 timing = _timeonewrite(
3456 ui,
3456 ui,
3457 rl,
3457 rl,
3458 source,
3458 source,
3459 startrev,
3459 startrev,
3460 stoprev,
3460 stoprev,
3461 c + 1,
3461 c + 1,
3462 lazydeltabase=lazydeltabase,
3462 lazydeltabase=lazydeltabase,
3463 clearcaches=clearcaches,
3463 clearcaches=clearcaches,
3464 )
3464 )
3465 allresults.append(timing)
3465 allresults.append(timing)
3466
3466
3467 ### consolidate the results in a single list
3467 ### consolidate the results in a single list
3468 results = []
3468 results = []
3469 for idx, (rev, t) in enumerate(allresults[0]):
3469 for idx, (rev, t) in enumerate(allresults[0]):
3470 ts = [t]
3470 ts = [t]
3471 for other in allresults[1:]:
3471 for other in allresults[1:]:
3472 orev, ot = other[idx]
3472 orev, ot = other[idx]
3473 assert orev == rev
3473 assert orev == rev
3474 ts.append(ot)
3474 ts.append(ot)
3475 results.append((rev, ts))
3475 results.append((rev, ts))
3476 resultcount = len(results)
3476 resultcount = len(results)
3477
3477
3478 ### Compute and display relevant statistics
3478 ### Compute and display relevant statistics
3479
3479
3480 # get a formatter
3480 # get a formatter
3481 fm = ui.formatter(b'perf', opts)
3481 fm = ui.formatter(b'perf', opts)
3482 displayall = ui.configbool(b"perf", b"all-timing", True)
3482 displayall = ui.configbool(b"perf", b"all-timing", True)
3483
3483
3484 # print individual details if requested
3484 # print individual details if requested
3485 if opts['details']:
3485 if opts['details']:
3486 for idx, item in enumerate(results, 1):
3486 for idx, item in enumerate(results, 1):
3487 rev, data = item
3487 rev, data = item
3488 title = 'revisions #%d of %d, rev %d' % (idx, resultcount, rev)
3488 title = 'revisions #%d of %d, rev %d' % (idx, resultcount, rev)
3489 formatone(fm, data, title=title, displayall=displayall)
3489 formatone(fm, data, title=title, displayall=displayall)
3490
3490
3491 # sorts results by median time
3491 # sorts results by median time
3492 results.sort(key=lambda x: sorted(x[1])[len(x[1]) // 2])
3492 results.sort(key=lambda x: sorted(x[1])[len(x[1]) // 2])
3493 # list of (name, index) to display)
3493 # list of (name, index) to display)
3494 relevants = [
3494 relevants = [
3495 ("min", 0),
3495 ("min", 0),
3496 ("10%", resultcount * 10 // 100),
3496 ("10%", resultcount * 10 // 100),
3497 ("25%", resultcount * 25 // 100),
3497 ("25%", resultcount * 25 // 100),
3498 ("50%", resultcount * 70 // 100),
3498 ("50%", resultcount * 70 // 100),
3499 ("75%", resultcount * 75 // 100),
3499 ("75%", resultcount * 75 // 100),
3500 ("90%", resultcount * 90 // 100),
3500 ("90%", resultcount * 90 // 100),
3501 ("95%", resultcount * 95 // 100),
3501 ("95%", resultcount * 95 // 100),
3502 ("99%", resultcount * 99 // 100),
3502 ("99%", resultcount * 99 // 100),
3503 ("99.9%", resultcount * 999 // 1000),
3503 ("99.9%", resultcount * 999 // 1000),
3504 ("99.99%", resultcount * 9999 // 10000),
3504 ("99.99%", resultcount * 9999 // 10000),
3505 ("99.999%", resultcount * 99999 // 100000),
3505 ("99.999%", resultcount * 99999 // 100000),
3506 ("max", -1),
3506 ("max", -1),
3507 ]
3507 ]
3508 if not ui.quiet:
3508 if not ui.quiet:
3509 for name, idx in relevants:
3509 for name, idx in relevants:
3510 data = results[idx]
3510 data = results[idx]
3511 title = '%s of %d, rev %d' % (name, resultcount, data[0])
3511 title = '%s of %d, rev %d' % (name, resultcount, data[0])
3512 formatone(fm, data[1], title=title, displayall=displayall)
3512 formatone(fm, data[1], title=title, displayall=displayall)
3513
3513
3514 # XXX summing that many float will not be very precise, we ignore this fact
3514 # XXX summing that many float will not be very precise, we ignore this fact
3515 # for now
3515 # for now
3516 totaltime = []
3516 totaltime = []
3517 for item in allresults:
3517 for item in allresults:
3518 totaltime.append(
3518 totaltime.append(
3519 (
3519 (
3520 sum(x[1][0] for x in item),
3520 sum(x[1][0] for x in item),
3521 sum(x[1][1] for x in item),
3521 sum(x[1][1] for x in item),
3522 sum(x[1][2] for x in item),
3522 sum(x[1][2] for x in item),
3523 )
3523 )
3524 )
3524 )
3525 formatone(
3525 formatone(
3526 fm,
3526 fm,
3527 totaltime,
3527 totaltime,
3528 title="total time (%d revs)" % resultcount,
3528 title="total time (%d revs)" % resultcount,
3529 displayall=displayall,
3529 displayall=displayall,
3530 )
3530 )
3531 fm.end()
3531 fm.end()
3532
3532
3533
3533
3534 class _faketr:
3534 class _faketr:
3535 def add(s, x, y, z=None):
3535 def add(s, x, y, z=None):
3536 return None
3536 return None
3537
3537
3538
3538
3539 def _timeonewrite(
3539 def _timeonewrite(
3540 ui,
3540 ui,
3541 orig,
3541 orig,
3542 source,
3542 source,
3543 startrev,
3543 startrev,
3544 stoprev,
3544 stoprev,
3545 runidx=None,
3545 runidx=None,
3546 lazydeltabase=True,
3546 lazydeltabase=True,
3547 clearcaches=True,
3547 clearcaches=True,
3548 ):
3548 ):
3549 timings = []
3549 timings = []
3550 tr = _faketr()
3550 tr = _faketr()
3551 with _temprevlog(ui, orig, startrev) as dest:
3551 with _temprevlog(ui, orig, startrev) as dest:
3552 dest._lazydeltabase = lazydeltabase
3552 if hasattr(dest, "delta_config"):
3553 dest.delta_config.lazy_delta_base = lazydeltabase
3554 else:
3555 dest._lazydeltabase = lazydeltabase
3553 revs = list(orig.revs(startrev, stoprev))
3556 revs = list(orig.revs(startrev, stoprev))
3554 total = len(revs)
3557 total = len(revs)
3555 topic = 'adding'
3558 topic = 'adding'
3556 if runidx is not None:
3559 if runidx is not None:
3557 topic += ' (run #%d)' % runidx
3560 topic += ' (run #%d)' % runidx
3558 # Support both old and new progress API
3561 # Support both old and new progress API
3559 if util.safehasattr(ui, 'makeprogress'):
3562 if util.safehasattr(ui, 'makeprogress'):
3560 progress = ui.makeprogress(topic, unit='revs', total=total)
3563 progress = ui.makeprogress(topic, unit='revs', total=total)
3561
3564
3562 def updateprogress(pos):
3565 def updateprogress(pos):
3563 progress.update(pos)
3566 progress.update(pos)
3564
3567
3565 def completeprogress():
3568 def completeprogress():
3566 progress.complete()
3569 progress.complete()
3567
3570
3568 else:
3571 else:
3569
3572
3570 def updateprogress(pos):
3573 def updateprogress(pos):
3571 ui.progress(topic, pos, unit='revs', total=total)
3574 ui.progress(topic, pos, unit='revs', total=total)
3572
3575
3573 def completeprogress():
3576 def completeprogress():
3574 ui.progress(topic, None, unit='revs', total=total)
3577 ui.progress(topic, None, unit='revs', total=total)
3575
3578
3576 for idx, rev in enumerate(revs):
3579 for idx, rev in enumerate(revs):
3577 updateprogress(idx)
3580 updateprogress(idx)
3578 addargs, addkwargs = _getrevisionseed(orig, rev, tr, source)
3581 addargs, addkwargs = _getrevisionseed(orig, rev, tr, source)
3579 if clearcaches:
3582 if clearcaches:
3580 dest.index.clearcaches()
3583 dest.index.clearcaches()
3581 dest.clearcaches()
3584 dest.clearcaches()
3582 with timeone() as r:
3585 with timeone() as r:
3583 dest.addrawrevision(*addargs, **addkwargs)
3586 dest.addrawrevision(*addargs, **addkwargs)
3584 timings.append((rev, r[0]))
3587 timings.append((rev, r[0]))
3585 updateprogress(total)
3588 updateprogress(total)
3586 completeprogress()
3589 completeprogress()
3587 return timings
3590 return timings
3588
3591
3589
3592
3590 def _getrevisionseed(orig, rev, tr, source):
3593 def _getrevisionseed(orig, rev, tr, source):
3591 from mercurial.node import nullid
3594 from mercurial.node import nullid
3592
3595
3593 linkrev = orig.linkrev(rev)
3596 linkrev = orig.linkrev(rev)
3594 node = orig.node(rev)
3597 node = orig.node(rev)
3595 p1, p2 = orig.parents(node)
3598 p1, p2 = orig.parents(node)
3596 flags = orig.flags(rev)
3599 flags = orig.flags(rev)
3597 cachedelta = None
3600 cachedelta = None
3598 text = None
3601 text = None
3599
3602
3600 if source == b'full':
3603 if source == b'full':
3601 text = orig.revision(rev)
3604 text = orig.revision(rev)
3602 elif source == b'parent-1':
3605 elif source == b'parent-1':
3603 baserev = orig.rev(p1)
3606 baserev = orig.rev(p1)
3604 cachedelta = (baserev, orig.revdiff(p1, rev))
3607 cachedelta = (baserev, orig.revdiff(p1, rev))
3605 elif source == b'parent-2':
3608 elif source == b'parent-2':
3606 parent = p2
3609 parent = p2
3607 if p2 == nullid:
3610 if p2 == nullid:
3608 parent = p1
3611 parent = p1
3609 baserev = orig.rev(parent)
3612 baserev = orig.rev(parent)
3610 cachedelta = (baserev, orig.revdiff(parent, rev))
3613 cachedelta = (baserev, orig.revdiff(parent, rev))
3611 elif source == b'parent-smallest':
3614 elif source == b'parent-smallest':
3612 p1diff = orig.revdiff(p1, rev)
3615 p1diff = orig.revdiff(p1, rev)
3613 parent = p1
3616 parent = p1
3614 diff = p1diff
3617 diff = p1diff
3615 if p2 != nullid:
3618 if p2 != nullid:
3616 p2diff = orig.revdiff(p2, rev)
3619 p2diff = orig.revdiff(p2, rev)
3617 if len(p1diff) > len(p2diff):
3620 if len(p1diff) > len(p2diff):
3618 parent = p2
3621 parent = p2
3619 diff = p2diff
3622 diff = p2diff
3620 baserev = orig.rev(parent)
3623 baserev = orig.rev(parent)
3621 cachedelta = (baserev, diff)
3624 cachedelta = (baserev, diff)
3622 elif source == b'storage':
3625 elif source == b'storage':
3623 baserev = orig.deltaparent(rev)
3626 baserev = orig.deltaparent(rev)
3624 cachedelta = (baserev, orig.revdiff(orig.node(baserev), rev))
3627 cachedelta = (baserev, orig.revdiff(orig.node(baserev), rev))
3625
3628
3626 return (
3629 return (
3627 (text, tr, linkrev, p1, p2),
3630 (text, tr, linkrev, p1, p2),
3628 {'node': node, 'flags': flags, 'cachedelta': cachedelta},
3631 {'node': node, 'flags': flags, 'cachedelta': cachedelta},
3629 )
3632 )
3630
3633
3631
3634
3632 @contextlib.contextmanager
3635 @contextlib.contextmanager
3633 def _temprevlog(ui, orig, truncaterev):
3636 def _temprevlog(ui, orig, truncaterev):
3634 from mercurial import vfs as vfsmod
3637 from mercurial import vfs as vfsmod
3635
3638
3636 if orig._inline:
3639 if orig._inline:
3637 raise error.Abort('not supporting inline revlog (yet)')
3640 raise error.Abort('not supporting inline revlog (yet)')
3638 revlogkwargs = {}
3641 revlogkwargs = {}
3639 k = 'upperboundcomp'
3642 k = 'upperboundcomp'
3640 if util.safehasattr(orig, k):
3643 if util.safehasattr(orig, k):
3641 revlogkwargs[k] = getattr(orig, k)
3644 revlogkwargs[k] = getattr(orig, k)
3642
3645
3643 indexfile = getattr(orig, '_indexfile', None)
3646 indexfile = getattr(orig, '_indexfile', None)
3644 if indexfile is None:
3647 if indexfile is None:
3645 # compatibility with <= hg-5.8
3648 # compatibility with <= hg-5.8
3646 indexfile = getattr(orig, 'indexfile')
3649 indexfile = getattr(orig, 'indexfile')
3647 origindexpath = orig.opener.join(indexfile)
3650 origindexpath = orig.opener.join(indexfile)
3648
3651
3649 datafile = getattr(orig, '_datafile', getattr(orig, 'datafile'))
3652 datafile = getattr(orig, '_datafile', getattr(orig, 'datafile'))
3650 origdatapath = orig.opener.join(datafile)
3653 origdatapath = orig.opener.join(datafile)
3651 radix = b'revlog'
3654 radix = b'revlog'
3652 indexname = b'revlog.i'
3655 indexname = b'revlog.i'
3653 dataname = b'revlog.d'
3656 dataname = b'revlog.d'
3654
3657
3655 tmpdir = tempfile.mkdtemp(prefix='tmp-hgperf-')
3658 tmpdir = tempfile.mkdtemp(prefix='tmp-hgperf-')
3656 try:
3659 try:
3657 # copy the data file in a temporary directory
3660 # copy the data file in a temporary directory
3658 ui.debug('copying data in %s\n' % tmpdir)
3661 ui.debug('copying data in %s\n' % tmpdir)
3659 destindexpath = os.path.join(tmpdir, 'revlog.i')
3662 destindexpath = os.path.join(tmpdir, 'revlog.i')
3660 destdatapath = os.path.join(tmpdir, 'revlog.d')
3663 destdatapath = os.path.join(tmpdir, 'revlog.d')
3661 shutil.copyfile(origindexpath, destindexpath)
3664 shutil.copyfile(origindexpath, destindexpath)
3662 shutil.copyfile(origdatapath, destdatapath)
3665 shutil.copyfile(origdatapath, destdatapath)
3663
3666
3664 # remove the data we want to add again
3667 # remove the data we want to add again
3665 ui.debug('truncating data to be rewritten\n')
3668 ui.debug('truncating data to be rewritten\n')
3666 with open(destindexpath, 'ab') as index:
3669 with open(destindexpath, 'ab') as index:
3667 index.seek(0)
3670 index.seek(0)
3668 index.truncate(truncaterev * orig._io.size)
3671 index.truncate(truncaterev * orig._io.size)
3669 with open(destdatapath, 'ab') as data:
3672 with open(destdatapath, 'ab') as data:
3670 data.seek(0)
3673 data.seek(0)
3671 data.truncate(orig.start(truncaterev))
3674 data.truncate(orig.start(truncaterev))
3672
3675
3673 # instantiate a new revlog from the temporary copy
3676 # instantiate a new revlog from the temporary copy
3674 ui.debug('truncating adding to be rewritten\n')
3677 ui.debug('truncating adding to be rewritten\n')
3675 vfs = vfsmod.vfs(tmpdir)
3678 vfs = vfsmod.vfs(tmpdir)
3676 vfs.options = getattr(orig.opener, 'options', None)
3679 vfs.options = getattr(orig.opener, 'options', None)
3677
3680
3678 try:
3681 try:
3679 dest = revlog(vfs, radix=radix, **revlogkwargs)
3682 dest = revlog(vfs, radix=radix, **revlogkwargs)
3680 except TypeError:
3683 except TypeError:
3681 dest = revlog(
3684 dest = revlog(
3682 vfs, indexfile=indexname, datafile=dataname, **revlogkwargs
3685 vfs, indexfile=indexname, datafile=dataname, **revlogkwargs
3683 )
3686 )
3684 if dest._inline:
3687 if dest._inline:
3685 raise error.Abort('not supporting inline revlog (yet)')
3688 raise error.Abort('not supporting inline revlog (yet)')
3686 # make sure internals are initialized
3689 # make sure internals are initialized
3687 dest.revision(len(dest) - 1)
3690 dest.revision(len(dest) - 1)
3688 yield dest
3691 yield dest
3689 del dest, vfs
3692 del dest, vfs
3690 finally:
3693 finally:
3691 shutil.rmtree(tmpdir, True)
3694 shutil.rmtree(tmpdir, True)
3692
3695
3693
3696
3694 @command(
3697 @command(
3695 b'perf::revlogchunks|perfrevlogchunks',
3698 b'perf::revlogchunks|perfrevlogchunks',
3696 revlogopts
3699 revlogopts
3697 + formatteropts
3700 + formatteropts
3698 + [
3701 + [
3699 (b'e', b'engines', b'', b'compression engines to use'),
3702 (b'e', b'engines', b'', b'compression engines to use'),
3700 (b's', b'startrev', 0, b'revision to start at'),
3703 (b's', b'startrev', 0, b'revision to start at'),
3701 ],
3704 ],
3702 b'-c|-m|FILE',
3705 b'-c|-m|FILE',
3703 )
3706 )
3704 def perfrevlogchunks(ui, repo, file_=None, engines=None, startrev=0, **opts):
3707 def perfrevlogchunks(ui, repo, file_=None, engines=None, startrev=0, **opts):
3705 """Benchmark operations on revlog chunks.
3708 """Benchmark operations on revlog chunks.
3706
3709
3707 Logically, each revlog is a collection of fulltext revisions. However,
3710 Logically, each revlog is a collection of fulltext revisions. However,
3708 stored within each revlog are "chunks" of possibly compressed data. This
3711 stored within each revlog are "chunks" of possibly compressed data. This
3709 data needs to be read and decompressed or compressed and written.
3712 data needs to be read and decompressed or compressed and written.
3710
3713
3711 This command measures the time it takes to read+decompress and recompress
3714 This command measures the time it takes to read+decompress and recompress
3712 chunks in a revlog. It effectively isolates I/O and compression performance.
3715 chunks in a revlog. It effectively isolates I/O and compression performance.
3713 For measurements of higher-level operations like resolving revisions,
3716 For measurements of higher-level operations like resolving revisions,
3714 see ``perfrevlogrevisions`` and ``perfrevlogrevision``.
3717 see ``perfrevlogrevisions`` and ``perfrevlogrevision``.
3715 """
3718 """
3716 opts = _byteskwargs(opts)
3719 opts = _byteskwargs(opts)
3717
3720
3718 rl = cmdutil.openrevlog(repo, b'perfrevlogchunks', file_, opts)
3721 rl = cmdutil.openrevlog(repo, b'perfrevlogchunks', file_, opts)
3719
3722
3720 # _chunkraw was renamed to _getsegmentforrevs.
3723 # _chunkraw was renamed to _getsegmentforrevs.
3721 try:
3724 try:
3722 segmentforrevs = rl._getsegmentforrevs
3725 segmentforrevs = rl._getsegmentforrevs
3723 except AttributeError:
3726 except AttributeError:
3724 segmentforrevs = rl._chunkraw
3727 segmentforrevs = rl._chunkraw
3725
3728
3726 # Verify engines argument.
3729 # Verify engines argument.
3727 if engines:
3730 if engines:
3728 engines = {e.strip() for e in engines.split(b',')}
3731 engines = {e.strip() for e in engines.split(b',')}
3729 for engine in engines:
3732 for engine in engines:
3730 try:
3733 try:
3731 util.compressionengines[engine]
3734 util.compressionengines[engine]
3732 except KeyError:
3735 except KeyError:
3733 raise error.Abort(b'unknown compression engine: %s' % engine)
3736 raise error.Abort(b'unknown compression engine: %s' % engine)
3734 else:
3737 else:
3735 engines = []
3738 engines = []
3736 for e in util.compengines:
3739 for e in util.compengines:
3737 engine = util.compengines[e]
3740 engine = util.compengines[e]
3738 try:
3741 try:
3739 if engine.available():
3742 if engine.available():
3740 engine.revlogcompressor().compress(b'dummy')
3743 engine.revlogcompressor().compress(b'dummy')
3741 engines.append(e)
3744 engines.append(e)
3742 except NotImplementedError:
3745 except NotImplementedError:
3743 pass
3746 pass
3744
3747
3745 revs = list(rl.revs(startrev, len(rl) - 1))
3748 revs = list(rl.revs(startrev, len(rl) - 1))
3746
3749
3747 @contextlib.contextmanager
3750 @contextlib.contextmanager
3748 def reading(rl):
3751 def reading(rl):
3749 if getattr(rl, 'reading', None) is not None:
3752 if getattr(rl, 'reading', None) is not None:
3750 with rl.reading():
3753 with rl.reading():
3751 yield None
3754 yield None
3752 elif rl._inline:
3755 elif rl._inline:
3753 indexfile = getattr(rl, '_indexfile', None)
3756 indexfile = getattr(rl, '_indexfile', None)
3754 if indexfile is None:
3757 if indexfile is None:
3755 # compatibility with <= hg-5.8
3758 # compatibility with <= hg-5.8
3756 indexfile = getattr(rl, 'indexfile')
3759 indexfile = getattr(rl, 'indexfile')
3757 yield getsvfs(repo)(indexfile)
3760 yield getsvfs(repo)(indexfile)
3758 else:
3761 else:
3759 datafile = getattr(rl, 'datafile', getattr(rl, 'datafile'))
3762 datafile = getattr(rl, 'datafile', getattr(rl, 'datafile'))
3760 yield getsvfs(repo)(datafile)
3763 yield getsvfs(repo)(datafile)
3761
3764
3762 if getattr(rl, 'reading', None) is not None:
3765 if getattr(rl, 'reading', None) is not None:
3763
3766
3764 @contextlib.contextmanager
3767 @contextlib.contextmanager
3765 def lazy_reading(rl):
3768 def lazy_reading(rl):
3766 with rl.reading():
3769 with rl.reading():
3767 yield
3770 yield
3768
3771
3769 else:
3772 else:
3770
3773
3771 @contextlib.contextmanager
3774 @contextlib.contextmanager
3772 def lazy_reading(rl):
3775 def lazy_reading(rl):
3773 yield
3776 yield
3774
3777
3775 def doread():
3778 def doread():
3776 rl.clearcaches()
3779 rl.clearcaches()
3777 for rev in revs:
3780 for rev in revs:
3778 with lazy_reading(rl):
3781 with lazy_reading(rl):
3779 segmentforrevs(rev, rev)
3782 segmentforrevs(rev, rev)
3780
3783
3781 def doreadcachedfh():
3784 def doreadcachedfh():
3782 rl.clearcaches()
3785 rl.clearcaches()
3783 with reading(rl) as fh:
3786 with reading(rl) as fh:
3784 if fh is not None:
3787 if fh is not None:
3785 for rev in revs:
3788 for rev in revs:
3786 segmentforrevs(rev, rev, df=fh)
3789 segmentforrevs(rev, rev, df=fh)
3787 else:
3790 else:
3788 for rev in revs:
3791 for rev in revs:
3789 segmentforrevs(rev, rev)
3792 segmentforrevs(rev, rev)
3790
3793
3791 def doreadbatch():
3794 def doreadbatch():
3792 rl.clearcaches()
3795 rl.clearcaches()
3793 with lazy_reading(rl):
3796 with lazy_reading(rl):
3794 segmentforrevs(revs[0], revs[-1])
3797 segmentforrevs(revs[0], revs[-1])
3795
3798
3796 def doreadbatchcachedfh():
3799 def doreadbatchcachedfh():
3797 rl.clearcaches()
3800 rl.clearcaches()
3798 with reading(rl) as fh:
3801 with reading(rl) as fh:
3799 if fh is not None:
3802 if fh is not None:
3800 segmentforrevs(revs[0], revs[-1], df=fh)
3803 segmentforrevs(revs[0], revs[-1], df=fh)
3801 else:
3804 else:
3802 segmentforrevs(revs[0], revs[-1])
3805 segmentforrevs(revs[0], revs[-1])
3803
3806
3804 def dochunk():
3807 def dochunk():
3805 rl.clearcaches()
3808 rl.clearcaches()
3806 with reading(rl) as fh:
3809 with reading(rl) as fh:
3807 if fh is not None:
3810 if fh is not None:
3808 for rev in revs:
3811 for rev in revs:
3809 rl._chunk(rev, df=fh)
3812 rl._chunk(rev, df=fh)
3810 else:
3813 else:
3811 for rev in revs:
3814 for rev in revs:
3812 rl._chunk(rev)
3815 rl._chunk(rev)
3813
3816
3814 chunks = [None]
3817 chunks = [None]
3815
3818
3816 def dochunkbatch():
3819 def dochunkbatch():
3817 rl.clearcaches()
3820 rl.clearcaches()
3818 with reading(rl) as fh:
3821 with reading(rl) as fh:
3819 if fh is not None:
3822 if fh is not None:
3820 # Save chunks as a side-effect.
3823 # Save chunks as a side-effect.
3821 chunks[0] = rl._chunks(revs, df=fh)
3824 chunks[0] = rl._chunks(revs, df=fh)
3822 else:
3825 else:
3823 # Save chunks as a side-effect.
3826 # Save chunks as a side-effect.
3824 chunks[0] = rl._chunks(revs)
3827 chunks[0] = rl._chunks(revs)
3825
3828
3826 def docompress(compressor):
3829 def docompress(compressor):
3827 rl.clearcaches()
3830 rl.clearcaches()
3828
3831
3829 try:
3832 try:
3830 # Swap in the requested compression engine.
3833 # Swap in the requested compression engine.
3831 oldcompressor = rl._compressor
3834 oldcompressor = rl._compressor
3832 rl._compressor = compressor
3835 rl._compressor = compressor
3833 for chunk in chunks[0]:
3836 for chunk in chunks[0]:
3834 rl.compress(chunk)
3837 rl.compress(chunk)
3835 finally:
3838 finally:
3836 rl._compressor = oldcompressor
3839 rl._compressor = oldcompressor
3837
3840
3838 benches = [
3841 benches = [
3839 (lambda: doread(), b'read'),
3842 (lambda: doread(), b'read'),
3840 (lambda: doreadcachedfh(), b'read w/ reused fd'),
3843 (lambda: doreadcachedfh(), b'read w/ reused fd'),
3841 (lambda: doreadbatch(), b'read batch'),
3844 (lambda: doreadbatch(), b'read batch'),
3842 (lambda: doreadbatchcachedfh(), b'read batch w/ reused fd'),
3845 (lambda: doreadbatchcachedfh(), b'read batch w/ reused fd'),
3843 (lambda: dochunk(), b'chunk'),
3846 (lambda: dochunk(), b'chunk'),
3844 (lambda: dochunkbatch(), b'chunk batch'),
3847 (lambda: dochunkbatch(), b'chunk batch'),
3845 ]
3848 ]
3846
3849
3847 for engine in sorted(engines):
3850 for engine in sorted(engines):
3848 compressor = util.compengines[engine].revlogcompressor()
3851 compressor = util.compengines[engine].revlogcompressor()
3849 benches.append(
3852 benches.append(
3850 (
3853 (
3851 functools.partial(docompress, compressor),
3854 functools.partial(docompress, compressor),
3852 b'compress w/ %s' % engine,
3855 b'compress w/ %s' % engine,
3853 )
3856 )
3854 )
3857 )
3855
3858
3856 for fn, title in benches:
3859 for fn, title in benches:
3857 timer, fm = gettimer(ui, opts)
3860 timer, fm = gettimer(ui, opts)
3858 timer(fn, title=title)
3861 timer(fn, title=title)
3859 fm.end()
3862 fm.end()
3860
3863
3861
3864
3862 @command(
3865 @command(
3863 b'perf::revlogrevision|perfrevlogrevision',
3866 b'perf::revlogrevision|perfrevlogrevision',
3864 revlogopts
3867 revlogopts
3865 + formatteropts
3868 + formatteropts
3866 + [(b'', b'cache', False, b'use caches instead of clearing')],
3869 + [(b'', b'cache', False, b'use caches instead of clearing')],
3867 b'-c|-m|FILE REV',
3870 b'-c|-m|FILE REV',
3868 )
3871 )
3869 def perfrevlogrevision(ui, repo, file_, rev=None, cache=None, **opts):
3872 def perfrevlogrevision(ui, repo, file_, rev=None, cache=None, **opts):
3870 """Benchmark obtaining a revlog revision.
3873 """Benchmark obtaining a revlog revision.
3871
3874
3872 Obtaining a revlog revision consists of roughly the following steps:
3875 Obtaining a revlog revision consists of roughly the following steps:
3873
3876
3874 1. Compute the delta chain
3877 1. Compute the delta chain
3875 2. Slice the delta chain if applicable
3878 2. Slice the delta chain if applicable
3876 3. Obtain the raw chunks for that delta chain
3879 3. Obtain the raw chunks for that delta chain
3877 4. Decompress each raw chunk
3880 4. Decompress each raw chunk
3878 5. Apply binary patches to obtain fulltext
3881 5. Apply binary patches to obtain fulltext
3879 6. Verify hash of fulltext
3882 6. Verify hash of fulltext
3880
3883
3881 This command measures the time spent in each of these phases.
3884 This command measures the time spent in each of these phases.
3882 """
3885 """
3883 opts = _byteskwargs(opts)
3886 opts = _byteskwargs(opts)
3884
3887
3885 if opts.get(b'changelog') or opts.get(b'manifest'):
3888 if opts.get(b'changelog') or opts.get(b'manifest'):
3886 file_, rev = None, file_
3889 file_, rev = None, file_
3887 elif rev is None:
3890 elif rev is None:
3888 raise error.CommandError(b'perfrevlogrevision', b'invalid arguments')
3891 raise error.CommandError(b'perfrevlogrevision', b'invalid arguments')
3889
3892
3890 r = cmdutil.openrevlog(repo, b'perfrevlogrevision', file_, opts)
3893 r = cmdutil.openrevlog(repo, b'perfrevlogrevision', file_, opts)
3891
3894
3892 # _chunkraw was renamed to _getsegmentforrevs.
3895 # _chunkraw was renamed to _getsegmentforrevs.
3893 try:
3896 try:
3894 segmentforrevs = r._getsegmentforrevs
3897 segmentforrevs = r._getsegmentforrevs
3895 except AttributeError:
3898 except AttributeError:
3896 segmentforrevs = r._chunkraw
3899 segmentforrevs = r._chunkraw
3897
3900
3898 node = r.lookup(rev)
3901 node = r.lookup(rev)
3899 rev = r.rev(node)
3902 rev = r.rev(node)
3900
3903
3901 if getattr(r, 'reading', None) is not None:
3904 if getattr(r, 'reading', None) is not None:
3902
3905
3903 @contextlib.contextmanager
3906 @contextlib.contextmanager
3904 def lazy_reading(r):
3907 def lazy_reading(r):
3905 with r.reading():
3908 with r.reading():
3906 yield
3909 yield
3907
3910
3908 else:
3911 else:
3909
3912
3910 @contextlib.contextmanager
3913 @contextlib.contextmanager
3911 def lazy_reading(r):
3914 def lazy_reading(r):
3912 yield
3915 yield
3913
3916
3914 def getrawchunks(data, chain):
3917 def getrawchunks(data, chain):
3915 start = r.start
3918 start = r.start
3916 length = r.length
3919 length = r.length
3917 inline = r._inline
3920 inline = r._inline
3918 try:
3921 try:
3919 iosize = r.index.entry_size
3922 iosize = r.index.entry_size
3920 except AttributeError:
3923 except AttributeError:
3921 iosize = r._io.size
3924 iosize = r._io.size
3922 buffer = util.buffer
3925 buffer = util.buffer
3923
3926
3924 chunks = []
3927 chunks = []
3925 ladd = chunks.append
3928 ladd = chunks.append
3926 for idx, item in enumerate(chain):
3929 for idx, item in enumerate(chain):
3927 offset = start(item[0])
3930 offset = start(item[0])
3928 bits = data[idx]
3931 bits = data[idx]
3929 for rev in item:
3932 for rev in item:
3930 chunkstart = start(rev)
3933 chunkstart = start(rev)
3931 if inline:
3934 if inline:
3932 chunkstart += (rev + 1) * iosize
3935 chunkstart += (rev + 1) * iosize
3933 chunklength = length(rev)
3936 chunklength = length(rev)
3934 ladd(buffer(bits, chunkstart - offset, chunklength))
3937 ladd(buffer(bits, chunkstart - offset, chunklength))
3935
3938
3936 return chunks
3939 return chunks
3937
3940
3938 def dodeltachain(rev):
3941 def dodeltachain(rev):
3939 if not cache:
3942 if not cache:
3940 r.clearcaches()
3943 r.clearcaches()
3941 r._deltachain(rev)
3944 r._deltachain(rev)
3942
3945
3943 def doread(chain):
3946 def doread(chain):
3944 if not cache:
3947 if not cache:
3945 r.clearcaches()
3948 r.clearcaches()
3946 for item in slicedchain:
3949 for item in slicedchain:
3947 with lazy_reading(r):
3950 with lazy_reading(r):
3948 segmentforrevs(item[0], item[-1])
3951 segmentforrevs(item[0], item[-1])
3949
3952
3950 def doslice(r, chain, size):
3953 def doslice(r, chain, size):
3951 for s in slicechunk(r, chain, targetsize=size):
3954 for s in slicechunk(r, chain, targetsize=size):
3952 pass
3955 pass
3953
3956
3954 def dorawchunks(data, chain):
3957 def dorawchunks(data, chain):
3955 if not cache:
3958 if not cache:
3956 r.clearcaches()
3959 r.clearcaches()
3957 getrawchunks(data, chain)
3960 getrawchunks(data, chain)
3958
3961
3959 def dodecompress(chunks):
3962 def dodecompress(chunks):
3960 decomp = r.decompress
3963 decomp = r.decompress
3961 for chunk in chunks:
3964 for chunk in chunks:
3962 decomp(chunk)
3965 decomp(chunk)
3963
3966
3964 def dopatch(text, bins):
3967 def dopatch(text, bins):
3965 if not cache:
3968 if not cache:
3966 r.clearcaches()
3969 r.clearcaches()
3967 mdiff.patches(text, bins)
3970 mdiff.patches(text, bins)
3968
3971
3969 def dohash(text):
3972 def dohash(text):
3970 if not cache:
3973 if not cache:
3971 r.clearcaches()
3974 r.clearcaches()
3972 r.checkhash(text, node, rev=rev)
3975 r.checkhash(text, node, rev=rev)
3973
3976
3974 def dorevision():
3977 def dorevision():
3975 if not cache:
3978 if not cache:
3976 r.clearcaches()
3979 r.clearcaches()
3977 r.revision(node)
3980 r.revision(node)
3978
3981
3979 try:
3982 try:
3980 from mercurial.revlogutils.deltas import slicechunk
3983 from mercurial.revlogutils.deltas import slicechunk
3981 except ImportError:
3984 except ImportError:
3982 slicechunk = getattr(revlog, '_slicechunk', None)
3985 slicechunk = getattr(revlog, '_slicechunk', None)
3983
3986
3984 size = r.length(rev)
3987 size = r.length(rev)
3985 chain = r._deltachain(rev)[0]
3988 chain = r._deltachain(rev)[0]
3986
3989
3987 with_sparse_read = False
3990 with_sparse_read = False
3988 if hasattr(r, 'data_config'):
3991 if hasattr(r, 'data_config'):
3989 with_sparse_read = r.data_config.with_sparse_read
3992 with_sparse_read = r.data_config.with_sparse_read
3990 elif hasattr(r, '_withsparseread'):
3993 elif hasattr(r, '_withsparseread'):
3991 with_sparse_read = r._withsparseread
3994 with_sparse_read = r._withsparseread
3992 if with_sparse_read:
3995 if with_sparse_read:
3993 slicedchain = (chain,)
3996 slicedchain = (chain,)
3994 else:
3997 else:
3995 slicedchain = tuple(slicechunk(r, chain, targetsize=size))
3998 slicedchain = tuple(slicechunk(r, chain, targetsize=size))
3996 data = [segmentforrevs(seg[0], seg[-1])[1] for seg in slicedchain]
3999 data = [segmentforrevs(seg[0], seg[-1])[1] for seg in slicedchain]
3997 rawchunks = getrawchunks(data, slicedchain)
4000 rawchunks = getrawchunks(data, slicedchain)
3998 bins = r._chunks(chain)
4001 bins = r._chunks(chain)
3999 text = bytes(bins[0])
4002 text = bytes(bins[0])
4000 bins = bins[1:]
4003 bins = bins[1:]
4001 text = mdiff.patches(text, bins)
4004 text = mdiff.patches(text, bins)
4002
4005
4003 benches = [
4006 benches = [
4004 (lambda: dorevision(), b'full'),
4007 (lambda: dorevision(), b'full'),
4005 (lambda: dodeltachain(rev), b'deltachain'),
4008 (lambda: dodeltachain(rev), b'deltachain'),
4006 (lambda: doread(chain), b'read'),
4009 (lambda: doread(chain), b'read'),
4007 ]
4010 ]
4008
4011
4009 if with_sparse_read:
4012 if with_sparse_read:
4010 slicing = (lambda: doslice(r, chain, size), b'slice-sparse-chain')
4013 slicing = (lambda: doslice(r, chain, size), b'slice-sparse-chain')
4011 benches.append(slicing)
4014 benches.append(slicing)
4012
4015
4013 benches.extend(
4016 benches.extend(
4014 [
4017 [
4015 (lambda: dorawchunks(data, slicedchain), b'rawchunks'),
4018 (lambda: dorawchunks(data, slicedchain), b'rawchunks'),
4016 (lambda: dodecompress(rawchunks), b'decompress'),
4019 (lambda: dodecompress(rawchunks), b'decompress'),
4017 (lambda: dopatch(text, bins), b'patch'),
4020 (lambda: dopatch(text, bins), b'patch'),
4018 (lambda: dohash(text), b'hash'),
4021 (lambda: dohash(text), b'hash'),
4019 ]
4022 ]
4020 )
4023 )
4021
4024
4022 timer, fm = gettimer(ui, opts)
4025 timer, fm = gettimer(ui, opts)
4023 for fn, title in benches:
4026 for fn, title in benches:
4024 timer(fn, title=title)
4027 timer(fn, title=title)
4025 fm.end()
4028 fm.end()
4026
4029
4027
4030
4028 @command(
4031 @command(
4029 b'perf::revset|perfrevset',
4032 b'perf::revset|perfrevset',
4030 [
4033 [
4031 (b'C', b'clear', False, b'clear volatile cache between each call.'),
4034 (b'C', b'clear', False, b'clear volatile cache between each call.'),
4032 (b'', b'contexts', False, b'obtain changectx for each revision'),
4035 (b'', b'contexts', False, b'obtain changectx for each revision'),
4033 ]
4036 ]
4034 + formatteropts,
4037 + formatteropts,
4035 b"REVSET",
4038 b"REVSET",
4036 )
4039 )
4037 def perfrevset(ui, repo, expr, clear=False, contexts=False, **opts):
4040 def perfrevset(ui, repo, expr, clear=False, contexts=False, **opts):
4038 """benchmark the execution time of a revset
4041 """benchmark the execution time of a revset
4039
4042
4040 Use the --clean option if need to evaluate the impact of build volatile
4043 Use the --clean option if need to evaluate the impact of build volatile
4041 revisions set cache on the revset execution. Volatile cache hold filtered
4044 revisions set cache on the revset execution. Volatile cache hold filtered
4042 and obsolete related cache."""
4045 and obsolete related cache."""
4043 opts = _byteskwargs(opts)
4046 opts = _byteskwargs(opts)
4044
4047
4045 timer, fm = gettimer(ui, opts)
4048 timer, fm = gettimer(ui, opts)
4046
4049
4047 def d():
4050 def d():
4048 if clear:
4051 if clear:
4049 repo.invalidatevolatilesets()
4052 repo.invalidatevolatilesets()
4050 if contexts:
4053 if contexts:
4051 for ctx in repo.set(expr):
4054 for ctx in repo.set(expr):
4052 pass
4055 pass
4053 else:
4056 else:
4054 for r in repo.revs(expr):
4057 for r in repo.revs(expr):
4055 pass
4058 pass
4056
4059
4057 timer(d)
4060 timer(d)
4058 fm.end()
4061 fm.end()
4059
4062
4060
4063
4061 @command(
4064 @command(
4062 b'perf::volatilesets|perfvolatilesets',
4065 b'perf::volatilesets|perfvolatilesets',
4063 [
4066 [
4064 (b'', b'clear-obsstore', False, b'drop obsstore between each call.'),
4067 (b'', b'clear-obsstore', False, b'drop obsstore between each call.'),
4065 ]
4068 ]
4066 + formatteropts,
4069 + formatteropts,
4067 )
4070 )
4068 def perfvolatilesets(ui, repo, *names, **opts):
4071 def perfvolatilesets(ui, repo, *names, **opts):
4069 """benchmark the computation of various volatile set
4072 """benchmark the computation of various volatile set
4070
4073
4071 Volatile set computes element related to filtering and obsolescence."""
4074 Volatile set computes element related to filtering and obsolescence."""
4072 opts = _byteskwargs(opts)
4075 opts = _byteskwargs(opts)
4073 timer, fm = gettimer(ui, opts)
4076 timer, fm = gettimer(ui, opts)
4074 repo = repo.unfiltered()
4077 repo = repo.unfiltered()
4075
4078
4076 def getobs(name):
4079 def getobs(name):
4077 def d():
4080 def d():
4078 repo.invalidatevolatilesets()
4081 repo.invalidatevolatilesets()
4079 if opts[b'clear_obsstore']:
4082 if opts[b'clear_obsstore']:
4080 clearfilecache(repo, b'obsstore')
4083 clearfilecache(repo, b'obsstore')
4081 obsolete.getrevs(repo, name)
4084 obsolete.getrevs(repo, name)
4082
4085
4083 return d
4086 return d
4084
4087
4085 allobs = sorted(obsolete.cachefuncs)
4088 allobs = sorted(obsolete.cachefuncs)
4086 if names:
4089 if names:
4087 allobs = [n for n in allobs if n in names]
4090 allobs = [n for n in allobs if n in names]
4088
4091
4089 for name in allobs:
4092 for name in allobs:
4090 timer(getobs(name), title=name)
4093 timer(getobs(name), title=name)
4091
4094
4092 def getfiltered(name):
4095 def getfiltered(name):
4093 def d():
4096 def d():
4094 repo.invalidatevolatilesets()
4097 repo.invalidatevolatilesets()
4095 if opts[b'clear_obsstore']:
4098 if opts[b'clear_obsstore']:
4096 clearfilecache(repo, b'obsstore')
4099 clearfilecache(repo, b'obsstore')
4097 repoview.filterrevs(repo, name)
4100 repoview.filterrevs(repo, name)
4098
4101
4099 return d
4102 return d
4100
4103
4101 allfilter = sorted(repoview.filtertable)
4104 allfilter = sorted(repoview.filtertable)
4102 if names:
4105 if names:
4103 allfilter = [n for n in allfilter if n in names]
4106 allfilter = [n for n in allfilter if n in names]
4104
4107
4105 for name in allfilter:
4108 for name in allfilter:
4106 timer(getfiltered(name), title=name)
4109 timer(getfiltered(name), title=name)
4107 fm.end()
4110 fm.end()
4108
4111
4109
4112
4110 @command(
4113 @command(
4111 b'perf::branchmap|perfbranchmap',
4114 b'perf::branchmap|perfbranchmap',
4112 [
4115 [
4113 (b'f', b'full', False, b'Includes build time of subset'),
4116 (b'f', b'full', False, b'Includes build time of subset'),
4114 (
4117 (
4115 b'',
4118 b'',
4116 b'clear-revbranch',
4119 b'clear-revbranch',
4117 False,
4120 False,
4118 b'purge the revbranch cache between computation',
4121 b'purge the revbranch cache between computation',
4119 ),
4122 ),
4120 ]
4123 ]
4121 + formatteropts,
4124 + formatteropts,
4122 )
4125 )
4123 def perfbranchmap(ui, repo, *filternames, **opts):
4126 def perfbranchmap(ui, repo, *filternames, **opts):
4124 """benchmark the update of a branchmap
4127 """benchmark the update of a branchmap
4125
4128
4126 This benchmarks the full repo.branchmap() call with read and write disabled
4129 This benchmarks the full repo.branchmap() call with read and write disabled
4127 """
4130 """
4128 opts = _byteskwargs(opts)
4131 opts = _byteskwargs(opts)
4129 full = opts.get(b"full", False)
4132 full = opts.get(b"full", False)
4130 clear_revbranch = opts.get(b"clear_revbranch", False)
4133 clear_revbranch = opts.get(b"clear_revbranch", False)
4131 timer, fm = gettimer(ui, opts)
4134 timer, fm = gettimer(ui, opts)
4132
4135
4133 def getbranchmap(filtername):
4136 def getbranchmap(filtername):
4134 """generate a benchmark function for the filtername"""
4137 """generate a benchmark function for the filtername"""
4135 if filtername is None:
4138 if filtername is None:
4136 view = repo
4139 view = repo
4137 else:
4140 else:
4138 view = repo.filtered(filtername)
4141 view = repo.filtered(filtername)
4139 if util.safehasattr(view._branchcaches, '_per_filter'):
4142 if util.safehasattr(view._branchcaches, '_per_filter'):
4140 filtered = view._branchcaches._per_filter
4143 filtered = view._branchcaches._per_filter
4141 else:
4144 else:
4142 # older versions
4145 # older versions
4143 filtered = view._branchcaches
4146 filtered = view._branchcaches
4144
4147
4145 def d():
4148 def d():
4146 if clear_revbranch:
4149 if clear_revbranch:
4147 repo.revbranchcache()._clear()
4150 repo.revbranchcache()._clear()
4148 if full:
4151 if full:
4149 view._branchcaches.clear()
4152 view._branchcaches.clear()
4150 else:
4153 else:
4151 filtered.pop(filtername, None)
4154 filtered.pop(filtername, None)
4152 view.branchmap()
4155 view.branchmap()
4153
4156
4154 return d
4157 return d
4155
4158
4156 # add filter in smaller subset to bigger subset
4159 # add filter in smaller subset to bigger subset
4157 possiblefilters = set(repoview.filtertable)
4160 possiblefilters = set(repoview.filtertable)
4158 if filternames:
4161 if filternames:
4159 possiblefilters &= set(filternames)
4162 possiblefilters &= set(filternames)
4160 subsettable = getbranchmapsubsettable()
4163 subsettable = getbranchmapsubsettable()
4161 allfilters = []
4164 allfilters = []
4162 while possiblefilters:
4165 while possiblefilters:
4163 for name in possiblefilters:
4166 for name in possiblefilters:
4164 subset = subsettable.get(name)
4167 subset = subsettable.get(name)
4165 if subset not in possiblefilters:
4168 if subset not in possiblefilters:
4166 break
4169 break
4167 else:
4170 else:
4168 assert False, b'subset cycle %s!' % possiblefilters
4171 assert False, b'subset cycle %s!' % possiblefilters
4169 allfilters.append(name)
4172 allfilters.append(name)
4170 possiblefilters.remove(name)
4173 possiblefilters.remove(name)
4171
4174
4172 # warm the cache
4175 # warm the cache
4173 if not full:
4176 if not full:
4174 for name in allfilters:
4177 for name in allfilters:
4175 repo.filtered(name).branchmap()
4178 repo.filtered(name).branchmap()
4176 if not filternames or b'unfiltered' in filternames:
4179 if not filternames or b'unfiltered' in filternames:
4177 # add unfiltered
4180 # add unfiltered
4178 allfilters.append(None)
4181 allfilters.append(None)
4179
4182
4180 if util.safehasattr(branchmap.branchcache, 'fromfile'):
4183 if util.safehasattr(branchmap.branchcache, 'fromfile'):
4181 branchcacheread = safeattrsetter(branchmap.branchcache, b'fromfile')
4184 branchcacheread = safeattrsetter(branchmap.branchcache, b'fromfile')
4182 branchcacheread.set(classmethod(lambda *args: None))
4185 branchcacheread.set(classmethod(lambda *args: None))
4183 else:
4186 else:
4184 # older versions
4187 # older versions
4185 branchcacheread = safeattrsetter(branchmap, b'read')
4188 branchcacheread = safeattrsetter(branchmap, b'read')
4186 branchcacheread.set(lambda *args: None)
4189 branchcacheread.set(lambda *args: None)
4187 branchcachewrite = safeattrsetter(branchmap.branchcache, b'write')
4190 branchcachewrite = safeattrsetter(branchmap.branchcache, b'write')
4188 branchcachewrite.set(lambda *args: None)
4191 branchcachewrite.set(lambda *args: None)
4189 try:
4192 try:
4190 for name in allfilters:
4193 for name in allfilters:
4191 printname = name
4194 printname = name
4192 if name is None:
4195 if name is None:
4193 printname = b'unfiltered'
4196 printname = b'unfiltered'
4194 timer(getbranchmap(name), title=printname)
4197 timer(getbranchmap(name), title=printname)
4195 finally:
4198 finally:
4196 branchcacheread.restore()
4199 branchcacheread.restore()
4197 branchcachewrite.restore()
4200 branchcachewrite.restore()
4198 fm.end()
4201 fm.end()
4199
4202
4200
4203
4201 @command(
4204 @command(
4202 b'perf::branchmapupdate|perfbranchmapupdate',
4205 b'perf::branchmapupdate|perfbranchmapupdate',
4203 [
4206 [
4204 (b'', b'base', [], b'subset of revision to start from'),
4207 (b'', b'base', [], b'subset of revision to start from'),
4205 (b'', b'target', [], b'subset of revision to end with'),
4208 (b'', b'target', [], b'subset of revision to end with'),
4206 (b'', b'clear-caches', False, b'clear cache between each runs'),
4209 (b'', b'clear-caches', False, b'clear cache between each runs'),
4207 ]
4210 ]
4208 + formatteropts,
4211 + formatteropts,
4209 )
4212 )
4210 def perfbranchmapupdate(ui, repo, base=(), target=(), **opts):
4213 def perfbranchmapupdate(ui, repo, base=(), target=(), **opts):
4211 """benchmark branchmap update from for <base> revs to <target> revs
4214 """benchmark branchmap update from for <base> revs to <target> revs
4212
4215
4213 If `--clear-caches` is passed, the following items will be reset before
4216 If `--clear-caches` is passed, the following items will be reset before
4214 each update:
4217 each update:
4215 * the changelog instance and associated indexes
4218 * the changelog instance and associated indexes
4216 * the rev-branch-cache instance
4219 * the rev-branch-cache instance
4217
4220
4218 Examples:
4221 Examples:
4219
4222
4220 # update for the one last revision
4223 # update for the one last revision
4221 $ hg perfbranchmapupdate --base 'not tip' --target 'tip'
4224 $ hg perfbranchmapupdate --base 'not tip' --target 'tip'
4222
4225
4223 $ update for change coming with a new branch
4226 $ update for change coming with a new branch
4224 $ hg perfbranchmapupdate --base 'stable' --target 'default'
4227 $ hg perfbranchmapupdate --base 'stable' --target 'default'
4225 """
4228 """
4226 from mercurial import branchmap
4229 from mercurial import branchmap
4227 from mercurial import repoview
4230 from mercurial import repoview
4228
4231
4229 opts = _byteskwargs(opts)
4232 opts = _byteskwargs(opts)
4230 timer, fm = gettimer(ui, opts)
4233 timer, fm = gettimer(ui, opts)
4231 clearcaches = opts[b'clear_caches']
4234 clearcaches = opts[b'clear_caches']
4232 unfi = repo.unfiltered()
4235 unfi = repo.unfiltered()
4233 x = [None] # used to pass data between closure
4236 x = [None] # used to pass data between closure
4234
4237
4235 # we use a `list` here to avoid possible side effect from smartset
4238 # we use a `list` here to avoid possible side effect from smartset
4236 baserevs = list(scmutil.revrange(repo, base))
4239 baserevs = list(scmutil.revrange(repo, base))
4237 targetrevs = list(scmutil.revrange(repo, target))
4240 targetrevs = list(scmutil.revrange(repo, target))
4238 if not baserevs:
4241 if not baserevs:
4239 raise error.Abort(b'no revisions selected for --base')
4242 raise error.Abort(b'no revisions selected for --base')
4240 if not targetrevs:
4243 if not targetrevs:
4241 raise error.Abort(b'no revisions selected for --target')
4244 raise error.Abort(b'no revisions selected for --target')
4242
4245
4243 # make sure the target branchmap also contains the one in the base
4246 # make sure the target branchmap also contains the one in the base
4244 targetrevs = list(set(baserevs) | set(targetrevs))
4247 targetrevs = list(set(baserevs) | set(targetrevs))
4245 targetrevs.sort()
4248 targetrevs.sort()
4246
4249
4247 cl = repo.changelog
4250 cl = repo.changelog
4248 allbaserevs = list(cl.ancestors(baserevs, inclusive=True))
4251 allbaserevs = list(cl.ancestors(baserevs, inclusive=True))
4249 allbaserevs.sort()
4252 allbaserevs.sort()
4250 alltargetrevs = frozenset(cl.ancestors(targetrevs, inclusive=True))
4253 alltargetrevs = frozenset(cl.ancestors(targetrevs, inclusive=True))
4251
4254
4252 newrevs = list(alltargetrevs.difference(allbaserevs))
4255 newrevs = list(alltargetrevs.difference(allbaserevs))
4253 newrevs.sort()
4256 newrevs.sort()
4254
4257
4255 allrevs = frozenset(unfi.changelog.revs())
4258 allrevs = frozenset(unfi.changelog.revs())
4256 basefilterrevs = frozenset(allrevs.difference(allbaserevs))
4259 basefilterrevs = frozenset(allrevs.difference(allbaserevs))
4257 targetfilterrevs = frozenset(allrevs.difference(alltargetrevs))
4260 targetfilterrevs = frozenset(allrevs.difference(alltargetrevs))
4258
4261
4259 def basefilter(repo, visibilityexceptions=None):
4262 def basefilter(repo, visibilityexceptions=None):
4260 return basefilterrevs
4263 return basefilterrevs
4261
4264
4262 def targetfilter(repo, visibilityexceptions=None):
4265 def targetfilter(repo, visibilityexceptions=None):
4263 return targetfilterrevs
4266 return targetfilterrevs
4264
4267
4265 msg = b'benchmark of branchmap with %d revisions with %d new ones\n'
4268 msg = b'benchmark of branchmap with %d revisions with %d new ones\n'
4266 ui.status(msg % (len(allbaserevs), len(newrevs)))
4269 ui.status(msg % (len(allbaserevs), len(newrevs)))
4267 if targetfilterrevs:
4270 if targetfilterrevs:
4268 msg = b'(%d revisions still filtered)\n'
4271 msg = b'(%d revisions still filtered)\n'
4269 ui.status(msg % len(targetfilterrevs))
4272 ui.status(msg % len(targetfilterrevs))
4270
4273
4271 try:
4274 try:
4272 repoview.filtertable[b'__perf_branchmap_update_base'] = basefilter
4275 repoview.filtertable[b'__perf_branchmap_update_base'] = basefilter
4273 repoview.filtertable[b'__perf_branchmap_update_target'] = targetfilter
4276 repoview.filtertable[b'__perf_branchmap_update_target'] = targetfilter
4274
4277
4275 baserepo = repo.filtered(b'__perf_branchmap_update_base')
4278 baserepo = repo.filtered(b'__perf_branchmap_update_base')
4276 targetrepo = repo.filtered(b'__perf_branchmap_update_target')
4279 targetrepo = repo.filtered(b'__perf_branchmap_update_target')
4277
4280
4278 # try to find an existing branchmap to reuse
4281 # try to find an existing branchmap to reuse
4279 subsettable = getbranchmapsubsettable()
4282 subsettable = getbranchmapsubsettable()
4280 candidatefilter = subsettable.get(None)
4283 candidatefilter = subsettable.get(None)
4281 while candidatefilter is not None:
4284 while candidatefilter is not None:
4282 candidatebm = repo.filtered(candidatefilter).branchmap()
4285 candidatebm = repo.filtered(candidatefilter).branchmap()
4283 if candidatebm.validfor(baserepo):
4286 if candidatebm.validfor(baserepo):
4284 filtered = repoview.filterrevs(repo, candidatefilter)
4287 filtered = repoview.filterrevs(repo, candidatefilter)
4285 missing = [r for r in allbaserevs if r in filtered]
4288 missing = [r for r in allbaserevs if r in filtered]
4286 base = candidatebm.copy()
4289 base = candidatebm.copy()
4287 base.update(baserepo, missing)
4290 base.update(baserepo, missing)
4288 break
4291 break
4289 candidatefilter = subsettable.get(candidatefilter)
4292 candidatefilter = subsettable.get(candidatefilter)
4290 else:
4293 else:
4291 # no suitable subset where found
4294 # no suitable subset where found
4292 base = branchmap.branchcache()
4295 base = branchmap.branchcache()
4293 base.update(baserepo, allbaserevs)
4296 base.update(baserepo, allbaserevs)
4294
4297
4295 def setup():
4298 def setup():
4296 x[0] = base.copy()
4299 x[0] = base.copy()
4297 if clearcaches:
4300 if clearcaches:
4298 unfi._revbranchcache = None
4301 unfi._revbranchcache = None
4299 clearchangelog(repo)
4302 clearchangelog(repo)
4300
4303
4301 def bench():
4304 def bench():
4302 x[0].update(targetrepo, newrevs)
4305 x[0].update(targetrepo, newrevs)
4303
4306
4304 timer(bench, setup=setup)
4307 timer(bench, setup=setup)
4305 fm.end()
4308 fm.end()
4306 finally:
4309 finally:
4307 repoview.filtertable.pop(b'__perf_branchmap_update_base', None)
4310 repoview.filtertable.pop(b'__perf_branchmap_update_base', None)
4308 repoview.filtertable.pop(b'__perf_branchmap_update_target', None)
4311 repoview.filtertable.pop(b'__perf_branchmap_update_target', None)
4309
4312
4310
4313
4311 @command(
4314 @command(
4312 b'perf::branchmapload|perfbranchmapload',
4315 b'perf::branchmapload|perfbranchmapload',
4313 [
4316 [
4314 (b'f', b'filter', b'', b'Specify repoview filter'),
4317 (b'f', b'filter', b'', b'Specify repoview filter'),
4315 (b'', b'list', False, b'List brachmap filter caches'),
4318 (b'', b'list', False, b'List brachmap filter caches'),
4316 (b'', b'clear-revlogs', False, b'refresh changelog and manifest'),
4319 (b'', b'clear-revlogs', False, b'refresh changelog and manifest'),
4317 ]
4320 ]
4318 + formatteropts,
4321 + formatteropts,
4319 )
4322 )
4320 def perfbranchmapload(ui, repo, filter=b'', list=False, **opts):
4323 def perfbranchmapload(ui, repo, filter=b'', list=False, **opts):
4321 """benchmark reading the branchmap"""
4324 """benchmark reading the branchmap"""
4322 opts = _byteskwargs(opts)
4325 opts = _byteskwargs(opts)
4323 clearrevlogs = opts[b'clear_revlogs']
4326 clearrevlogs = opts[b'clear_revlogs']
4324
4327
4325 if list:
4328 if list:
4326 for name, kind, st in repo.cachevfs.readdir(stat=True):
4329 for name, kind, st in repo.cachevfs.readdir(stat=True):
4327 if name.startswith(b'branch2'):
4330 if name.startswith(b'branch2'):
4328 filtername = name.partition(b'-')[2] or b'unfiltered'
4331 filtername = name.partition(b'-')[2] or b'unfiltered'
4329 ui.status(
4332 ui.status(
4330 b'%s - %s\n' % (filtername, util.bytecount(st.st_size))
4333 b'%s - %s\n' % (filtername, util.bytecount(st.st_size))
4331 )
4334 )
4332 return
4335 return
4333 if not filter:
4336 if not filter:
4334 filter = None
4337 filter = None
4335 subsettable = getbranchmapsubsettable()
4338 subsettable = getbranchmapsubsettable()
4336 if filter is None:
4339 if filter is None:
4337 repo = repo.unfiltered()
4340 repo = repo.unfiltered()
4338 else:
4341 else:
4339 repo = repoview.repoview(repo, filter)
4342 repo = repoview.repoview(repo, filter)
4340
4343
4341 repo.branchmap() # make sure we have a relevant, up to date branchmap
4344 repo.branchmap() # make sure we have a relevant, up to date branchmap
4342
4345
4343 try:
4346 try:
4344 fromfile = branchmap.branchcache.fromfile
4347 fromfile = branchmap.branchcache.fromfile
4345 except AttributeError:
4348 except AttributeError:
4346 # older versions
4349 # older versions
4347 fromfile = branchmap.read
4350 fromfile = branchmap.read
4348
4351
4349 currentfilter = filter
4352 currentfilter = filter
4350 # try once without timer, the filter may not be cached
4353 # try once without timer, the filter may not be cached
4351 while fromfile(repo) is None:
4354 while fromfile(repo) is None:
4352 currentfilter = subsettable.get(currentfilter)
4355 currentfilter = subsettable.get(currentfilter)
4353 if currentfilter is None:
4356 if currentfilter is None:
4354 raise error.Abort(
4357 raise error.Abort(
4355 b'No branchmap cached for %s repo' % (filter or b'unfiltered')
4358 b'No branchmap cached for %s repo' % (filter or b'unfiltered')
4356 )
4359 )
4357 repo = repo.filtered(currentfilter)
4360 repo = repo.filtered(currentfilter)
4358 timer, fm = gettimer(ui, opts)
4361 timer, fm = gettimer(ui, opts)
4359
4362
4360 def setup():
4363 def setup():
4361 if clearrevlogs:
4364 if clearrevlogs:
4362 clearchangelog(repo)
4365 clearchangelog(repo)
4363
4366
4364 def bench():
4367 def bench():
4365 fromfile(repo)
4368 fromfile(repo)
4366
4369
4367 timer(bench, setup=setup)
4370 timer(bench, setup=setup)
4368 fm.end()
4371 fm.end()
4369
4372
4370
4373
4371 @command(b'perf::loadmarkers|perfloadmarkers')
4374 @command(b'perf::loadmarkers|perfloadmarkers')
4372 def perfloadmarkers(ui, repo):
4375 def perfloadmarkers(ui, repo):
4373 """benchmark the time to parse the on-disk markers for a repo
4376 """benchmark the time to parse the on-disk markers for a repo
4374
4377
4375 Result is the number of markers in the repo."""
4378 Result is the number of markers in the repo."""
4376 timer, fm = gettimer(ui)
4379 timer, fm = gettimer(ui)
4377 svfs = getsvfs(repo)
4380 svfs = getsvfs(repo)
4378 timer(lambda: len(obsolete.obsstore(repo, svfs)))
4381 timer(lambda: len(obsolete.obsstore(repo, svfs)))
4379 fm.end()
4382 fm.end()
4380
4383
4381
4384
4382 @command(
4385 @command(
4383 b'perf::lrucachedict|perflrucachedict',
4386 b'perf::lrucachedict|perflrucachedict',
4384 formatteropts
4387 formatteropts
4385 + [
4388 + [
4386 (b'', b'costlimit', 0, b'maximum total cost of items in cache'),
4389 (b'', b'costlimit', 0, b'maximum total cost of items in cache'),
4387 (b'', b'mincost', 0, b'smallest cost of items in cache'),
4390 (b'', b'mincost', 0, b'smallest cost of items in cache'),
4388 (b'', b'maxcost', 100, b'maximum cost of items in cache'),
4391 (b'', b'maxcost', 100, b'maximum cost of items in cache'),
4389 (b'', b'size', 4, b'size of cache'),
4392 (b'', b'size', 4, b'size of cache'),
4390 (b'', b'gets', 10000, b'number of key lookups'),
4393 (b'', b'gets', 10000, b'number of key lookups'),
4391 (b'', b'sets', 10000, b'number of key sets'),
4394 (b'', b'sets', 10000, b'number of key sets'),
4392 (b'', b'mixed', 10000, b'number of mixed mode operations'),
4395 (b'', b'mixed', 10000, b'number of mixed mode operations'),
4393 (
4396 (
4394 b'',
4397 b'',
4395 b'mixedgetfreq',
4398 b'mixedgetfreq',
4396 50,
4399 50,
4397 b'frequency of get vs set ops in mixed mode',
4400 b'frequency of get vs set ops in mixed mode',
4398 ),
4401 ),
4399 ],
4402 ],
4400 norepo=True,
4403 norepo=True,
4401 )
4404 )
4402 def perflrucache(
4405 def perflrucache(
4403 ui,
4406 ui,
4404 mincost=0,
4407 mincost=0,
4405 maxcost=100,
4408 maxcost=100,
4406 costlimit=0,
4409 costlimit=0,
4407 size=4,
4410 size=4,
4408 gets=10000,
4411 gets=10000,
4409 sets=10000,
4412 sets=10000,
4410 mixed=10000,
4413 mixed=10000,
4411 mixedgetfreq=50,
4414 mixedgetfreq=50,
4412 **opts
4415 **opts
4413 ):
4416 ):
4414 opts = _byteskwargs(opts)
4417 opts = _byteskwargs(opts)
4415
4418
4416 def doinit():
4419 def doinit():
4417 for i in _xrange(10000):
4420 for i in _xrange(10000):
4418 util.lrucachedict(size)
4421 util.lrucachedict(size)
4419
4422
4420 costrange = list(range(mincost, maxcost + 1))
4423 costrange = list(range(mincost, maxcost + 1))
4421
4424
4422 values = []
4425 values = []
4423 for i in _xrange(size):
4426 for i in _xrange(size):
4424 values.append(random.randint(0, _maxint))
4427 values.append(random.randint(0, _maxint))
4425
4428
4426 # Get mode fills the cache and tests raw lookup performance with no
4429 # Get mode fills the cache and tests raw lookup performance with no
4427 # eviction.
4430 # eviction.
4428 getseq = []
4431 getseq = []
4429 for i in _xrange(gets):
4432 for i in _xrange(gets):
4430 getseq.append(random.choice(values))
4433 getseq.append(random.choice(values))
4431
4434
4432 def dogets():
4435 def dogets():
4433 d = util.lrucachedict(size)
4436 d = util.lrucachedict(size)
4434 for v in values:
4437 for v in values:
4435 d[v] = v
4438 d[v] = v
4436 for key in getseq:
4439 for key in getseq:
4437 value = d[key]
4440 value = d[key]
4438 value # silence pyflakes warning
4441 value # silence pyflakes warning
4439
4442
4440 def dogetscost():
4443 def dogetscost():
4441 d = util.lrucachedict(size, maxcost=costlimit)
4444 d = util.lrucachedict(size, maxcost=costlimit)
4442 for i, v in enumerate(values):
4445 for i, v in enumerate(values):
4443 d.insert(v, v, cost=costs[i])
4446 d.insert(v, v, cost=costs[i])
4444 for key in getseq:
4447 for key in getseq:
4445 try:
4448 try:
4446 value = d[key]
4449 value = d[key]
4447 value # silence pyflakes warning
4450 value # silence pyflakes warning
4448 except KeyError:
4451 except KeyError:
4449 pass
4452 pass
4450
4453
4451 # Set mode tests insertion speed with cache eviction.
4454 # Set mode tests insertion speed with cache eviction.
4452 setseq = []
4455 setseq = []
4453 costs = []
4456 costs = []
4454 for i in _xrange(sets):
4457 for i in _xrange(sets):
4455 setseq.append(random.randint(0, _maxint))
4458 setseq.append(random.randint(0, _maxint))
4456 costs.append(random.choice(costrange))
4459 costs.append(random.choice(costrange))
4457
4460
4458 def doinserts():
4461 def doinserts():
4459 d = util.lrucachedict(size)
4462 d = util.lrucachedict(size)
4460 for v in setseq:
4463 for v in setseq:
4461 d.insert(v, v)
4464 d.insert(v, v)
4462
4465
4463 def doinsertscost():
4466 def doinsertscost():
4464 d = util.lrucachedict(size, maxcost=costlimit)
4467 d = util.lrucachedict(size, maxcost=costlimit)
4465 for i, v in enumerate(setseq):
4468 for i, v in enumerate(setseq):
4466 d.insert(v, v, cost=costs[i])
4469 d.insert(v, v, cost=costs[i])
4467
4470
4468 def dosets():
4471 def dosets():
4469 d = util.lrucachedict(size)
4472 d = util.lrucachedict(size)
4470 for v in setseq:
4473 for v in setseq:
4471 d[v] = v
4474 d[v] = v
4472
4475
4473 # Mixed mode randomly performs gets and sets with eviction.
4476 # Mixed mode randomly performs gets and sets with eviction.
4474 mixedops = []
4477 mixedops = []
4475 for i in _xrange(mixed):
4478 for i in _xrange(mixed):
4476 r = random.randint(0, 100)
4479 r = random.randint(0, 100)
4477 if r < mixedgetfreq:
4480 if r < mixedgetfreq:
4478 op = 0
4481 op = 0
4479 else:
4482 else:
4480 op = 1
4483 op = 1
4481
4484
4482 mixedops.append(
4485 mixedops.append(
4483 (op, random.randint(0, size * 2), random.choice(costrange))
4486 (op, random.randint(0, size * 2), random.choice(costrange))
4484 )
4487 )
4485
4488
4486 def domixed():
4489 def domixed():
4487 d = util.lrucachedict(size)
4490 d = util.lrucachedict(size)
4488
4491
4489 for op, v, cost in mixedops:
4492 for op, v, cost in mixedops:
4490 if op == 0:
4493 if op == 0:
4491 try:
4494 try:
4492 d[v]
4495 d[v]
4493 except KeyError:
4496 except KeyError:
4494 pass
4497 pass
4495 else:
4498 else:
4496 d[v] = v
4499 d[v] = v
4497
4500
4498 def domixedcost():
4501 def domixedcost():
4499 d = util.lrucachedict(size, maxcost=costlimit)
4502 d = util.lrucachedict(size, maxcost=costlimit)
4500
4503
4501 for op, v, cost in mixedops:
4504 for op, v, cost in mixedops:
4502 if op == 0:
4505 if op == 0:
4503 try:
4506 try:
4504 d[v]
4507 d[v]
4505 except KeyError:
4508 except KeyError:
4506 pass
4509 pass
4507 else:
4510 else:
4508 d.insert(v, v, cost=cost)
4511 d.insert(v, v, cost=cost)
4509
4512
4510 benches = [
4513 benches = [
4511 (doinit, b'init'),
4514 (doinit, b'init'),
4512 ]
4515 ]
4513
4516
4514 if costlimit:
4517 if costlimit:
4515 benches.extend(
4518 benches.extend(
4516 [
4519 [
4517 (dogetscost, b'gets w/ cost limit'),
4520 (dogetscost, b'gets w/ cost limit'),
4518 (doinsertscost, b'inserts w/ cost limit'),
4521 (doinsertscost, b'inserts w/ cost limit'),
4519 (domixedcost, b'mixed w/ cost limit'),
4522 (domixedcost, b'mixed w/ cost limit'),
4520 ]
4523 ]
4521 )
4524 )
4522 else:
4525 else:
4523 benches.extend(
4526 benches.extend(
4524 [
4527 [
4525 (dogets, b'gets'),
4528 (dogets, b'gets'),
4526 (doinserts, b'inserts'),
4529 (doinserts, b'inserts'),
4527 (dosets, b'sets'),
4530 (dosets, b'sets'),
4528 (domixed, b'mixed'),
4531 (domixed, b'mixed'),
4529 ]
4532 ]
4530 )
4533 )
4531
4534
4532 for fn, title in benches:
4535 for fn, title in benches:
4533 timer, fm = gettimer(ui, opts)
4536 timer, fm = gettimer(ui, opts)
4534 timer(fn, title=title)
4537 timer(fn, title=title)
4535 fm.end()
4538 fm.end()
4536
4539
4537
4540
4538 @command(
4541 @command(
4539 b'perf::write|perfwrite',
4542 b'perf::write|perfwrite',
4540 formatteropts
4543 formatteropts
4541 + [
4544 + [
4542 (b'', b'write-method', b'write', b'ui write method'),
4545 (b'', b'write-method', b'write', b'ui write method'),
4543 (b'', b'nlines', 100, b'number of lines'),
4546 (b'', b'nlines', 100, b'number of lines'),
4544 (b'', b'nitems', 100, b'number of items (per line)'),
4547 (b'', b'nitems', 100, b'number of items (per line)'),
4545 (b'', b'item', b'x', b'item that is written'),
4548 (b'', b'item', b'x', b'item that is written'),
4546 (b'', b'batch-line', None, b'pass whole line to write method at once'),
4549 (b'', b'batch-line', None, b'pass whole line to write method at once'),
4547 (b'', b'flush-line', None, b'flush after each line'),
4550 (b'', b'flush-line', None, b'flush after each line'),
4548 ],
4551 ],
4549 )
4552 )
4550 def perfwrite(ui, repo, **opts):
4553 def perfwrite(ui, repo, **opts):
4551 """microbenchmark ui.write (and others)"""
4554 """microbenchmark ui.write (and others)"""
4552 opts = _byteskwargs(opts)
4555 opts = _byteskwargs(opts)
4553
4556
4554 write = getattr(ui, _sysstr(opts[b'write_method']))
4557 write = getattr(ui, _sysstr(opts[b'write_method']))
4555 nlines = int(opts[b'nlines'])
4558 nlines = int(opts[b'nlines'])
4556 nitems = int(opts[b'nitems'])
4559 nitems = int(opts[b'nitems'])
4557 item = opts[b'item']
4560 item = opts[b'item']
4558 batch_line = opts.get(b'batch_line')
4561 batch_line = opts.get(b'batch_line')
4559 flush_line = opts.get(b'flush_line')
4562 flush_line = opts.get(b'flush_line')
4560
4563
4561 if batch_line:
4564 if batch_line:
4562 line = item * nitems + b'\n'
4565 line = item * nitems + b'\n'
4563
4566
4564 def benchmark():
4567 def benchmark():
4565 for i in pycompat.xrange(nlines):
4568 for i in pycompat.xrange(nlines):
4566 if batch_line:
4569 if batch_line:
4567 write(line)
4570 write(line)
4568 else:
4571 else:
4569 for i in pycompat.xrange(nitems):
4572 for i in pycompat.xrange(nitems):
4570 write(item)
4573 write(item)
4571 write(b'\n')
4574 write(b'\n')
4572 if flush_line:
4575 if flush_line:
4573 ui.flush()
4576 ui.flush()
4574 ui.flush()
4577 ui.flush()
4575
4578
4576 timer, fm = gettimer(ui, opts)
4579 timer, fm = gettimer(ui, opts)
4577 timer(benchmark)
4580 timer(benchmark)
4578 fm.end()
4581 fm.end()
4579
4582
4580
4583
4581 def uisetup(ui):
4584 def uisetup(ui):
4582 if util.safehasattr(cmdutil, b'openrevlog') and not util.safehasattr(
4585 if util.safehasattr(cmdutil, b'openrevlog') and not util.safehasattr(
4583 commands, b'debugrevlogopts'
4586 commands, b'debugrevlogopts'
4584 ):
4587 ):
4585 # for "historical portability":
4588 # for "historical portability":
4586 # In this case, Mercurial should be 1.9 (or a79fea6b3e77) -
4589 # In this case, Mercurial should be 1.9 (or a79fea6b3e77) -
4587 # 3.7 (or 5606f7d0d063). Therefore, '--dir' option for
4590 # 3.7 (or 5606f7d0d063). Therefore, '--dir' option for
4588 # openrevlog() should cause failure, because it has been
4591 # openrevlog() should cause failure, because it has been
4589 # available since 3.5 (or 49c583ca48c4).
4592 # available since 3.5 (or 49c583ca48c4).
4590 def openrevlog(orig, repo, cmd, file_, opts):
4593 def openrevlog(orig, repo, cmd, file_, opts):
4591 if opts.get(b'dir') and not util.safehasattr(repo, b'dirlog'):
4594 if opts.get(b'dir') and not util.safehasattr(repo, b'dirlog'):
4592 raise error.Abort(
4595 raise error.Abort(
4593 b"This version doesn't support --dir option",
4596 b"This version doesn't support --dir option",
4594 hint=b"use 3.5 or later",
4597 hint=b"use 3.5 or later",
4595 )
4598 )
4596 return orig(repo, cmd, file_, opts)
4599 return orig(repo, cmd, file_, opts)
4597
4600
4598 name = _sysstr(b'openrevlog')
4601 name = _sysstr(b'openrevlog')
4599 extensions.wrapfunction(cmdutil, name, openrevlog)
4602 extensions.wrapfunction(cmdutil, name, openrevlog)
4600
4603
4601
4604
4602 @command(
4605 @command(
4603 b'perf::progress|perfprogress',
4606 b'perf::progress|perfprogress',
4604 formatteropts
4607 formatteropts
4605 + [
4608 + [
4606 (b'', b'topic', b'topic', b'topic for progress messages'),
4609 (b'', b'topic', b'topic', b'topic for progress messages'),
4607 (b'c', b'total', 1000000, b'total value we are progressing to'),
4610 (b'c', b'total', 1000000, b'total value we are progressing to'),
4608 ],
4611 ],
4609 norepo=True,
4612 norepo=True,
4610 )
4613 )
4611 def perfprogress(ui, topic=None, total=None, **opts):
4614 def perfprogress(ui, topic=None, total=None, **opts):
4612 """printing of progress bars"""
4615 """printing of progress bars"""
4613 opts = _byteskwargs(opts)
4616 opts = _byteskwargs(opts)
4614
4617
4615 timer, fm = gettimer(ui, opts)
4618 timer, fm = gettimer(ui, opts)
4616
4619
4617 def doprogress():
4620 def doprogress():
4618 with ui.makeprogress(topic, total=total) as progress:
4621 with ui.makeprogress(topic, total=total) as progress:
4619 for i in _xrange(total):
4622 for i in _xrange(total):
4620 progress.increment()
4623 progress.increment()
4621
4624
4622 timer(doprogress)
4625 timer(doprogress)
4623 fm.end()
4626 fm.end()
@@ -1,1630 +1,1630 b''
1 # revlogdeltas.py - Logic around delta computation for revlog
1 # revlogdeltas.py - Logic around delta computation for revlog
2 #
2 #
3 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
3 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
4 # Copyright 2018 Octobus <contact@octobus.net>
4 # Copyright 2018 Octobus <contact@octobus.net>
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8 """Helper class to compute deltas stored inside revlogs"""
8 """Helper class to compute deltas stored inside revlogs"""
9
9
10
10
11 import collections
11 import collections
12 import struct
12 import struct
13
13
14 # import stuff from node for others to import from revlog
14 # import stuff from node for others to import from revlog
15 from ..node import nullrev
15 from ..node import nullrev
16 from ..i18n import _
16 from ..i18n import _
17
17
18 from .constants import (
18 from .constants import (
19 COMP_MODE_DEFAULT,
19 COMP_MODE_DEFAULT,
20 COMP_MODE_INLINE,
20 COMP_MODE_INLINE,
21 COMP_MODE_PLAIN,
21 COMP_MODE_PLAIN,
22 DELTA_BASE_REUSE_FORCE,
22 DELTA_BASE_REUSE_FORCE,
23 DELTA_BASE_REUSE_NO,
23 DELTA_BASE_REUSE_NO,
24 KIND_CHANGELOG,
24 KIND_CHANGELOG,
25 KIND_FILELOG,
25 KIND_FILELOG,
26 KIND_MANIFESTLOG,
26 KIND_MANIFESTLOG,
27 REVIDX_ISCENSORED,
27 REVIDX_ISCENSORED,
28 REVIDX_RAWTEXT_CHANGING_FLAGS,
28 REVIDX_RAWTEXT_CHANGING_FLAGS,
29 )
29 )
30
30
31 from ..thirdparty import attr
31 from ..thirdparty import attr
32
32
33 from .. import (
33 from .. import (
34 error,
34 error,
35 mdiff,
35 mdiff,
36 util,
36 util,
37 )
37 )
38
38
39 from . import flagutil
39 from . import flagutil
40
40
41 # maximum <delta-chain-data>/<revision-text-length> ratio
41 # maximum <delta-chain-data>/<revision-text-length> ratio
42 LIMIT_DELTA2TEXT = 2
42 LIMIT_DELTA2TEXT = 2
43
43
44
44
45 class _testrevlog:
45 class _testrevlog:
46 """minimalist fake revlog to use in doctests"""
46 """minimalist fake revlog to use in doctests"""
47
47
48 def __init__(self, data, density=0.5, mingap=0, snapshot=()):
48 def __init__(self, data, density=0.5, mingap=0, snapshot=()):
49 """data is an list of revision payload boundaries"""
49 """data is an list of revision payload boundaries"""
50 from .. import revlog
50 from .. import revlog
51
51
52 self._data = data
52 self._data = data
53 self.data_config = revlog.DataConfig()
53 self.data_config = revlog.DataConfig()
54 self.data_config.sr_density_threshold = density
54 self.data_config.sr_density_threshold = density
55 self.data_config.sr_min_gap_size = mingap
55 self.data_config.sr_min_gap_size = mingap
56 self.delta_config = revlog.DeltaConfig()
56 self.delta_config = revlog.DeltaConfig()
57 self.feature_config = revlog.FeatureConfig()
57 self.feature_config = revlog.FeatureConfig()
58 self._snapshot = set(snapshot)
58 self._snapshot = set(snapshot)
59 self.index = None
59 self.index = None
60
60
61 def start(self, rev):
61 def start(self, rev):
62 if rev == nullrev:
62 if rev == nullrev:
63 return 0
63 return 0
64 if rev == 0:
64 if rev == 0:
65 return 0
65 return 0
66 return self._data[rev - 1]
66 return self._data[rev - 1]
67
67
68 def end(self, rev):
68 def end(self, rev):
69 if rev == nullrev:
69 if rev == nullrev:
70 return 0
70 return 0
71 return self._data[rev]
71 return self._data[rev]
72
72
73 def length(self, rev):
73 def length(self, rev):
74 return self.end(rev) - self.start(rev)
74 return self.end(rev) - self.start(rev)
75
75
76 def __len__(self):
76 def __len__(self):
77 return len(self._data)
77 return len(self._data)
78
78
79 def issnapshot(self, rev):
79 def issnapshot(self, rev):
80 if rev == nullrev:
80 if rev == nullrev:
81 return True
81 return True
82 return rev in self._snapshot
82 return rev in self._snapshot
83
83
84
84
85 def slicechunk(revlog, revs, targetsize=None):
85 def slicechunk(revlog, revs, targetsize=None):
86 """slice revs to reduce the amount of unrelated data to be read from disk.
86 """slice revs to reduce the amount of unrelated data to be read from disk.
87
87
88 ``revs`` is sliced into groups that should be read in one time.
88 ``revs`` is sliced into groups that should be read in one time.
89 Assume that revs are sorted.
89 Assume that revs are sorted.
90
90
91 The initial chunk is sliced until the overall density (payload/chunks-span
91 The initial chunk is sliced until the overall density (payload/chunks-span
92 ratio) is above `revlog.data_config.sr_density_threshold`. No gap smaller
92 ratio) is above `revlog.data_config.sr_density_threshold`. No gap smaller
93 than `revlog.data_config.sr_min_gap_size` is skipped.
93 than `revlog.data_config.sr_min_gap_size` is skipped.
94
94
95 If `targetsize` is set, no chunk larger than `targetsize` will be yield.
95 If `targetsize` is set, no chunk larger than `targetsize` will be yield.
96 For consistency with other slicing choice, this limit won't go lower than
96 For consistency with other slicing choice, this limit won't go lower than
97 `revlog.data_config.sr_min_gap_size`.
97 `revlog.data_config.sr_min_gap_size`.
98
98
99 If individual revisions chunk are larger than this limit, they will still
99 If individual revisions chunk are larger than this limit, they will still
100 be raised individually.
100 be raised individually.
101
101
102 >>> data = [
102 >>> data = [
103 ... 5, #00 (5)
103 ... 5, #00 (5)
104 ... 10, #01 (5)
104 ... 10, #01 (5)
105 ... 12, #02 (2)
105 ... 12, #02 (2)
106 ... 12, #03 (empty)
106 ... 12, #03 (empty)
107 ... 27, #04 (15)
107 ... 27, #04 (15)
108 ... 31, #05 (4)
108 ... 31, #05 (4)
109 ... 31, #06 (empty)
109 ... 31, #06 (empty)
110 ... 42, #07 (11)
110 ... 42, #07 (11)
111 ... 47, #08 (5)
111 ... 47, #08 (5)
112 ... 47, #09 (empty)
112 ... 47, #09 (empty)
113 ... 48, #10 (1)
113 ... 48, #10 (1)
114 ... 51, #11 (3)
114 ... 51, #11 (3)
115 ... 74, #12 (23)
115 ... 74, #12 (23)
116 ... 85, #13 (11)
116 ... 85, #13 (11)
117 ... 86, #14 (1)
117 ... 86, #14 (1)
118 ... 91, #15 (5)
118 ... 91, #15 (5)
119 ... ]
119 ... ]
120 >>> revlog = _testrevlog(data, snapshot=range(16))
120 >>> revlog = _testrevlog(data, snapshot=range(16))
121
121
122 >>> list(slicechunk(revlog, list(range(16))))
122 >>> list(slicechunk(revlog, list(range(16))))
123 [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]
123 [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]
124 >>> list(slicechunk(revlog, [0, 15]))
124 >>> list(slicechunk(revlog, [0, 15]))
125 [[0], [15]]
125 [[0], [15]]
126 >>> list(slicechunk(revlog, [0, 11, 15]))
126 >>> list(slicechunk(revlog, [0, 11, 15]))
127 [[0], [11], [15]]
127 [[0], [11], [15]]
128 >>> list(slicechunk(revlog, [0, 11, 13, 15]))
128 >>> list(slicechunk(revlog, [0, 11, 13, 15]))
129 [[0], [11, 13, 15]]
129 [[0], [11, 13, 15]]
130 >>> list(slicechunk(revlog, [1, 2, 3, 5, 8, 10, 11, 14]))
130 >>> list(slicechunk(revlog, [1, 2, 3, 5, 8, 10, 11, 14]))
131 [[1, 2], [5, 8, 10, 11], [14]]
131 [[1, 2], [5, 8, 10, 11], [14]]
132
132
133 Slicing with a maximum chunk size
133 Slicing with a maximum chunk size
134 >>> list(slicechunk(revlog, [0, 11, 13, 15], targetsize=15))
134 >>> list(slicechunk(revlog, [0, 11, 13, 15], targetsize=15))
135 [[0], [11], [13], [15]]
135 [[0], [11], [13], [15]]
136 >>> list(slicechunk(revlog, [0, 11, 13, 15], targetsize=20))
136 >>> list(slicechunk(revlog, [0, 11, 13, 15], targetsize=20))
137 [[0], [11], [13, 15]]
137 [[0], [11], [13, 15]]
138
138
139 Slicing involving nullrev
139 Slicing involving nullrev
140 >>> list(slicechunk(revlog, [-1, 0, 11, 13, 15], targetsize=20))
140 >>> list(slicechunk(revlog, [-1, 0, 11, 13, 15], targetsize=20))
141 [[-1, 0], [11], [13, 15]]
141 [[-1, 0], [11], [13, 15]]
142 >>> list(slicechunk(revlog, [-1, 13, 15], targetsize=5))
142 >>> list(slicechunk(revlog, [-1, 13, 15], targetsize=5))
143 [[-1], [13], [15]]
143 [[-1], [13], [15]]
144 """
144 """
145 if targetsize is not None:
145 if targetsize is not None:
146 targetsize = max(targetsize, revlog.data_config.sr_min_gap_size)
146 targetsize = max(targetsize, revlog.data_config.sr_min_gap_size)
147 # targetsize should not be specified when evaluating delta candidates:
147 # targetsize should not be specified when evaluating delta candidates:
148 # * targetsize is used to ensure we stay within specification when reading,
148 # * targetsize is used to ensure we stay within specification when reading,
149 densityslicing = getattr(revlog.index, 'slicechunktodensity', None)
149 densityslicing = getattr(revlog.index, 'slicechunktodensity', None)
150 if densityslicing is None:
150 if densityslicing is None:
151 densityslicing = lambda x, y, z: _slicechunktodensity(revlog, x, y, z)
151 densityslicing = lambda x, y, z: _slicechunktodensity(revlog, x, y, z)
152 for chunk in densityslicing(
152 for chunk in densityslicing(
153 revs,
153 revs,
154 revlog.data_config.sr_density_threshold,
154 revlog.data_config.sr_density_threshold,
155 revlog.data_config.sr_min_gap_size,
155 revlog.data_config.sr_min_gap_size,
156 ):
156 ):
157 for subchunk in _slicechunktosize(revlog, chunk, targetsize):
157 for subchunk in _slicechunktosize(revlog, chunk, targetsize):
158 yield subchunk
158 yield subchunk
159
159
160
160
161 def _slicechunktosize(revlog, revs, targetsize=None):
161 def _slicechunktosize(revlog, revs, targetsize=None):
162 """slice revs to match the target size
162 """slice revs to match the target size
163
163
164 This is intended to be used on chunk that density slicing selected by that
164 This is intended to be used on chunk that density slicing selected by that
165 are still too large compared to the read garantee of revlog. This might
165 are still too large compared to the read garantee of revlog. This might
166 happens when "minimal gap size" interrupted the slicing or when chain are
166 happens when "minimal gap size" interrupted the slicing or when chain are
167 built in a way that create large blocks next to each other.
167 built in a way that create large blocks next to each other.
168
168
169 >>> data = [
169 >>> data = [
170 ... 3, #0 (3)
170 ... 3, #0 (3)
171 ... 5, #1 (2)
171 ... 5, #1 (2)
172 ... 6, #2 (1)
172 ... 6, #2 (1)
173 ... 8, #3 (2)
173 ... 8, #3 (2)
174 ... 8, #4 (empty)
174 ... 8, #4 (empty)
175 ... 11, #5 (3)
175 ... 11, #5 (3)
176 ... 12, #6 (1)
176 ... 12, #6 (1)
177 ... 13, #7 (1)
177 ... 13, #7 (1)
178 ... 14, #8 (1)
178 ... 14, #8 (1)
179 ... ]
179 ... ]
180
180
181 == All snapshots cases ==
181 == All snapshots cases ==
182 >>> revlog = _testrevlog(data, snapshot=range(9))
182 >>> revlog = _testrevlog(data, snapshot=range(9))
183
183
184 Cases where chunk is already small enough
184 Cases where chunk is already small enough
185 >>> list(_slicechunktosize(revlog, [0], 3))
185 >>> list(_slicechunktosize(revlog, [0], 3))
186 [[0]]
186 [[0]]
187 >>> list(_slicechunktosize(revlog, [6, 7], 3))
187 >>> list(_slicechunktosize(revlog, [6, 7], 3))
188 [[6, 7]]
188 [[6, 7]]
189 >>> list(_slicechunktosize(revlog, [0], None))
189 >>> list(_slicechunktosize(revlog, [0], None))
190 [[0]]
190 [[0]]
191 >>> list(_slicechunktosize(revlog, [6, 7], None))
191 >>> list(_slicechunktosize(revlog, [6, 7], None))
192 [[6, 7]]
192 [[6, 7]]
193
193
194 cases where we need actual slicing
194 cases where we need actual slicing
195 >>> list(_slicechunktosize(revlog, [0, 1], 3))
195 >>> list(_slicechunktosize(revlog, [0, 1], 3))
196 [[0], [1]]
196 [[0], [1]]
197 >>> list(_slicechunktosize(revlog, [1, 3], 3))
197 >>> list(_slicechunktosize(revlog, [1, 3], 3))
198 [[1], [3]]
198 [[1], [3]]
199 >>> list(_slicechunktosize(revlog, [1, 2, 3], 3))
199 >>> list(_slicechunktosize(revlog, [1, 2, 3], 3))
200 [[1, 2], [3]]
200 [[1, 2], [3]]
201 >>> list(_slicechunktosize(revlog, [3, 5], 3))
201 >>> list(_slicechunktosize(revlog, [3, 5], 3))
202 [[3], [5]]
202 [[3], [5]]
203 >>> list(_slicechunktosize(revlog, [3, 4, 5], 3))
203 >>> list(_slicechunktosize(revlog, [3, 4, 5], 3))
204 [[3], [5]]
204 [[3], [5]]
205 >>> list(_slicechunktosize(revlog, [5, 6, 7, 8], 3))
205 >>> list(_slicechunktosize(revlog, [5, 6, 7, 8], 3))
206 [[5], [6, 7, 8]]
206 [[5], [6, 7, 8]]
207 >>> list(_slicechunktosize(revlog, [0, 1, 2, 3, 4, 5, 6, 7, 8], 3))
207 >>> list(_slicechunktosize(revlog, [0, 1, 2, 3, 4, 5, 6, 7, 8], 3))
208 [[0], [1, 2], [3], [5], [6, 7, 8]]
208 [[0], [1, 2], [3], [5], [6, 7, 8]]
209
209
210 Case with too large individual chunk (must return valid chunk)
210 Case with too large individual chunk (must return valid chunk)
211 >>> list(_slicechunktosize(revlog, [0, 1], 2))
211 >>> list(_slicechunktosize(revlog, [0, 1], 2))
212 [[0], [1]]
212 [[0], [1]]
213 >>> list(_slicechunktosize(revlog, [1, 3], 1))
213 >>> list(_slicechunktosize(revlog, [1, 3], 1))
214 [[1], [3]]
214 [[1], [3]]
215 >>> list(_slicechunktosize(revlog, [3, 4, 5], 2))
215 >>> list(_slicechunktosize(revlog, [3, 4, 5], 2))
216 [[3], [5]]
216 [[3], [5]]
217
217
218 == No Snapshot cases ==
218 == No Snapshot cases ==
219 >>> revlog = _testrevlog(data)
219 >>> revlog = _testrevlog(data)
220
220
221 Cases where chunk is already small enough
221 Cases where chunk is already small enough
222 >>> list(_slicechunktosize(revlog, [0], 3))
222 >>> list(_slicechunktosize(revlog, [0], 3))
223 [[0]]
223 [[0]]
224 >>> list(_slicechunktosize(revlog, [6, 7], 3))
224 >>> list(_slicechunktosize(revlog, [6, 7], 3))
225 [[6, 7]]
225 [[6, 7]]
226 >>> list(_slicechunktosize(revlog, [0], None))
226 >>> list(_slicechunktosize(revlog, [0], None))
227 [[0]]
227 [[0]]
228 >>> list(_slicechunktosize(revlog, [6, 7], None))
228 >>> list(_slicechunktosize(revlog, [6, 7], None))
229 [[6, 7]]
229 [[6, 7]]
230
230
231 cases where we need actual slicing
231 cases where we need actual slicing
232 >>> list(_slicechunktosize(revlog, [0, 1], 3))
232 >>> list(_slicechunktosize(revlog, [0, 1], 3))
233 [[0], [1]]
233 [[0], [1]]
234 >>> list(_slicechunktosize(revlog, [1, 3], 3))
234 >>> list(_slicechunktosize(revlog, [1, 3], 3))
235 [[1], [3]]
235 [[1], [3]]
236 >>> list(_slicechunktosize(revlog, [1, 2, 3], 3))
236 >>> list(_slicechunktosize(revlog, [1, 2, 3], 3))
237 [[1], [2, 3]]
237 [[1], [2, 3]]
238 >>> list(_slicechunktosize(revlog, [3, 5], 3))
238 >>> list(_slicechunktosize(revlog, [3, 5], 3))
239 [[3], [5]]
239 [[3], [5]]
240 >>> list(_slicechunktosize(revlog, [3, 4, 5], 3))
240 >>> list(_slicechunktosize(revlog, [3, 4, 5], 3))
241 [[3], [4, 5]]
241 [[3], [4, 5]]
242 >>> list(_slicechunktosize(revlog, [5, 6, 7, 8], 3))
242 >>> list(_slicechunktosize(revlog, [5, 6, 7, 8], 3))
243 [[5], [6, 7, 8]]
243 [[5], [6, 7, 8]]
244 >>> list(_slicechunktosize(revlog, [0, 1, 2, 3, 4, 5, 6, 7, 8], 3))
244 >>> list(_slicechunktosize(revlog, [0, 1, 2, 3, 4, 5, 6, 7, 8], 3))
245 [[0], [1, 2], [3], [5], [6, 7, 8]]
245 [[0], [1, 2], [3], [5], [6, 7, 8]]
246
246
247 Case with too large individual chunk (must return valid chunk)
247 Case with too large individual chunk (must return valid chunk)
248 >>> list(_slicechunktosize(revlog, [0, 1], 2))
248 >>> list(_slicechunktosize(revlog, [0, 1], 2))
249 [[0], [1]]
249 [[0], [1]]
250 >>> list(_slicechunktosize(revlog, [1, 3], 1))
250 >>> list(_slicechunktosize(revlog, [1, 3], 1))
251 [[1], [3]]
251 [[1], [3]]
252 >>> list(_slicechunktosize(revlog, [3, 4, 5], 2))
252 >>> list(_slicechunktosize(revlog, [3, 4, 5], 2))
253 [[3], [5]]
253 [[3], [5]]
254
254
255 == mixed case ==
255 == mixed case ==
256 >>> revlog = _testrevlog(data, snapshot=[0, 1, 2])
256 >>> revlog = _testrevlog(data, snapshot=[0, 1, 2])
257 >>> list(_slicechunktosize(revlog, list(range(9)), 5))
257 >>> list(_slicechunktosize(revlog, list(range(9)), 5))
258 [[0, 1], [2], [3, 4, 5], [6, 7, 8]]
258 [[0, 1], [2], [3, 4, 5], [6, 7, 8]]
259 """
259 """
260 assert targetsize is None or 0 <= targetsize
260 assert targetsize is None or 0 <= targetsize
261 startdata = revlog.start(revs[0])
261 startdata = revlog.start(revs[0])
262 enddata = revlog.end(revs[-1])
262 enddata = revlog.end(revs[-1])
263 fullspan = enddata - startdata
263 fullspan = enddata - startdata
264 if targetsize is None or fullspan <= targetsize:
264 if targetsize is None or fullspan <= targetsize:
265 yield revs
265 yield revs
266 return
266 return
267
267
268 startrevidx = 0
268 startrevidx = 0
269 endrevidx = 1
269 endrevidx = 1
270 iterrevs = enumerate(revs)
270 iterrevs = enumerate(revs)
271 next(iterrevs) # skip first rev.
271 next(iterrevs) # skip first rev.
272 # first step: get snapshots out of the way
272 # first step: get snapshots out of the way
273 for idx, r in iterrevs:
273 for idx, r in iterrevs:
274 span = revlog.end(r) - startdata
274 span = revlog.end(r) - startdata
275 snapshot = revlog.issnapshot(r)
275 snapshot = revlog.issnapshot(r)
276 if span <= targetsize and snapshot:
276 if span <= targetsize and snapshot:
277 endrevidx = idx + 1
277 endrevidx = idx + 1
278 else:
278 else:
279 chunk = _trimchunk(revlog, revs, startrevidx, endrevidx)
279 chunk = _trimchunk(revlog, revs, startrevidx, endrevidx)
280 if chunk:
280 if chunk:
281 yield chunk
281 yield chunk
282 startrevidx = idx
282 startrevidx = idx
283 startdata = revlog.start(r)
283 startdata = revlog.start(r)
284 endrevidx = idx + 1
284 endrevidx = idx + 1
285 if not snapshot:
285 if not snapshot:
286 break
286 break
287
287
288 # for the others, we use binary slicing to quickly converge toward valid
288 # for the others, we use binary slicing to quickly converge toward valid
289 # chunks (otherwise, we might end up looking for start/end of many
289 # chunks (otherwise, we might end up looking for start/end of many
290 # revisions). This logic is not looking for the perfect slicing point, it
290 # revisions). This logic is not looking for the perfect slicing point, it
291 # focuses on quickly converging toward valid chunks.
291 # focuses on quickly converging toward valid chunks.
292 nbitem = len(revs)
292 nbitem = len(revs)
293 while (enddata - startdata) > targetsize:
293 while (enddata - startdata) > targetsize:
294 endrevidx = nbitem
294 endrevidx = nbitem
295 if nbitem - startrevidx <= 1:
295 if nbitem - startrevidx <= 1:
296 break # protect against individual chunk larger than limit
296 break # protect against individual chunk larger than limit
297 localenddata = revlog.end(revs[endrevidx - 1])
297 localenddata = revlog.end(revs[endrevidx - 1])
298 span = localenddata - startdata
298 span = localenddata - startdata
299 while span > targetsize:
299 while span > targetsize:
300 if endrevidx - startrevidx <= 1:
300 if endrevidx - startrevidx <= 1:
301 break # protect against individual chunk larger than limit
301 break # protect against individual chunk larger than limit
302 endrevidx -= (endrevidx - startrevidx) // 2
302 endrevidx -= (endrevidx - startrevidx) // 2
303 localenddata = revlog.end(revs[endrevidx - 1])
303 localenddata = revlog.end(revs[endrevidx - 1])
304 span = localenddata - startdata
304 span = localenddata - startdata
305 chunk = _trimchunk(revlog, revs, startrevidx, endrevidx)
305 chunk = _trimchunk(revlog, revs, startrevidx, endrevidx)
306 if chunk:
306 if chunk:
307 yield chunk
307 yield chunk
308 startrevidx = endrevidx
308 startrevidx = endrevidx
309 startdata = revlog.start(revs[startrevidx])
309 startdata = revlog.start(revs[startrevidx])
310
310
311 chunk = _trimchunk(revlog, revs, startrevidx)
311 chunk = _trimchunk(revlog, revs, startrevidx)
312 if chunk:
312 if chunk:
313 yield chunk
313 yield chunk
314
314
315
315
316 def _slicechunktodensity(revlog, revs, targetdensity=0.5, mingapsize=0):
316 def _slicechunktodensity(revlog, revs, targetdensity=0.5, mingapsize=0):
317 """slice revs to reduce the amount of unrelated data to be read from disk.
317 """slice revs to reduce the amount of unrelated data to be read from disk.
318
318
319 ``revs`` is sliced into groups that should be read in one time.
319 ``revs`` is sliced into groups that should be read in one time.
320 Assume that revs are sorted.
320 Assume that revs are sorted.
321
321
322 The initial chunk is sliced until the overall density (payload/chunks-span
322 The initial chunk is sliced until the overall density (payload/chunks-span
323 ratio) is above `targetdensity`. No gap smaller than `mingapsize` is
323 ratio) is above `targetdensity`. No gap smaller than `mingapsize` is
324 skipped.
324 skipped.
325
325
326 >>> revlog = _testrevlog([
326 >>> revlog = _testrevlog([
327 ... 5, #00 (5)
327 ... 5, #00 (5)
328 ... 10, #01 (5)
328 ... 10, #01 (5)
329 ... 12, #02 (2)
329 ... 12, #02 (2)
330 ... 12, #03 (empty)
330 ... 12, #03 (empty)
331 ... 27, #04 (15)
331 ... 27, #04 (15)
332 ... 31, #05 (4)
332 ... 31, #05 (4)
333 ... 31, #06 (empty)
333 ... 31, #06 (empty)
334 ... 42, #07 (11)
334 ... 42, #07 (11)
335 ... 47, #08 (5)
335 ... 47, #08 (5)
336 ... 47, #09 (empty)
336 ... 47, #09 (empty)
337 ... 48, #10 (1)
337 ... 48, #10 (1)
338 ... 51, #11 (3)
338 ... 51, #11 (3)
339 ... 74, #12 (23)
339 ... 74, #12 (23)
340 ... 85, #13 (11)
340 ... 85, #13 (11)
341 ... 86, #14 (1)
341 ... 86, #14 (1)
342 ... 91, #15 (5)
342 ... 91, #15 (5)
343 ... ])
343 ... ])
344
344
345 >>> list(_slicechunktodensity(revlog, list(range(16))))
345 >>> list(_slicechunktodensity(revlog, list(range(16))))
346 [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]
346 [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]
347 >>> list(_slicechunktodensity(revlog, [0, 15]))
347 >>> list(_slicechunktodensity(revlog, [0, 15]))
348 [[0], [15]]
348 [[0], [15]]
349 >>> list(_slicechunktodensity(revlog, [0, 11, 15]))
349 >>> list(_slicechunktodensity(revlog, [0, 11, 15]))
350 [[0], [11], [15]]
350 [[0], [11], [15]]
351 >>> list(_slicechunktodensity(revlog, [0, 11, 13, 15]))
351 >>> list(_slicechunktodensity(revlog, [0, 11, 13, 15]))
352 [[0], [11, 13, 15]]
352 [[0], [11, 13, 15]]
353 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14]))
353 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14]))
354 [[1, 2], [5, 8, 10, 11], [14]]
354 [[1, 2], [5, 8, 10, 11], [14]]
355 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
355 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
356 ... mingapsize=20))
356 ... mingapsize=20))
357 [[1, 2, 3, 5, 8, 10, 11], [14]]
357 [[1, 2, 3, 5, 8, 10, 11], [14]]
358 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
358 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
359 ... targetdensity=0.95))
359 ... targetdensity=0.95))
360 [[1, 2], [5], [8, 10, 11], [14]]
360 [[1, 2], [5], [8, 10, 11], [14]]
361 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
361 >>> list(_slicechunktodensity(revlog, [1, 2, 3, 5, 8, 10, 11, 14],
362 ... targetdensity=0.95, mingapsize=12))
362 ... targetdensity=0.95, mingapsize=12))
363 [[1, 2], [5, 8, 10, 11], [14]]
363 [[1, 2], [5, 8, 10, 11], [14]]
364 """
364 """
365 start = revlog.start
365 start = revlog.start
366 length = revlog.length
366 length = revlog.length
367
367
368 if len(revs) <= 1:
368 if len(revs) <= 1:
369 yield revs
369 yield revs
370 return
370 return
371
371
372 deltachainspan = segmentspan(revlog, revs)
372 deltachainspan = segmentspan(revlog, revs)
373
373
374 if deltachainspan < mingapsize:
374 if deltachainspan < mingapsize:
375 yield revs
375 yield revs
376 return
376 return
377
377
378 readdata = deltachainspan
378 readdata = deltachainspan
379 chainpayload = sum(length(r) for r in revs)
379 chainpayload = sum(length(r) for r in revs)
380
380
381 if deltachainspan:
381 if deltachainspan:
382 density = chainpayload / float(deltachainspan)
382 density = chainpayload / float(deltachainspan)
383 else:
383 else:
384 density = 1.0
384 density = 1.0
385
385
386 if density >= targetdensity:
386 if density >= targetdensity:
387 yield revs
387 yield revs
388 return
388 return
389
389
390 # Store the gaps in a heap to have them sorted by decreasing size
390 # Store the gaps in a heap to have them sorted by decreasing size
391 gaps = []
391 gaps = []
392 prevend = None
392 prevend = None
393 for i, rev in enumerate(revs):
393 for i, rev in enumerate(revs):
394 revstart = start(rev)
394 revstart = start(rev)
395 revlen = length(rev)
395 revlen = length(rev)
396
396
397 # Skip empty revisions to form larger holes
397 # Skip empty revisions to form larger holes
398 if revlen == 0:
398 if revlen == 0:
399 continue
399 continue
400
400
401 if prevend is not None:
401 if prevend is not None:
402 gapsize = revstart - prevend
402 gapsize = revstart - prevend
403 # only consider holes that are large enough
403 # only consider holes that are large enough
404 if gapsize > mingapsize:
404 if gapsize > mingapsize:
405 gaps.append((gapsize, i))
405 gaps.append((gapsize, i))
406
406
407 prevend = revstart + revlen
407 prevend = revstart + revlen
408 # sort the gaps to pop them from largest to small
408 # sort the gaps to pop them from largest to small
409 gaps.sort()
409 gaps.sort()
410
410
411 # Collect the indices of the largest holes until the density is acceptable
411 # Collect the indices of the largest holes until the density is acceptable
412 selected = []
412 selected = []
413 while gaps and density < targetdensity:
413 while gaps and density < targetdensity:
414 gapsize, gapidx = gaps.pop()
414 gapsize, gapidx = gaps.pop()
415
415
416 selected.append(gapidx)
416 selected.append(gapidx)
417
417
418 # the gap sizes are stored as negatives to be sorted decreasingly
418 # the gap sizes are stored as negatives to be sorted decreasingly
419 # by the heap
419 # by the heap
420 readdata -= gapsize
420 readdata -= gapsize
421 if readdata > 0:
421 if readdata > 0:
422 density = chainpayload / float(readdata)
422 density = chainpayload / float(readdata)
423 else:
423 else:
424 density = 1.0
424 density = 1.0
425 selected.sort()
425 selected.sort()
426
426
427 # Cut the revs at collected indices
427 # Cut the revs at collected indices
428 previdx = 0
428 previdx = 0
429 for idx in selected:
429 for idx in selected:
430
430
431 chunk = _trimchunk(revlog, revs, previdx, idx)
431 chunk = _trimchunk(revlog, revs, previdx, idx)
432 if chunk:
432 if chunk:
433 yield chunk
433 yield chunk
434
434
435 previdx = idx
435 previdx = idx
436
436
437 chunk = _trimchunk(revlog, revs, previdx)
437 chunk = _trimchunk(revlog, revs, previdx)
438 if chunk:
438 if chunk:
439 yield chunk
439 yield chunk
440
440
441
441
442 def _trimchunk(revlog, revs, startidx, endidx=None):
442 def _trimchunk(revlog, revs, startidx, endidx=None):
443 """returns revs[startidx:endidx] without empty trailing revs
443 """returns revs[startidx:endidx] without empty trailing revs
444
444
445 Doctest Setup
445 Doctest Setup
446 >>> revlog = _testrevlog([
446 >>> revlog = _testrevlog([
447 ... 5, #0
447 ... 5, #0
448 ... 10, #1
448 ... 10, #1
449 ... 12, #2
449 ... 12, #2
450 ... 12, #3 (empty)
450 ... 12, #3 (empty)
451 ... 17, #4
451 ... 17, #4
452 ... 21, #5
452 ... 21, #5
453 ... 21, #6 (empty)
453 ... 21, #6 (empty)
454 ... ])
454 ... ])
455
455
456 Contiguous cases:
456 Contiguous cases:
457 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0)
457 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0)
458 [0, 1, 2, 3, 4, 5]
458 [0, 1, 2, 3, 4, 5]
459 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0, 5)
459 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0, 5)
460 [0, 1, 2, 3, 4]
460 [0, 1, 2, 3, 4]
461 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0, 4)
461 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 0, 4)
462 [0, 1, 2]
462 [0, 1, 2]
463 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 2, 4)
463 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 2, 4)
464 [2]
464 [2]
465 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 3)
465 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 3)
466 [3, 4, 5]
466 [3, 4, 5]
467 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 3, 5)
467 >>> _trimchunk(revlog, [0, 1, 2, 3, 4, 5, 6], 3, 5)
468 [3, 4]
468 [3, 4]
469
469
470 Discontiguous cases:
470 Discontiguous cases:
471 >>> _trimchunk(revlog, [1, 3, 5, 6], 0)
471 >>> _trimchunk(revlog, [1, 3, 5, 6], 0)
472 [1, 3, 5]
472 [1, 3, 5]
473 >>> _trimchunk(revlog, [1, 3, 5, 6], 0, 2)
473 >>> _trimchunk(revlog, [1, 3, 5, 6], 0, 2)
474 [1]
474 [1]
475 >>> _trimchunk(revlog, [1, 3, 5, 6], 1, 3)
475 >>> _trimchunk(revlog, [1, 3, 5, 6], 1, 3)
476 [3, 5]
476 [3, 5]
477 >>> _trimchunk(revlog, [1, 3, 5, 6], 1)
477 >>> _trimchunk(revlog, [1, 3, 5, 6], 1)
478 [3, 5]
478 [3, 5]
479 """
479 """
480 length = revlog.length
480 length = revlog.length
481
481
482 if endidx is None:
482 if endidx is None:
483 endidx = len(revs)
483 endidx = len(revs)
484
484
485 # If we have a non-emtpy delta candidate, there are nothing to trim
485 # If we have a non-emtpy delta candidate, there are nothing to trim
486 if revs[endidx - 1] < len(revlog):
486 if revs[endidx - 1] < len(revlog):
487 # Trim empty revs at the end, except the very first revision of a chain
487 # Trim empty revs at the end, except the very first revision of a chain
488 while (
488 while (
489 endidx > 1 and endidx > startidx and length(revs[endidx - 1]) == 0
489 endidx > 1 and endidx > startidx and length(revs[endidx - 1]) == 0
490 ):
490 ):
491 endidx -= 1
491 endidx -= 1
492
492
493 return revs[startidx:endidx]
493 return revs[startidx:endidx]
494
494
495
495
496 def segmentspan(revlog, revs):
496 def segmentspan(revlog, revs):
497 """Get the byte span of a segment of revisions
497 """Get the byte span of a segment of revisions
498
498
499 revs is a sorted array of revision numbers
499 revs is a sorted array of revision numbers
500
500
501 >>> revlog = _testrevlog([
501 >>> revlog = _testrevlog([
502 ... 5, #0
502 ... 5, #0
503 ... 10, #1
503 ... 10, #1
504 ... 12, #2
504 ... 12, #2
505 ... 12, #3 (empty)
505 ... 12, #3 (empty)
506 ... 17, #4
506 ... 17, #4
507 ... ])
507 ... ])
508
508
509 >>> segmentspan(revlog, [0, 1, 2, 3, 4])
509 >>> segmentspan(revlog, [0, 1, 2, 3, 4])
510 17
510 17
511 >>> segmentspan(revlog, [0, 4])
511 >>> segmentspan(revlog, [0, 4])
512 17
512 17
513 >>> segmentspan(revlog, [3, 4])
513 >>> segmentspan(revlog, [3, 4])
514 5
514 5
515 >>> segmentspan(revlog, [1, 2, 3,])
515 >>> segmentspan(revlog, [1, 2, 3,])
516 7
516 7
517 >>> segmentspan(revlog, [1, 3])
517 >>> segmentspan(revlog, [1, 3])
518 7
518 7
519 """
519 """
520 if not revs:
520 if not revs:
521 return 0
521 return 0
522 end = revlog.end(revs[-1])
522 end = revlog.end(revs[-1])
523 return end - revlog.start(revs[0])
523 return end - revlog.start(revs[0])
524
524
525
525
526 def _textfromdelta(revlog, baserev, delta, p1, p2, flags, expectednode):
526 def _textfromdelta(revlog, baserev, delta, p1, p2, flags, expectednode):
527 """build full text from a (base, delta) pair and other metadata"""
527 """build full text from a (base, delta) pair and other metadata"""
528 # special case deltas which replace entire base; no need to decode
528 # special case deltas which replace entire base; no need to decode
529 # base revision. this neatly avoids censored bases, which throw when
529 # base revision. this neatly avoids censored bases, which throw when
530 # they're decoded.
530 # they're decoded.
531 hlen = struct.calcsize(b">lll")
531 hlen = struct.calcsize(b">lll")
532 if delta[:hlen] == mdiff.replacediffheader(
532 if delta[:hlen] == mdiff.replacediffheader(
533 revlog.rawsize(baserev), len(delta) - hlen
533 revlog.rawsize(baserev), len(delta) - hlen
534 ):
534 ):
535 fulltext = delta[hlen:]
535 fulltext = delta[hlen:]
536 else:
536 else:
537 # deltabase is rawtext before changed by flag processors, which is
537 # deltabase is rawtext before changed by flag processors, which is
538 # equivalent to non-raw text
538 # equivalent to non-raw text
539 basetext = revlog.revision(baserev)
539 basetext = revlog.revision(baserev)
540 fulltext = mdiff.patch(basetext, delta)
540 fulltext = mdiff.patch(basetext, delta)
541
541
542 try:
542 try:
543 validatehash = flagutil.processflagsraw(revlog, fulltext, flags)
543 validatehash = flagutil.processflagsraw(revlog, fulltext, flags)
544 if validatehash:
544 if validatehash:
545 revlog.checkhash(fulltext, expectednode, p1=p1, p2=p2)
545 revlog.checkhash(fulltext, expectednode, p1=p1, p2=p2)
546 if flags & REVIDX_ISCENSORED:
546 if flags & REVIDX_ISCENSORED:
547 raise error.StorageError(
547 raise error.StorageError(
548 _(b'node %s is not censored') % expectednode
548 _(b'node %s is not censored') % expectednode
549 )
549 )
550 except error.CensoredNodeError:
550 except error.CensoredNodeError:
551 # must pass the censored index flag to add censored revisions
551 # must pass the censored index flag to add censored revisions
552 if not flags & REVIDX_ISCENSORED:
552 if not flags & REVIDX_ISCENSORED:
553 raise
553 raise
554 return fulltext
554 return fulltext
555
555
556
556
557 @attr.s(slots=True, frozen=True)
557 @attr.s(slots=True, frozen=True)
558 class _deltainfo:
558 class _deltainfo:
559 distance = attr.ib()
559 distance = attr.ib()
560 deltalen = attr.ib()
560 deltalen = attr.ib()
561 data = attr.ib()
561 data = attr.ib()
562 base = attr.ib()
562 base = attr.ib()
563 chainbase = attr.ib()
563 chainbase = attr.ib()
564 chainlen = attr.ib()
564 chainlen = attr.ib()
565 compresseddeltalen = attr.ib()
565 compresseddeltalen = attr.ib()
566 snapshotdepth = attr.ib()
566 snapshotdepth = attr.ib()
567
567
568
568
569 def drop_u_compression(delta):
569 def drop_u_compression(delta):
570 """turn into a "u" (no-compression) into no-compression without header
570 """turn into a "u" (no-compression) into no-compression without header
571
571
572 This is useful for revlog format that has better compression method.
572 This is useful for revlog format that has better compression method.
573 """
573 """
574 assert delta.data[0] == b'u', delta.data[0]
574 assert delta.data[0] == b'u', delta.data[0]
575 return _deltainfo(
575 return _deltainfo(
576 delta.distance,
576 delta.distance,
577 delta.deltalen - 1,
577 delta.deltalen - 1,
578 (b'', delta.data[1]),
578 (b'', delta.data[1]),
579 delta.base,
579 delta.base,
580 delta.chainbase,
580 delta.chainbase,
581 delta.chainlen,
581 delta.chainlen,
582 delta.compresseddeltalen,
582 delta.compresseddeltalen,
583 delta.snapshotdepth,
583 delta.snapshotdepth,
584 )
584 )
585
585
586
586
587 def is_good_delta_info(revlog, deltainfo, revinfo):
587 def is_good_delta_info(revlog, deltainfo, revinfo):
588 """Returns True if the given delta is good. Good means that it is within
588 """Returns True if the given delta is good. Good means that it is within
589 the disk span, disk size, and chain length bounds that we know to be
589 the disk span, disk size, and chain length bounds that we know to be
590 performant."""
590 performant."""
591 if deltainfo is None:
591 if deltainfo is None:
592 return False
592 return False
593
593
594 # the DELTA_BASE_REUSE_FORCE case should have been taken care of sooner so
594 # the DELTA_BASE_REUSE_FORCE case should have been taken care of sooner so
595 # we should never end up asking such question. Adding the assert as a
595 # we should never end up asking such question. Adding the assert as a
596 # safe-guard to detect anything that would be fishy in this regard.
596 # safe-guard to detect anything that would be fishy in this regard.
597 assert (
597 assert (
598 revinfo.cachedelta is None
598 revinfo.cachedelta is None
599 or revinfo.cachedelta[2] != DELTA_BASE_REUSE_FORCE
599 or revinfo.cachedelta[2] != DELTA_BASE_REUSE_FORCE
600 or not revlog.delta_config.general_delta
600 or not revlog.delta_config.general_delta
601 )
601 )
602
602
603 # - 'deltainfo.distance' is the distance from the base revision --
603 # - 'deltainfo.distance' is the distance from the base revision --
604 # bounding it limits the amount of I/O we need to do.
604 # bounding it limits the amount of I/O we need to do.
605 # - 'deltainfo.compresseddeltalen' is the sum of the total size of
605 # - 'deltainfo.compresseddeltalen' is the sum of the total size of
606 # deltas we need to apply -- bounding it limits the amount of CPU
606 # deltas we need to apply -- bounding it limits the amount of CPU
607 # we consume.
607 # we consume.
608
608
609 textlen = revinfo.textlen
609 textlen = revinfo.textlen
610 defaultmax = textlen * 4
610 defaultmax = textlen * 4
611 maxdist = revlog.delta_config.max_deltachain_span
611 maxdist = revlog.delta_config.max_deltachain_span
612 if not maxdist:
612 if not maxdist:
613 maxdist = deltainfo.distance # ensure the conditional pass
613 maxdist = deltainfo.distance # ensure the conditional pass
614 maxdist = max(maxdist, defaultmax)
614 maxdist = max(maxdist, defaultmax)
615
615
616 # Bad delta from read span:
616 # Bad delta from read span:
617 #
617 #
618 # If the span of data read is larger than the maximum allowed.
618 # If the span of data read is larger than the maximum allowed.
619 #
619 #
620 # In the sparse-revlog case, we rely on the associated "sparse reading"
620 # In the sparse-revlog case, we rely on the associated "sparse reading"
621 # to avoid issue related to the span of data. In theory, it would be
621 # to avoid issue related to the span of data. In theory, it would be
622 # possible to build pathological revlog where delta pattern would lead
622 # possible to build pathological revlog where delta pattern would lead
623 # to too many reads. However, they do not happen in practice at all. So
623 # to too many reads. However, they do not happen in practice at all. So
624 # we skip the span check entirely.
624 # we skip the span check entirely.
625 if not revlog.delta_config.sparse_revlog and maxdist < deltainfo.distance:
625 if not revlog.delta_config.sparse_revlog and maxdist < deltainfo.distance:
626 return False
626 return False
627
627
628 # Bad delta from new delta size:
628 # Bad delta from new delta size:
629 #
629 #
630 # If the delta size is larger than the target text, storing the
630 # If the delta size is larger than the target text, storing the
631 # delta will be inefficient.
631 # delta will be inefficient.
632 if textlen < deltainfo.deltalen:
632 if textlen < deltainfo.deltalen:
633 return False
633 return False
634
634
635 # Bad delta from cumulated payload size:
635 # Bad delta from cumulated payload size:
636 #
636 #
637 # If the sum of delta get larger than K * target text length.
637 # If the sum of delta get larger than K * target text length.
638 if textlen * LIMIT_DELTA2TEXT < deltainfo.compresseddeltalen:
638 if textlen * LIMIT_DELTA2TEXT < deltainfo.compresseddeltalen:
639 return False
639 return False
640
640
641 # Bad delta from chain length:
641 # Bad delta from chain length:
642 #
642 #
643 # If the number of delta in the chain gets too high.
643 # If the number of delta in the chain gets too high.
644 if (
644 if (
645 revlog.delta_config.max_chain_len
645 revlog.delta_config.max_chain_len
646 and revlog.delta_config.max_chain_len < deltainfo.chainlen
646 and revlog.delta_config.max_chain_len < deltainfo.chainlen
647 ):
647 ):
648 return False
648 return False
649
649
650 # bad delta from intermediate snapshot size limit
650 # bad delta from intermediate snapshot size limit
651 #
651 #
652 # If an intermediate snapshot size is higher than the limit. The
652 # If an intermediate snapshot size is higher than the limit. The
653 # limit exist to prevent endless chain of intermediate delta to be
653 # limit exist to prevent endless chain of intermediate delta to be
654 # created.
654 # created.
655 if (
655 if (
656 deltainfo.snapshotdepth is not None
656 deltainfo.snapshotdepth is not None
657 and (textlen >> deltainfo.snapshotdepth) < deltainfo.deltalen
657 and (textlen >> deltainfo.snapshotdepth) < deltainfo.deltalen
658 ):
658 ):
659 return False
659 return False
660
660
661 # bad delta if new intermediate snapshot is larger than the previous
661 # bad delta if new intermediate snapshot is larger than the previous
662 # snapshot
662 # snapshot
663 if (
663 if (
664 deltainfo.snapshotdepth
664 deltainfo.snapshotdepth
665 and revlog.length(deltainfo.base) < deltainfo.deltalen
665 and revlog.length(deltainfo.base) < deltainfo.deltalen
666 ):
666 ):
667 return False
667 return False
668
668
669 return True
669 return True
670
670
671
671
672 # If a revision's full text is that much bigger than a base candidate full
672 # If a revision's full text is that much bigger than a base candidate full
673 # text's, it is very unlikely that it will produce a valid delta. We no longer
673 # text's, it is very unlikely that it will produce a valid delta. We no longer
674 # consider these candidates.
674 # consider these candidates.
675 LIMIT_BASE2TEXT = 500
675 LIMIT_BASE2TEXT = 500
676
676
677
677
678 def _candidategroups(
678 def _candidategroups(
679 revlog,
679 revlog,
680 textlen,
680 textlen,
681 p1,
681 p1,
682 p2,
682 p2,
683 cachedelta,
683 cachedelta,
684 excluded_bases=None,
684 excluded_bases=None,
685 target_rev=None,
685 target_rev=None,
686 snapshot_cache=None,
686 snapshot_cache=None,
687 ):
687 ):
688 """Provides group of revision to be tested as delta base
688 """Provides group of revision to be tested as delta base
689
689
690 This top level function focus on emitting groups with unique and worthwhile
690 This top level function focus on emitting groups with unique and worthwhile
691 content. See _raw_candidate_groups for details about the group order.
691 content. See _raw_candidate_groups for details about the group order.
692 """
692 """
693 # should we try to build a delta?
693 # should we try to build a delta?
694 if not (len(revlog) and revlog._storedeltachains):
694 if not (len(revlog) and revlog._storedeltachains):
695 yield None
695 yield None
696 return
696 return
697
697
698 if target_rev is None:
698 if target_rev is None:
699 target_rev = len(revlog)
699 target_rev = len(revlog)
700
700
701 if not revlog.delta_config.general_delta:
701 if not revlog.delta_config.general_delta:
702 # before general delta, there is only one possible delta base
702 # before general delta, there is only one possible delta base
703 yield (target_rev - 1,)
703 yield (target_rev - 1,)
704 yield None
704 yield None
705 return
705 return
706
706
707 # the DELTA_BASE_REUSE_FORCE case should have been taken care of sooner so
707 # the DELTA_BASE_REUSE_FORCE case should have been taken care of sooner so
708 # we should never end up asking such question. Adding the assert as a
708 # we should never end up asking such question. Adding the assert as a
709 # safe-guard to detect anything that would be fishy in this regard.
709 # safe-guard to detect anything that would be fishy in this regard.
710 assert (
710 assert (
711 cachedelta is None
711 cachedelta is None
712 or cachedelta[2] != DELTA_BASE_REUSE_FORCE
712 or cachedelta[2] != DELTA_BASE_REUSE_FORCE
713 or not revlog.delta_config.general_delta
713 or not revlog.delta_config.general_delta
714 )
714 )
715
715
716 deltalength = revlog.length
716 deltalength = revlog.length
717 deltaparent = revlog.deltaparent
717 deltaparent = revlog.deltaparent
718 sparse = revlog.delta_config.sparse_revlog
718 sparse = revlog.delta_config.sparse_revlog
719 good = None
719 good = None
720
720
721 deltas_limit = textlen * LIMIT_DELTA2TEXT
721 deltas_limit = textlen * LIMIT_DELTA2TEXT
722 group_chunk_size = revlog.delta_config.candidate_group_chunk_size
722 group_chunk_size = revlog.delta_config.candidate_group_chunk_size
723
723
724 tested = {nullrev}
724 tested = {nullrev}
725 candidates = _refinedgroups(
725 candidates = _refinedgroups(
726 revlog,
726 revlog,
727 p1,
727 p1,
728 p2,
728 p2,
729 cachedelta,
729 cachedelta,
730 snapshot_cache=snapshot_cache,
730 snapshot_cache=snapshot_cache,
731 )
731 )
732 while True:
732 while True:
733 temptative = candidates.send(good)
733 temptative = candidates.send(good)
734 if temptative is None:
734 if temptative is None:
735 break
735 break
736 group = []
736 group = []
737 for rev in temptative:
737 for rev in temptative:
738 # skip over empty delta (no need to include them in a chain)
738 # skip over empty delta (no need to include them in a chain)
739 while not (rev == nullrev or rev in tested or deltalength(rev)):
739 while not (rev == nullrev or rev in tested or deltalength(rev)):
740 tested.add(rev)
740 tested.add(rev)
741 rev = deltaparent(rev)
741 rev = deltaparent(rev)
742 # no need to try a delta against nullrev, this will be done as a
742 # no need to try a delta against nullrev, this will be done as a
743 # last resort.
743 # last resort.
744 if rev == nullrev:
744 if rev == nullrev:
745 continue
745 continue
746 # filter out revision we tested already
746 # filter out revision we tested already
747 if rev in tested:
747 if rev in tested:
748 continue
748 continue
749
749
750 # an higher authority deamed the base unworthy (e.g. censored)
750 # an higher authority deamed the base unworthy (e.g. censored)
751 if excluded_bases is not None and rev in excluded_bases:
751 if excluded_bases is not None and rev in excluded_bases:
752 tested.add(rev)
752 tested.add(rev)
753 continue
753 continue
754 # We are in some recomputation cases and that rev is too high in
754 # We are in some recomputation cases and that rev is too high in
755 # the revlog
755 # the revlog
756 if target_rev is not None and rev >= target_rev:
756 if target_rev is not None and rev >= target_rev:
757 tested.add(rev)
757 tested.add(rev)
758 continue
758 continue
759 # filter out delta base that will never produce good delta
759 # filter out delta base that will never produce good delta
760 if deltas_limit < revlog.length(rev):
760 if deltas_limit < revlog.length(rev):
761 tested.add(rev)
761 tested.add(rev)
762 continue
762 continue
763 if sparse and revlog.rawsize(rev) < (textlen // LIMIT_BASE2TEXT):
763 if sparse and revlog.rawsize(rev) < (textlen // LIMIT_BASE2TEXT):
764 tested.add(rev)
764 tested.add(rev)
765 continue
765 continue
766 # no delta for rawtext-changing revs (see "candelta" for why)
766 # no delta for rawtext-changing revs (see "candelta" for why)
767 if revlog.flags(rev) & REVIDX_RAWTEXT_CHANGING_FLAGS:
767 if revlog.flags(rev) & REVIDX_RAWTEXT_CHANGING_FLAGS:
768 tested.add(rev)
768 tested.add(rev)
769 continue
769 continue
770
770
771 # If we reach here, we are about to build and test a delta.
771 # If we reach here, we are about to build and test a delta.
772 # The delta building process will compute the chaininfo in all
772 # The delta building process will compute the chaininfo in all
773 # case, since that computation is cached, it is fine to access it
773 # case, since that computation is cached, it is fine to access it
774 # here too.
774 # here too.
775 chainlen, chainsize = revlog._chaininfo(rev)
775 chainlen, chainsize = revlog._chaininfo(rev)
776 # if chain will be too long, skip base
776 # if chain will be too long, skip base
777 if (
777 if (
778 revlog.delta_config.max_chain_len
778 revlog.delta_config.max_chain_len
779 and chainlen >= revlog.delta_config.max_chain_len
779 and chainlen >= revlog.delta_config.max_chain_len
780 ):
780 ):
781 tested.add(rev)
781 tested.add(rev)
782 continue
782 continue
783 # if chain already have too much data, skip base
783 # if chain already have too much data, skip base
784 if deltas_limit < chainsize:
784 if deltas_limit < chainsize:
785 tested.add(rev)
785 tested.add(rev)
786 continue
786 continue
787 if sparse and revlog.upperboundcomp is not None:
787 if sparse and revlog.upperboundcomp is not None:
788 maxcomp = revlog.upperboundcomp
788 maxcomp = revlog.upperboundcomp
789 basenotsnap = (p1, p2, nullrev)
789 basenotsnap = (p1, p2, nullrev)
790 if rev not in basenotsnap and revlog.issnapshot(rev):
790 if rev not in basenotsnap and revlog.issnapshot(rev):
791 snapshotdepth = revlog.snapshotdepth(rev)
791 snapshotdepth = revlog.snapshotdepth(rev)
792 # If text is significantly larger than the base, we can
792 # If text is significantly larger than the base, we can
793 # expect the resulting delta to be proportional to the size
793 # expect the resulting delta to be proportional to the size
794 # difference
794 # difference
795 revsize = revlog.rawsize(rev)
795 revsize = revlog.rawsize(rev)
796 rawsizedistance = max(textlen - revsize, 0)
796 rawsizedistance = max(textlen - revsize, 0)
797 # use an estimate of the compression upper bound.
797 # use an estimate of the compression upper bound.
798 lowestrealisticdeltalen = rawsizedistance // maxcomp
798 lowestrealisticdeltalen = rawsizedistance // maxcomp
799
799
800 # check the absolute constraint on the delta size
800 # check the absolute constraint on the delta size
801 snapshotlimit = textlen >> snapshotdepth
801 snapshotlimit = textlen >> snapshotdepth
802 if snapshotlimit < lowestrealisticdeltalen:
802 if snapshotlimit < lowestrealisticdeltalen:
803 # delta lower bound is larger than accepted upper bound
803 # delta lower bound is larger than accepted upper bound
804 tested.add(rev)
804 tested.add(rev)
805 continue
805 continue
806
806
807 # check the relative constraint on the delta size
807 # check the relative constraint on the delta size
808 revlength = revlog.length(rev)
808 revlength = revlog.length(rev)
809 if revlength < lowestrealisticdeltalen:
809 if revlength < lowestrealisticdeltalen:
810 # delta probable lower bound is larger than target base
810 # delta probable lower bound is larger than target base
811 tested.add(rev)
811 tested.add(rev)
812 continue
812 continue
813
813
814 group.append(rev)
814 group.append(rev)
815 if group:
815 if group:
816 # When the size of the candidate group is big, it can result in a
816 # When the size of the candidate group is big, it can result in a
817 # quite significant performance impact. To reduce this, we can send
817 # quite significant performance impact. To reduce this, we can send
818 # them in smaller batches until the new batch does not provide any
818 # them in smaller batches until the new batch does not provide any
819 # improvements.
819 # improvements.
820 #
820 #
821 # This might reduce the overall efficiency of the compression in
821 # This might reduce the overall efficiency of the compression in
822 # some corner cases, but that should also prevent very pathological
822 # some corner cases, but that should also prevent very pathological
823 # cases from being an issue. (eg. 20 000 candidates).
823 # cases from being an issue. (eg. 20 000 candidates).
824 #
824 #
825 # XXX note that the ordering of the group becomes important as it
825 # XXX note that the ordering of the group becomes important as it
826 # now impacts the final result. The current order is unprocessed
826 # now impacts the final result. The current order is unprocessed
827 # and can be improved.
827 # and can be improved.
828 if group_chunk_size == 0:
828 if group_chunk_size == 0:
829 tested.update(group)
829 tested.update(group)
830 good = yield tuple(group)
830 good = yield tuple(group)
831 else:
831 else:
832 prev_good = good
832 prev_good = good
833 for start in range(0, len(group), group_chunk_size):
833 for start in range(0, len(group), group_chunk_size):
834 sub_group = group[start : start + group_chunk_size]
834 sub_group = group[start : start + group_chunk_size]
835 tested.update(sub_group)
835 tested.update(sub_group)
836 good = yield tuple(sub_group)
836 good = yield tuple(sub_group)
837 if prev_good == good:
837 if prev_good == good:
838 break
838 break
839
839
840 yield None
840 yield None
841
841
842
842
843 def _refinedgroups(revlog, p1, p2, cachedelta, snapshot_cache=None):
843 def _refinedgroups(revlog, p1, p2, cachedelta, snapshot_cache=None):
844 good = None
844 good = None
845 # First we try to reuse a the delta contained in the bundle.
845 # First we try to reuse a the delta contained in the bundle.
846 # (or from the source revlog)
846 # (or from the source revlog)
847 #
847 #
848 # This logic only applies to general delta repositories and can be disabled
848 # This logic only applies to general delta repositories and can be disabled
849 # through configuration. Disabling reuse source delta is useful when
849 # through configuration. Disabling reuse source delta is useful when
850 # we want to make sure we recomputed "optimal" deltas.
850 # we want to make sure we recomputed "optimal" deltas.
851 debug_info = None
851 debug_info = None
852 if cachedelta is not None and cachedelta[2] > DELTA_BASE_REUSE_NO:
852 if cachedelta is not None and cachedelta[2] > DELTA_BASE_REUSE_NO:
853 # Assume what we received from the server is a good choice
853 # Assume what we received from the server is a good choice
854 # build delta will reuse the cache
854 # build delta will reuse the cache
855 if debug_info is not None:
855 if debug_info is not None:
856 debug_info['cached-delta.tested'] += 1
856 debug_info['cached-delta.tested'] += 1
857 good = yield (cachedelta[0],)
857 good = yield (cachedelta[0],)
858 if good is not None:
858 if good is not None:
859 if debug_info is not None:
859 if debug_info is not None:
860 debug_info['cached-delta.accepted'] += 1
860 debug_info['cached-delta.accepted'] += 1
861 yield None
861 yield None
862 return
862 return
863 if snapshot_cache is None:
863 if snapshot_cache is None:
864 snapshot_cache = SnapshotCache()
864 snapshot_cache = SnapshotCache()
865 groups = _rawgroups(
865 groups = _rawgroups(
866 revlog,
866 revlog,
867 p1,
867 p1,
868 p2,
868 p2,
869 cachedelta,
869 cachedelta,
870 snapshot_cache,
870 snapshot_cache,
871 )
871 )
872 for candidates in groups:
872 for candidates in groups:
873 good = yield candidates
873 good = yield candidates
874 if good is not None:
874 if good is not None:
875 break
875 break
876
876
877 # If sparse revlog is enabled, we can try to refine the available deltas
877 # If sparse revlog is enabled, we can try to refine the available deltas
878 if not revlog.delta_config.sparse_revlog:
878 if not revlog.delta_config.sparse_revlog:
879 yield None
879 yield None
880 return
880 return
881
881
882 # if we have a refinable value, try to refine it
882 # if we have a refinable value, try to refine it
883 if good is not None and good not in (p1, p2) and revlog.issnapshot(good):
883 if good is not None and good not in (p1, p2) and revlog.issnapshot(good):
884 # refine snapshot down
884 # refine snapshot down
885 previous = None
885 previous = None
886 while previous != good:
886 while previous != good:
887 previous = good
887 previous = good
888 base = revlog.deltaparent(good)
888 base = revlog.deltaparent(good)
889 if base == nullrev:
889 if base == nullrev:
890 break
890 break
891 good = yield (base,)
891 good = yield (base,)
892 # refine snapshot up
892 # refine snapshot up
893 if not snapshot_cache.snapshots:
893 if not snapshot_cache.snapshots:
894 snapshot_cache.update(revlog, good + 1)
894 snapshot_cache.update(revlog, good + 1)
895 previous = None
895 previous = None
896 while good != previous:
896 while good != previous:
897 previous = good
897 previous = good
898 children = tuple(sorted(c for c in snapshot_cache.snapshots[good]))
898 children = tuple(sorted(c for c in snapshot_cache.snapshots[good]))
899 good = yield children
899 good = yield children
900
900
901 if debug_info is not None:
901 if debug_info is not None:
902 if good is None:
902 if good is None:
903 debug_info['no-solution'] += 1
903 debug_info['no-solution'] += 1
904
904
905 yield None
905 yield None
906
906
907
907
908 def _rawgroups(revlog, p1, p2, cachedelta, snapshot_cache=None):
908 def _rawgroups(revlog, p1, p2, cachedelta, snapshot_cache=None):
909 """Provides group of revision to be tested as delta base
909 """Provides group of revision to be tested as delta base
910
910
911 This lower level function focus on emitting delta theorically interresting
911 This lower level function focus on emitting delta theorically interresting
912 without looking it any practical details.
912 without looking it any practical details.
913
913
914 The group order aims at providing fast or small candidates first.
914 The group order aims at providing fast or small candidates first.
915 """
915 """
916 # Why search for delta base if we cannot use a delta base ?
916 # Why search for delta base if we cannot use a delta base ?
917 assert revlog.delta_config.general_delta
917 assert revlog.delta_config.general_delta
918 # also see issue6056
918 # also see issue6056
919 sparse = revlog.delta_config.sparse_revlog
919 sparse = revlog.delta_config.sparse_revlog
920 curr = len(revlog)
920 curr = len(revlog)
921 prev = curr - 1
921 prev = curr - 1
922 deltachain = lambda rev: revlog._deltachain(rev)[0]
922 deltachain = lambda rev: revlog._deltachain(rev)[0]
923
923
924 # exclude already lazy tested base if any
924 # exclude already lazy tested base if any
925 parents = [p for p in (p1, p2) if p != nullrev]
925 parents = [p for p in (p1, p2) if p != nullrev]
926
926
927 if not revlog.delta_config.delta_both_parents and len(parents) == 2:
927 if not revlog.delta_config.delta_both_parents and len(parents) == 2:
928 parents.sort()
928 parents.sort()
929 # To minimize the chance of having to build a fulltext,
929 # To minimize the chance of having to build a fulltext,
930 # pick first whichever parent is closest to us (max rev)
930 # pick first whichever parent is closest to us (max rev)
931 yield (parents[1],)
931 yield (parents[1],)
932 # then the other one (min rev) if the first did not fit
932 # then the other one (min rev) if the first did not fit
933 yield (parents[0],)
933 yield (parents[0],)
934 elif len(parents) > 0:
934 elif len(parents) > 0:
935 # Test all parents (1 or 2), and keep the best candidate
935 # Test all parents (1 or 2), and keep the best candidate
936 yield parents
936 yield parents
937
937
938 if sparse and parents:
938 if sparse and parents:
939 if snapshot_cache is None:
939 if snapshot_cache is None:
940 # map: base-rev: [snapshot-revs]
940 # map: base-rev: [snapshot-revs]
941 snapshot_cache = SnapshotCache()
941 snapshot_cache = SnapshotCache()
942 # See if we can use an existing snapshot in the parent chains to use as
942 # See if we can use an existing snapshot in the parent chains to use as
943 # a base for a new intermediate-snapshot
943 # a base for a new intermediate-snapshot
944 #
944 #
945 # search for snapshot in parents delta chain
945 # search for snapshot in parents delta chain
946 # map: snapshot-level: snapshot-rev
946 # map: snapshot-level: snapshot-rev
947 parents_snaps = collections.defaultdict(set)
947 parents_snaps = collections.defaultdict(set)
948 candidate_chains = [deltachain(p) for p in parents]
948 candidate_chains = [deltachain(p) for p in parents]
949 for chain in candidate_chains:
949 for chain in candidate_chains:
950 for idx, s in enumerate(chain):
950 for idx, s in enumerate(chain):
951 if not revlog.issnapshot(s):
951 if not revlog.issnapshot(s):
952 break
952 break
953 parents_snaps[idx].add(s)
953 parents_snaps[idx].add(s)
954 snapfloor = min(parents_snaps[0]) + 1
954 snapfloor = min(parents_snaps[0]) + 1
955 snapshot_cache.update(revlog, snapfloor)
955 snapshot_cache.update(revlog, snapfloor)
956 # search for the highest "unrelated" revision
956 # search for the highest "unrelated" revision
957 #
957 #
958 # Adding snapshots used by "unrelated" revision increase the odd we
958 # Adding snapshots used by "unrelated" revision increase the odd we
959 # reuse an independant, yet better snapshot chain.
959 # reuse an independant, yet better snapshot chain.
960 #
960 #
961 # XXX instead of building a set of revisions, we could lazily enumerate
961 # XXX instead of building a set of revisions, we could lazily enumerate
962 # over the chains. That would be more efficient, however we stick to
962 # over the chains. That would be more efficient, however we stick to
963 # simple code for now.
963 # simple code for now.
964 all_revs = set()
964 all_revs = set()
965 for chain in candidate_chains:
965 for chain in candidate_chains:
966 all_revs.update(chain)
966 all_revs.update(chain)
967 other = None
967 other = None
968 for r in revlog.revs(prev, snapfloor):
968 for r in revlog.revs(prev, snapfloor):
969 if r not in all_revs:
969 if r not in all_revs:
970 other = r
970 other = r
971 break
971 break
972 if other is not None:
972 if other is not None:
973 # To avoid unfair competition, we won't use unrelated intermediate
973 # To avoid unfair competition, we won't use unrelated intermediate
974 # snapshot that are deeper than the ones from the parent delta
974 # snapshot that are deeper than the ones from the parent delta
975 # chain.
975 # chain.
976 max_depth = max(parents_snaps.keys())
976 max_depth = max(parents_snaps.keys())
977 chain = deltachain(other)
977 chain = deltachain(other)
978 for depth, s in enumerate(chain):
978 for depth, s in enumerate(chain):
979 if s < snapfloor:
979 if s < snapfloor:
980 continue
980 continue
981 if max_depth < depth:
981 if max_depth < depth:
982 break
982 break
983 if not revlog.issnapshot(s):
983 if not revlog.issnapshot(s):
984 break
984 break
985 parents_snaps[depth].add(s)
985 parents_snaps[depth].add(s)
986 # Test them as possible intermediate snapshot base
986 # Test them as possible intermediate snapshot base
987 # We test them from highest to lowest level. High level one are more
987 # We test them from highest to lowest level. High level one are more
988 # likely to result in small delta
988 # likely to result in small delta
989 floor = None
989 floor = None
990 for idx, snaps in sorted(parents_snaps.items(), reverse=True):
990 for idx, snaps in sorted(parents_snaps.items(), reverse=True):
991 siblings = set()
991 siblings = set()
992 for s in snaps:
992 for s in snaps:
993 siblings.update(snapshot_cache.snapshots[s])
993 siblings.update(snapshot_cache.snapshots[s])
994 # Before considering making a new intermediate snapshot, we check
994 # Before considering making a new intermediate snapshot, we check
995 # if an existing snapshot, children of base we consider, would be
995 # if an existing snapshot, children of base we consider, would be
996 # suitable.
996 # suitable.
997 #
997 #
998 # It give a change to reuse a delta chain "unrelated" to the
998 # It give a change to reuse a delta chain "unrelated" to the
999 # current revision instead of starting our own. Without such
999 # current revision instead of starting our own. Without such
1000 # re-use, topological branches would keep reopening new chains.
1000 # re-use, topological branches would keep reopening new chains.
1001 # Creating more and more snapshot as the repository grow.
1001 # Creating more and more snapshot as the repository grow.
1002
1002
1003 if floor is not None:
1003 if floor is not None:
1004 # We only do this for siblings created after the one in our
1004 # We only do this for siblings created after the one in our
1005 # parent's delta chain. Those created before has less chances
1005 # parent's delta chain. Those created before has less chances
1006 # to be valid base since our ancestors had to create a new
1006 # to be valid base since our ancestors had to create a new
1007 # snapshot.
1007 # snapshot.
1008 siblings = [r for r in siblings if floor < r]
1008 siblings = [r for r in siblings if floor < r]
1009 yield tuple(sorted(siblings))
1009 yield tuple(sorted(siblings))
1010 # then test the base from our parent's delta chain.
1010 # then test the base from our parent's delta chain.
1011 yield tuple(sorted(snaps))
1011 yield tuple(sorted(snaps))
1012 floor = min(snaps)
1012 floor = min(snaps)
1013 # No suitable base found in the parent chain, search if any full
1013 # No suitable base found in the parent chain, search if any full
1014 # snapshots emitted since parent's base would be a suitable base for an
1014 # snapshots emitted since parent's base would be a suitable base for an
1015 # intermediate snapshot.
1015 # intermediate snapshot.
1016 #
1016 #
1017 # It give a chance to reuse a delta chain unrelated to the current
1017 # It give a chance to reuse a delta chain unrelated to the current
1018 # revisions instead of starting our own. Without such re-use,
1018 # revisions instead of starting our own. Without such re-use,
1019 # topological branches would keep reopening new full chains. Creating
1019 # topological branches would keep reopening new full chains. Creating
1020 # more and more snapshot as the repository grow.
1020 # more and more snapshot as the repository grow.
1021 full = [r for r in snapshot_cache.snapshots[nullrev] if snapfloor <= r]
1021 full = [r for r in snapshot_cache.snapshots[nullrev] if snapfloor <= r]
1022 yield tuple(sorted(full))
1022 yield tuple(sorted(full))
1023
1023
1024 if not sparse:
1024 if not sparse:
1025 # other approach failed try against prev to hopefully save us a
1025 # other approach failed try against prev to hopefully save us a
1026 # fulltext.
1026 # fulltext.
1027 yield (prev,)
1027 yield (prev,)
1028
1028
1029
1029
1030 class SnapshotCache:
1030 class SnapshotCache:
1031 __slots__ = ('snapshots', '_start_rev', '_end_rev')
1031 __slots__ = ('snapshots', '_start_rev', '_end_rev')
1032
1032
1033 def __init__(self):
1033 def __init__(self):
1034 self.snapshots = collections.defaultdict(set)
1034 self.snapshots = collections.defaultdict(set)
1035 self._start_rev = None
1035 self._start_rev = None
1036 self._end_rev = None
1036 self._end_rev = None
1037
1037
1038 def update(self, revlog, start_rev=0):
1038 def update(self, revlog, start_rev=0):
1039 """find snapshots from start_rev to tip"""
1039 """find snapshots from start_rev to tip"""
1040 nb_revs = len(revlog)
1040 nb_revs = len(revlog)
1041 end_rev = nb_revs - 1
1041 end_rev = nb_revs - 1
1042 if start_rev > end_rev:
1042 if start_rev > end_rev:
1043 return # range is empty
1043 return # range is empty
1044
1044
1045 if self._start_rev is None:
1045 if self._start_rev is None:
1046 assert self._end_rev is None
1046 assert self._end_rev is None
1047 self._update(revlog, start_rev, end_rev)
1047 self._update(revlog, start_rev, end_rev)
1048 elif not (self._start_rev <= start_rev and end_rev <= self._end_rev):
1048 elif not (self._start_rev <= start_rev and end_rev <= self._end_rev):
1049 if start_rev < self._start_rev:
1049 if start_rev < self._start_rev:
1050 self._update(revlog, start_rev, self._start_rev - 1)
1050 self._update(revlog, start_rev, self._start_rev - 1)
1051 if self._end_rev < end_rev:
1051 if self._end_rev < end_rev:
1052 self._update(revlog, self._end_rev + 1, end_rev)
1052 self._update(revlog, self._end_rev + 1, end_rev)
1053
1053
1054 if self._start_rev is None:
1054 if self._start_rev is None:
1055 assert self._end_rev is None
1055 assert self._end_rev is None
1056 self._end_rev = end_rev
1056 self._end_rev = end_rev
1057 self._start_rev = start_rev
1057 self._start_rev = start_rev
1058 else:
1058 else:
1059 self._start_rev = min(self._start_rev, start_rev)
1059 self._start_rev = min(self._start_rev, start_rev)
1060 self._end_rev = max(self._end_rev, end_rev)
1060 self._end_rev = max(self._end_rev, end_rev)
1061 assert self._start_rev <= self._end_rev, (
1061 assert self._start_rev <= self._end_rev, (
1062 self._start_rev,
1062 self._start_rev,
1063 self._end_rev,
1063 self._end_rev,
1064 )
1064 )
1065
1065
1066 def _update(self, revlog, start_rev, end_rev):
1066 def _update(self, revlog, start_rev, end_rev):
1067 """internal method that actually do update content"""
1067 """internal method that actually do update content"""
1068 assert self._start_rev is None or (
1068 assert self._start_rev is None or (
1069 start_rev < self._start_rev or start_rev > self._end_rev
1069 start_rev < self._start_rev or start_rev > self._end_rev
1070 ), (self._start_rev, self._end_rev, start_rev, end_rev)
1070 ), (self._start_rev, self._end_rev, start_rev, end_rev)
1071 assert self._start_rev is None or (
1071 assert self._start_rev is None or (
1072 end_rev < self._start_rev or end_rev > self._end_rev
1072 end_rev < self._start_rev or end_rev > self._end_rev
1073 ), (self._start_rev, self._end_rev, start_rev, end_rev)
1073 ), (self._start_rev, self._end_rev, start_rev, end_rev)
1074 cache = self.snapshots
1074 cache = self.snapshots
1075 if hasattr(revlog.index, 'findsnapshots'):
1075 if hasattr(revlog.index, 'findsnapshots'):
1076 revlog.index.findsnapshots(cache, start_rev, end_rev)
1076 revlog.index.findsnapshots(cache, start_rev, end_rev)
1077 else:
1077 else:
1078 deltaparent = revlog.deltaparent
1078 deltaparent = revlog.deltaparent
1079 issnapshot = revlog.issnapshot
1079 issnapshot = revlog.issnapshot
1080 for rev in revlog.revs(start_rev, end_rev):
1080 for rev in revlog.revs(start_rev, end_rev):
1081 if issnapshot(rev):
1081 if issnapshot(rev):
1082 cache[deltaparent(rev)].add(rev)
1082 cache[deltaparent(rev)].add(rev)
1083
1083
1084
1084
1085 class deltacomputer:
1085 class deltacomputer:
1086 def __init__(
1086 def __init__(
1087 self,
1087 self,
1088 revlog,
1088 revlog,
1089 write_debug=None,
1089 write_debug=None,
1090 debug_search=False,
1090 debug_search=False,
1091 debug_info=None,
1091 debug_info=None,
1092 ):
1092 ):
1093 self.revlog = revlog
1093 self.revlog = revlog
1094 self._write_debug = write_debug
1094 self._write_debug = write_debug
1095 if write_debug is None:
1095 if write_debug is None:
1096 self._debug_search = False
1096 self._debug_search = False
1097 else:
1097 else:
1098 self._debug_search = debug_search
1098 self._debug_search = debug_search
1099 self._debug_info = debug_info
1099 self._debug_info = debug_info
1100 self._snapshot_cache = SnapshotCache()
1100 self._snapshot_cache = SnapshotCache()
1101
1101
1102 @property
1102 @property
1103 def _gather_debug(self):
1103 def _gather_debug(self):
1104 return self._write_debug is not None or self._debug_info is not None
1104 return self._write_debug is not None or self._debug_info is not None
1105
1105
1106 def buildtext(self, revinfo):
1106 def buildtext(self, revinfo):
1107 """Builds a fulltext version of a revision
1107 """Builds a fulltext version of a revision
1108
1108
1109 revinfo: revisioninfo instance that contains all needed info
1109 revinfo: revisioninfo instance that contains all needed info
1110 """
1110 """
1111 btext = revinfo.btext
1111 btext = revinfo.btext
1112 if btext[0] is not None:
1112 if btext[0] is not None:
1113 return btext[0]
1113 return btext[0]
1114
1114
1115 revlog = self.revlog
1115 revlog = self.revlog
1116 cachedelta = revinfo.cachedelta
1116 cachedelta = revinfo.cachedelta
1117 baserev = cachedelta[0]
1117 baserev = cachedelta[0]
1118 delta = cachedelta[1]
1118 delta = cachedelta[1]
1119
1119
1120 fulltext = btext[0] = _textfromdelta(
1120 fulltext = btext[0] = _textfromdelta(
1121 revlog,
1121 revlog,
1122 baserev,
1122 baserev,
1123 delta,
1123 delta,
1124 revinfo.p1,
1124 revinfo.p1,
1125 revinfo.p2,
1125 revinfo.p2,
1126 revinfo.flags,
1126 revinfo.flags,
1127 revinfo.node,
1127 revinfo.node,
1128 )
1128 )
1129 return fulltext
1129 return fulltext
1130
1130
1131 def _builddeltadiff(self, base, revinfo):
1131 def _builddeltadiff(self, base, revinfo):
1132 revlog = self.revlog
1132 revlog = self.revlog
1133 t = self.buildtext(revinfo)
1133 t = self.buildtext(revinfo)
1134 if revlog.iscensored(base):
1134 if revlog.iscensored(base):
1135 # deltas based on a censored revision must replace the
1135 # deltas based on a censored revision must replace the
1136 # full content in one patch, so delta works everywhere
1136 # full content in one patch, so delta works everywhere
1137 header = mdiff.replacediffheader(revlog.rawsize(base), len(t))
1137 header = mdiff.replacediffheader(revlog.rawsize(base), len(t))
1138 delta = header + t
1138 delta = header + t
1139 else:
1139 else:
1140 ptext = revlog.rawdata(base)
1140 ptext = revlog.rawdata(base)
1141 delta = mdiff.textdiff(ptext, t)
1141 delta = mdiff.textdiff(ptext, t)
1142
1142
1143 return delta
1143 return delta
1144
1144
1145 def _builddeltainfo(self, revinfo, base, target_rev=None):
1145 def _builddeltainfo(self, revinfo, base, target_rev=None):
1146 # can we use the cached delta?
1146 # can we use the cached delta?
1147 revlog = self.revlog
1147 revlog = self.revlog
1148 chainbase = revlog.chainbase(base)
1148 chainbase = revlog.chainbase(base)
1149 if revlog.delta_config.general_delta:
1149 if revlog.delta_config.general_delta:
1150 deltabase = base
1150 deltabase = base
1151 else:
1151 else:
1152 if target_rev is not None and base != target_rev - 1:
1152 if target_rev is not None and base != target_rev - 1:
1153 msg = (
1153 msg = (
1154 b'general delta cannot use delta for something else '
1154 b'general delta cannot use delta for something else '
1155 b'than `prev`: %d<-%d'
1155 b'than `prev`: %d<-%d'
1156 )
1156 )
1157 msg %= (base, target_rev)
1157 msg %= (base, target_rev)
1158 raise error.ProgrammingError(msg)
1158 raise error.ProgrammingError(msg)
1159 deltabase = chainbase
1159 deltabase = chainbase
1160 snapshotdepth = None
1160 snapshotdepth = None
1161 if revlog.delta_config.sparse_revlog and deltabase == nullrev:
1161 if revlog.delta_config.sparse_revlog and deltabase == nullrev:
1162 snapshotdepth = 0
1162 snapshotdepth = 0
1163 elif revlog.delta_config.sparse_revlog and revlog.issnapshot(deltabase):
1163 elif revlog.delta_config.sparse_revlog and revlog.issnapshot(deltabase):
1164 # A delta chain should always be one full snapshot,
1164 # A delta chain should always be one full snapshot,
1165 # zero or more semi-snapshots, and zero or more deltas
1165 # zero or more semi-snapshots, and zero or more deltas
1166 p1, p2 = revlog.rev(revinfo.p1), revlog.rev(revinfo.p2)
1166 p1, p2 = revlog.rev(revinfo.p1), revlog.rev(revinfo.p2)
1167 if deltabase not in (p1, p2) and revlog.issnapshot(deltabase):
1167 if deltabase not in (p1, p2) and revlog.issnapshot(deltabase):
1168 snapshotdepth = len(revlog._deltachain(deltabase)[0])
1168 snapshotdepth = len(revlog._deltachain(deltabase)[0])
1169 delta = None
1169 delta = None
1170 if revinfo.cachedelta:
1170 if revinfo.cachedelta:
1171 cachebase = revinfo.cachedelta[0]
1171 cachebase = revinfo.cachedelta[0]
1172 # check if the diff still apply
1172 # check if the diff still apply
1173 currentbase = cachebase
1173 currentbase = cachebase
1174 while (
1174 while (
1175 currentbase != nullrev
1175 currentbase != nullrev
1176 and currentbase != base
1176 and currentbase != base
1177 and self.revlog.length(currentbase) == 0
1177 and self.revlog.length(currentbase) == 0
1178 ):
1178 ):
1179 currentbase = self.revlog.deltaparent(currentbase)
1179 currentbase = self.revlog.deltaparent(currentbase)
1180 if self.revlog._lazydelta and currentbase == base:
1180 if self.revlog.delta_config.lazy_delta and currentbase == base:
1181 delta = revinfo.cachedelta[1]
1181 delta = revinfo.cachedelta[1]
1182 if delta is None:
1182 if delta is None:
1183 delta = self._builddeltadiff(base, revinfo)
1183 delta = self._builddeltadiff(base, revinfo)
1184 if self._debug_search:
1184 if self._debug_search:
1185 msg = b"DBG-DELTAS-SEARCH: uncompressed-delta-size=%d\n"
1185 msg = b"DBG-DELTAS-SEARCH: uncompressed-delta-size=%d\n"
1186 msg %= len(delta)
1186 msg %= len(delta)
1187 self._write_debug(msg)
1187 self._write_debug(msg)
1188 # snapshotdept need to be neither None nor 0 level snapshot
1188 # snapshotdept need to be neither None nor 0 level snapshot
1189 if revlog.upperboundcomp is not None and snapshotdepth:
1189 if revlog.upperboundcomp is not None and snapshotdepth:
1190 lowestrealisticdeltalen = len(delta) // revlog.upperboundcomp
1190 lowestrealisticdeltalen = len(delta) // revlog.upperboundcomp
1191 snapshotlimit = revinfo.textlen >> snapshotdepth
1191 snapshotlimit = revinfo.textlen >> snapshotdepth
1192 if self._debug_search:
1192 if self._debug_search:
1193 msg = b"DBG-DELTAS-SEARCH: projected-lower-size=%d\n"
1193 msg = b"DBG-DELTAS-SEARCH: projected-lower-size=%d\n"
1194 msg %= lowestrealisticdeltalen
1194 msg %= lowestrealisticdeltalen
1195 self._write_debug(msg)
1195 self._write_debug(msg)
1196 if snapshotlimit < lowestrealisticdeltalen:
1196 if snapshotlimit < lowestrealisticdeltalen:
1197 if self._debug_search:
1197 if self._debug_search:
1198 msg = b"DBG-DELTAS-SEARCH: DISCARDED (snapshot limit)\n"
1198 msg = b"DBG-DELTAS-SEARCH: DISCARDED (snapshot limit)\n"
1199 self._write_debug(msg)
1199 self._write_debug(msg)
1200 return None
1200 return None
1201 if revlog.length(base) < lowestrealisticdeltalen:
1201 if revlog.length(base) < lowestrealisticdeltalen:
1202 if self._debug_search:
1202 if self._debug_search:
1203 msg = b"DBG-DELTAS-SEARCH: DISCARDED (prev size)\n"
1203 msg = b"DBG-DELTAS-SEARCH: DISCARDED (prev size)\n"
1204 self._write_debug(msg)
1204 self._write_debug(msg)
1205 return None
1205 return None
1206 header, data = revlog.compress(delta)
1206 header, data = revlog.compress(delta)
1207 deltalen = len(header) + len(data)
1207 deltalen = len(header) + len(data)
1208 offset = revlog.end(len(revlog) - 1)
1208 offset = revlog.end(len(revlog) - 1)
1209 dist = deltalen + offset - revlog.start(chainbase)
1209 dist = deltalen + offset - revlog.start(chainbase)
1210 chainlen, compresseddeltalen = revlog._chaininfo(base)
1210 chainlen, compresseddeltalen = revlog._chaininfo(base)
1211 chainlen += 1
1211 chainlen += 1
1212 compresseddeltalen += deltalen
1212 compresseddeltalen += deltalen
1213
1213
1214 return _deltainfo(
1214 return _deltainfo(
1215 dist,
1215 dist,
1216 deltalen,
1216 deltalen,
1217 (header, data),
1217 (header, data),
1218 deltabase,
1218 deltabase,
1219 chainbase,
1219 chainbase,
1220 chainlen,
1220 chainlen,
1221 compresseddeltalen,
1221 compresseddeltalen,
1222 snapshotdepth,
1222 snapshotdepth,
1223 )
1223 )
1224
1224
1225 def _fullsnapshotinfo(self, revinfo, curr):
1225 def _fullsnapshotinfo(self, revinfo, curr):
1226 rawtext = self.buildtext(revinfo)
1226 rawtext = self.buildtext(revinfo)
1227 data = self.revlog.compress(rawtext)
1227 data = self.revlog.compress(rawtext)
1228 compresseddeltalen = deltalen = dist = len(data[1]) + len(data[0])
1228 compresseddeltalen = deltalen = dist = len(data[1]) + len(data[0])
1229 deltabase = chainbase = curr
1229 deltabase = chainbase = curr
1230 snapshotdepth = 0
1230 snapshotdepth = 0
1231 chainlen = 1
1231 chainlen = 1
1232
1232
1233 return _deltainfo(
1233 return _deltainfo(
1234 dist,
1234 dist,
1235 deltalen,
1235 deltalen,
1236 data,
1236 data,
1237 deltabase,
1237 deltabase,
1238 chainbase,
1238 chainbase,
1239 chainlen,
1239 chainlen,
1240 compresseddeltalen,
1240 compresseddeltalen,
1241 snapshotdepth,
1241 snapshotdepth,
1242 )
1242 )
1243
1243
1244 def finddeltainfo(self, revinfo, excluded_bases=None, target_rev=None):
1244 def finddeltainfo(self, revinfo, excluded_bases=None, target_rev=None):
1245 """Find an acceptable delta against a candidate revision
1245 """Find an acceptable delta against a candidate revision
1246
1246
1247 revinfo: information about the revision (instance of _revisioninfo)
1247 revinfo: information about the revision (instance of _revisioninfo)
1248
1248
1249 Returns the first acceptable candidate revision, as ordered by
1249 Returns the first acceptable candidate revision, as ordered by
1250 _candidategroups
1250 _candidategroups
1251
1251
1252 If no suitable deltabase is found, we return delta info for a full
1252 If no suitable deltabase is found, we return delta info for a full
1253 snapshot.
1253 snapshot.
1254
1254
1255 `excluded_bases` is an optional set of revision that cannot be used as
1255 `excluded_bases` is an optional set of revision that cannot be used as
1256 a delta base. Use this to recompute delta suitable in censor or strip
1256 a delta base. Use this to recompute delta suitable in censor or strip
1257 context.
1257 context.
1258 """
1258 """
1259 if target_rev is None:
1259 if target_rev is None:
1260 target_rev = len(self.revlog)
1260 target_rev = len(self.revlog)
1261
1261
1262 gather_debug = self._gather_debug
1262 gather_debug = self._gather_debug
1263 cachedelta = revinfo.cachedelta
1263 cachedelta = revinfo.cachedelta
1264 revlog = self.revlog
1264 revlog = self.revlog
1265 p1r = p2r = None
1265 p1r = p2r = None
1266
1266
1267 if excluded_bases is None:
1267 if excluded_bases is None:
1268 excluded_bases = set()
1268 excluded_bases = set()
1269
1269
1270 if gather_debug:
1270 if gather_debug:
1271 start = util.timer()
1271 start = util.timer()
1272 dbg = self._one_dbg_data()
1272 dbg = self._one_dbg_data()
1273 dbg['revision'] = target_rev
1273 dbg['revision'] = target_rev
1274 target_revlog = b"UNKNOWN"
1274 target_revlog = b"UNKNOWN"
1275 target_type = self.revlog.target[0]
1275 target_type = self.revlog.target[0]
1276 target_key = self.revlog.target[1]
1276 target_key = self.revlog.target[1]
1277 if target_type == KIND_CHANGELOG:
1277 if target_type == KIND_CHANGELOG:
1278 target_revlog = b'CHANGELOG:'
1278 target_revlog = b'CHANGELOG:'
1279 elif target_type == KIND_MANIFESTLOG:
1279 elif target_type == KIND_MANIFESTLOG:
1280 target_revlog = b'MANIFESTLOG:'
1280 target_revlog = b'MANIFESTLOG:'
1281 if target_key:
1281 if target_key:
1282 target_revlog += b'%s:' % target_key
1282 target_revlog += b'%s:' % target_key
1283 elif target_type == KIND_FILELOG:
1283 elif target_type == KIND_FILELOG:
1284 target_revlog = b'FILELOG:'
1284 target_revlog = b'FILELOG:'
1285 if target_key:
1285 if target_key:
1286 target_revlog += b'%s:' % target_key
1286 target_revlog += b'%s:' % target_key
1287 dbg['target-revlog'] = target_revlog
1287 dbg['target-revlog'] = target_revlog
1288 p1r = revlog.rev(revinfo.p1)
1288 p1r = revlog.rev(revinfo.p1)
1289 p2r = revlog.rev(revinfo.p2)
1289 p2r = revlog.rev(revinfo.p2)
1290 if p1r != nullrev:
1290 if p1r != nullrev:
1291 p1_chain_len = revlog._chaininfo(p1r)[0]
1291 p1_chain_len = revlog._chaininfo(p1r)[0]
1292 else:
1292 else:
1293 p1_chain_len = -1
1293 p1_chain_len = -1
1294 if p2r != nullrev:
1294 if p2r != nullrev:
1295 p2_chain_len = revlog._chaininfo(p2r)[0]
1295 p2_chain_len = revlog._chaininfo(p2r)[0]
1296 else:
1296 else:
1297 p2_chain_len = -1
1297 p2_chain_len = -1
1298 dbg['p1-chain-len'] = p1_chain_len
1298 dbg['p1-chain-len'] = p1_chain_len
1299 dbg['p2-chain-len'] = p2_chain_len
1299 dbg['p2-chain-len'] = p2_chain_len
1300
1300
1301 # 1) if the revision is empty, no amount of delta can beat it
1301 # 1) if the revision is empty, no amount of delta can beat it
1302 #
1302 #
1303 # 2) no delta for flag processor revision (see "candelta" for why)
1303 # 2) no delta for flag processor revision (see "candelta" for why)
1304 # not calling candelta since only one revision needs test, also to
1304 # not calling candelta since only one revision needs test, also to
1305 # avoid overhead fetching flags again.
1305 # avoid overhead fetching flags again.
1306 if not revinfo.textlen or revinfo.flags & REVIDX_RAWTEXT_CHANGING_FLAGS:
1306 if not revinfo.textlen or revinfo.flags & REVIDX_RAWTEXT_CHANGING_FLAGS:
1307 deltainfo = self._fullsnapshotinfo(revinfo, target_rev)
1307 deltainfo = self._fullsnapshotinfo(revinfo, target_rev)
1308 if gather_debug:
1308 if gather_debug:
1309 end = util.timer()
1309 end = util.timer()
1310 dbg['duration'] = end - start
1310 dbg['duration'] = end - start
1311 dbg[
1311 dbg[
1312 'delta-base'
1312 'delta-base'
1313 ] = deltainfo.base # pytype: disable=attribute-error
1313 ] = deltainfo.base # pytype: disable=attribute-error
1314 dbg['search_round_count'] = 0
1314 dbg['search_round_count'] = 0
1315 dbg['using-cached-base'] = False
1315 dbg['using-cached-base'] = False
1316 dbg['delta_try_count'] = 0
1316 dbg['delta_try_count'] = 0
1317 dbg['type'] = b"full"
1317 dbg['type'] = b"full"
1318 dbg['snapshot-depth'] = 0
1318 dbg['snapshot-depth'] = 0
1319 self._dbg_process_data(dbg)
1319 self._dbg_process_data(dbg)
1320 return deltainfo
1320 return deltainfo
1321
1321
1322 deltainfo = None
1322 deltainfo = None
1323
1323
1324 # If this source delta are to be forcibly reuse, let us comply early.
1324 # If this source delta are to be forcibly reuse, let us comply early.
1325 if (
1325 if (
1326 revlog.delta_config.general_delta
1326 revlog.delta_config.general_delta
1327 and revinfo.cachedelta is not None
1327 and revinfo.cachedelta is not None
1328 and revinfo.cachedelta[2] == DELTA_BASE_REUSE_FORCE
1328 and revinfo.cachedelta[2] == DELTA_BASE_REUSE_FORCE
1329 ):
1329 ):
1330 base = revinfo.cachedelta[0]
1330 base = revinfo.cachedelta[0]
1331 if base == nullrev:
1331 if base == nullrev:
1332 dbg_type = b"full"
1332 dbg_type = b"full"
1333 deltainfo = self._fullsnapshotinfo(revinfo, target_rev)
1333 deltainfo = self._fullsnapshotinfo(revinfo, target_rev)
1334 if gather_debug:
1334 if gather_debug:
1335 snapshotdepth = 0
1335 snapshotdepth = 0
1336 elif base not in excluded_bases:
1336 elif base not in excluded_bases:
1337 delta = revinfo.cachedelta[1]
1337 delta = revinfo.cachedelta[1]
1338 header, data = revlog.compress(delta)
1338 header, data = revlog.compress(delta)
1339 deltalen = len(header) + len(data)
1339 deltalen = len(header) + len(data)
1340 if gather_debug:
1340 if gather_debug:
1341 offset = revlog.end(len(revlog) - 1)
1341 offset = revlog.end(len(revlog) - 1)
1342 chainbase = revlog.chainbase(base)
1342 chainbase = revlog.chainbase(base)
1343 distance = deltalen + offset - revlog.start(chainbase)
1343 distance = deltalen + offset - revlog.start(chainbase)
1344 chainlen, compresseddeltalen = revlog._chaininfo(base)
1344 chainlen, compresseddeltalen = revlog._chaininfo(base)
1345 chainlen += 1
1345 chainlen += 1
1346 compresseddeltalen += deltalen
1346 compresseddeltalen += deltalen
1347 if base == p1r or base == p2r:
1347 if base == p1r or base == p2r:
1348 dbg_type = b"delta"
1348 dbg_type = b"delta"
1349 snapshotdepth = None
1349 snapshotdepth = None
1350 elif not revlog.issnapshot(base):
1350 elif not revlog.issnapshot(base):
1351 snapshotdepth = None
1351 snapshotdepth = None
1352 else:
1352 else:
1353 dbg_type = b"snapshot"
1353 dbg_type = b"snapshot"
1354 snapshotdepth = revlog.snapshotdepth(base) + 1
1354 snapshotdepth = revlog.snapshotdepth(base) + 1
1355 else:
1355 else:
1356 distance = None
1356 distance = None
1357 chainbase = None
1357 chainbase = None
1358 chainlen = None
1358 chainlen = None
1359 compresseddeltalen = None
1359 compresseddeltalen = None
1360 snapshotdepth = None
1360 snapshotdepth = None
1361 deltainfo = _deltainfo(
1361 deltainfo = _deltainfo(
1362 distance=distance,
1362 distance=distance,
1363 deltalen=deltalen,
1363 deltalen=deltalen,
1364 data=(header, data),
1364 data=(header, data),
1365 base=base,
1365 base=base,
1366 chainbase=chainbase,
1366 chainbase=chainbase,
1367 chainlen=chainlen,
1367 chainlen=chainlen,
1368 compresseddeltalen=compresseddeltalen,
1368 compresseddeltalen=compresseddeltalen,
1369 snapshotdepth=snapshotdepth,
1369 snapshotdepth=snapshotdepth,
1370 )
1370 )
1371
1371
1372 if deltainfo is not None:
1372 if deltainfo is not None:
1373 if gather_debug:
1373 if gather_debug:
1374 end = util.timer()
1374 end = util.timer()
1375 dbg['duration'] = end - start
1375 dbg['duration'] = end - start
1376 dbg[
1376 dbg[
1377 'delta-base'
1377 'delta-base'
1378 ] = deltainfo.base # pytype: disable=attribute-error
1378 ] = deltainfo.base # pytype: disable=attribute-error
1379 dbg['search_round_count'] = 0
1379 dbg['search_round_count'] = 0
1380 dbg['using-cached-base'] = True
1380 dbg['using-cached-base'] = True
1381 dbg['delta_try_count'] = 0
1381 dbg['delta_try_count'] = 0
1382 dbg['type'] = b"full"
1382 dbg['type'] = b"full"
1383 if snapshotdepth is None:
1383 if snapshotdepth is None:
1384 dbg['snapshot-depth'] = 0
1384 dbg['snapshot-depth'] = 0
1385 else:
1385 else:
1386 dbg['snapshot-depth'] = snapshotdepth
1386 dbg['snapshot-depth'] = snapshotdepth
1387 self._dbg_process_data(dbg)
1387 self._dbg_process_data(dbg)
1388 return deltainfo
1388 return deltainfo
1389
1389
1390 # count the number of different delta we tried (for debug purpose)
1390 # count the number of different delta we tried (for debug purpose)
1391 dbg_try_count = 0
1391 dbg_try_count = 0
1392 # count the number of "search round" we did. (for debug purpose)
1392 # count the number of "search round" we did. (for debug purpose)
1393 dbg_try_rounds = 0
1393 dbg_try_rounds = 0
1394 dbg_type = b'unknown'
1394 dbg_type = b'unknown'
1395
1395
1396 if p1r is None:
1396 if p1r is None:
1397 p1r = revlog.rev(revinfo.p1)
1397 p1r = revlog.rev(revinfo.p1)
1398 p2r = revlog.rev(revinfo.p2)
1398 p2r = revlog.rev(revinfo.p2)
1399
1399
1400 if self._debug_search:
1400 if self._debug_search:
1401 msg = b"DBG-DELTAS-SEARCH: SEARCH rev=%d\n"
1401 msg = b"DBG-DELTAS-SEARCH: SEARCH rev=%d\n"
1402 msg %= target_rev
1402 msg %= target_rev
1403 self._write_debug(msg)
1403 self._write_debug(msg)
1404
1404
1405 groups = _candidategroups(
1405 groups = _candidategroups(
1406 self.revlog,
1406 self.revlog,
1407 revinfo.textlen,
1407 revinfo.textlen,
1408 p1r,
1408 p1r,
1409 p2r,
1409 p2r,
1410 cachedelta,
1410 cachedelta,
1411 excluded_bases,
1411 excluded_bases,
1412 target_rev,
1412 target_rev,
1413 snapshot_cache=self._snapshot_cache,
1413 snapshot_cache=self._snapshot_cache,
1414 )
1414 )
1415 candidaterevs = next(groups)
1415 candidaterevs = next(groups)
1416 while candidaterevs is not None:
1416 while candidaterevs is not None:
1417 dbg_try_rounds += 1
1417 dbg_try_rounds += 1
1418 if self._debug_search:
1418 if self._debug_search:
1419 prev = None
1419 prev = None
1420 if deltainfo is not None:
1420 if deltainfo is not None:
1421 prev = deltainfo.base
1421 prev = deltainfo.base
1422
1422
1423 if (
1423 if (
1424 cachedelta is not None
1424 cachedelta is not None
1425 and len(candidaterevs) == 1
1425 and len(candidaterevs) == 1
1426 and cachedelta[0] in candidaterevs
1426 and cachedelta[0] in candidaterevs
1427 ):
1427 ):
1428 round_type = b"cached-delta"
1428 round_type = b"cached-delta"
1429 elif p1r in candidaterevs or p2r in candidaterevs:
1429 elif p1r in candidaterevs or p2r in candidaterevs:
1430 round_type = b"parents"
1430 round_type = b"parents"
1431 elif prev is not None and all(c < prev for c in candidaterevs):
1431 elif prev is not None and all(c < prev for c in candidaterevs):
1432 round_type = b"refine-down"
1432 round_type = b"refine-down"
1433 elif prev is not None and all(c > prev for c in candidaterevs):
1433 elif prev is not None and all(c > prev for c in candidaterevs):
1434 round_type = b"refine-up"
1434 round_type = b"refine-up"
1435 else:
1435 else:
1436 round_type = b"search-down"
1436 round_type = b"search-down"
1437 msg = b"DBG-DELTAS-SEARCH: ROUND #%d - %d candidates - %s\n"
1437 msg = b"DBG-DELTAS-SEARCH: ROUND #%d - %d candidates - %s\n"
1438 msg %= (dbg_try_rounds, len(candidaterevs), round_type)
1438 msg %= (dbg_try_rounds, len(candidaterevs), round_type)
1439 self._write_debug(msg)
1439 self._write_debug(msg)
1440 nominateddeltas = []
1440 nominateddeltas = []
1441 if deltainfo is not None:
1441 if deltainfo is not None:
1442 if self._debug_search:
1442 if self._debug_search:
1443 msg = (
1443 msg = (
1444 b"DBG-DELTAS-SEARCH: CONTENDER: rev=%d - length=%d\n"
1444 b"DBG-DELTAS-SEARCH: CONTENDER: rev=%d - length=%d\n"
1445 )
1445 )
1446 msg %= (deltainfo.base, deltainfo.deltalen)
1446 msg %= (deltainfo.base, deltainfo.deltalen)
1447 self._write_debug(msg)
1447 self._write_debug(msg)
1448 # if we already found a good delta,
1448 # if we already found a good delta,
1449 # challenge it against refined candidates
1449 # challenge it against refined candidates
1450 nominateddeltas.append(deltainfo)
1450 nominateddeltas.append(deltainfo)
1451 for candidaterev in candidaterevs:
1451 for candidaterev in candidaterevs:
1452 if self._debug_search:
1452 if self._debug_search:
1453 msg = b"DBG-DELTAS-SEARCH: CANDIDATE: rev=%d\n"
1453 msg = b"DBG-DELTAS-SEARCH: CANDIDATE: rev=%d\n"
1454 msg %= candidaterev
1454 msg %= candidaterev
1455 self._write_debug(msg)
1455 self._write_debug(msg)
1456 candidate_type = None
1456 candidate_type = None
1457 if candidaterev == p1r:
1457 if candidaterev == p1r:
1458 candidate_type = b"p1"
1458 candidate_type = b"p1"
1459 elif candidaterev == p2r:
1459 elif candidaterev == p2r:
1460 candidate_type = b"p2"
1460 candidate_type = b"p2"
1461 elif self.revlog.issnapshot(candidaterev):
1461 elif self.revlog.issnapshot(candidaterev):
1462 candidate_type = b"snapshot-%d"
1462 candidate_type = b"snapshot-%d"
1463 candidate_type %= self.revlog.snapshotdepth(
1463 candidate_type %= self.revlog.snapshotdepth(
1464 candidaterev
1464 candidaterev
1465 )
1465 )
1466
1466
1467 if candidate_type is not None:
1467 if candidate_type is not None:
1468 msg = b"DBG-DELTAS-SEARCH: type=%s\n"
1468 msg = b"DBG-DELTAS-SEARCH: type=%s\n"
1469 msg %= candidate_type
1469 msg %= candidate_type
1470 self._write_debug(msg)
1470 self._write_debug(msg)
1471 msg = b"DBG-DELTAS-SEARCH: size=%d\n"
1471 msg = b"DBG-DELTAS-SEARCH: size=%d\n"
1472 msg %= self.revlog.length(candidaterev)
1472 msg %= self.revlog.length(candidaterev)
1473 self._write_debug(msg)
1473 self._write_debug(msg)
1474 msg = b"DBG-DELTAS-SEARCH: base=%d\n"
1474 msg = b"DBG-DELTAS-SEARCH: base=%d\n"
1475 msg %= self.revlog.deltaparent(candidaterev)
1475 msg %= self.revlog.deltaparent(candidaterev)
1476 self._write_debug(msg)
1476 self._write_debug(msg)
1477
1477
1478 dbg_try_count += 1
1478 dbg_try_count += 1
1479
1479
1480 if self._debug_search:
1480 if self._debug_search:
1481 delta_start = util.timer()
1481 delta_start = util.timer()
1482 candidatedelta = self._builddeltainfo(
1482 candidatedelta = self._builddeltainfo(
1483 revinfo,
1483 revinfo,
1484 candidaterev,
1484 candidaterev,
1485 target_rev=target_rev,
1485 target_rev=target_rev,
1486 )
1486 )
1487 if self._debug_search:
1487 if self._debug_search:
1488 delta_end = util.timer()
1488 delta_end = util.timer()
1489 msg = b"DBG-DELTAS-SEARCH: delta-search-time=%f\n"
1489 msg = b"DBG-DELTAS-SEARCH: delta-search-time=%f\n"
1490 msg %= delta_end - delta_start
1490 msg %= delta_end - delta_start
1491 self._write_debug(msg)
1491 self._write_debug(msg)
1492 if candidatedelta is not None:
1492 if candidatedelta is not None:
1493 if is_good_delta_info(self.revlog, candidatedelta, revinfo):
1493 if is_good_delta_info(self.revlog, candidatedelta, revinfo):
1494 if self._debug_search:
1494 if self._debug_search:
1495 msg = b"DBG-DELTAS-SEARCH: DELTA: length=%d (GOOD)\n"
1495 msg = b"DBG-DELTAS-SEARCH: DELTA: length=%d (GOOD)\n"
1496 msg %= candidatedelta.deltalen
1496 msg %= candidatedelta.deltalen
1497 self._write_debug(msg)
1497 self._write_debug(msg)
1498 nominateddeltas.append(candidatedelta)
1498 nominateddeltas.append(candidatedelta)
1499 elif self._debug_search:
1499 elif self._debug_search:
1500 msg = b"DBG-DELTAS-SEARCH: DELTA: length=%d (BAD)\n"
1500 msg = b"DBG-DELTAS-SEARCH: DELTA: length=%d (BAD)\n"
1501 msg %= candidatedelta.deltalen
1501 msg %= candidatedelta.deltalen
1502 self._write_debug(msg)
1502 self._write_debug(msg)
1503 elif self._debug_search:
1503 elif self._debug_search:
1504 msg = b"DBG-DELTAS-SEARCH: NO-DELTA\n"
1504 msg = b"DBG-DELTAS-SEARCH: NO-DELTA\n"
1505 self._write_debug(msg)
1505 self._write_debug(msg)
1506 if nominateddeltas:
1506 if nominateddeltas:
1507 deltainfo = min(nominateddeltas, key=lambda x: x.deltalen)
1507 deltainfo = min(nominateddeltas, key=lambda x: x.deltalen)
1508 if deltainfo is not None:
1508 if deltainfo is not None:
1509 candidaterevs = groups.send(deltainfo.base)
1509 candidaterevs = groups.send(deltainfo.base)
1510 else:
1510 else:
1511 candidaterevs = next(groups)
1511 candidaterevs = next(groups)
1512
1512
1513 if deltainfo is None:
1513 if deltainfo is None:
1514 dbg_type = b"full"
1514 dbg_type = b"full"
1515 deltainfo = self._fullsnapshotinfo(revinfo, target_rev)
1515 deltainfo = self._fullsnapshotinfo(revinfo, target_rev)
1516 elif deltainfo.snapshotdepth: # pytype: disable=attribute-error
1516 elif deltainfo.snapshotdepth: # pytype: disable=attribute-error
1517 dbg_type = b"snapshot"
1517 dbg_type = b"snapshot"
1518 else:
1518 else:
1519 dbg_type = b"delta"
1519 dbg_type = b"delta"
1520
1520
1521 if gather_debug:
1521 if gather_debug:
1522 end = util.timer()
1522 end = util.timer()
1523 if dbg_type == b'full':
1523 if dbg_type == b'full':
1524 used_cached = (
1524 used_cached = (
1525 cachedelta is not None
1525 cachedelta is not None
1526 and dbg_try_rounds == 0
1526 and dbg_try_rounds == 0
1527 and dbg_try_count == 0
1527 and dbg_try_count == 0
1528 and cachedelta[0] == nullrev
1528 and cachedelta[0] == nullrev
1529 )
1529 )
1530 else:
1530 else:
1531 used_cached = (
1531 used_cached = (
1532 cachedelta is not None
1532 cachedelta is not None
1533 and dbg_try_rounds == 1
1533 and dbg_try_rounds == 1
1534 and dbg_try_count == 1
1534 and dbg_try_count == 1
1535 and deltainfo.base == cachedelta[0]
1535 and deltainfo.base == cachedelta[0]
1536 )
1536 )
1537 dbg['duration'] = end - start
1537 dbg['duration'] = end - start
1538 dbg[
1538 dbg[
1539 'delta-base'
1539 'delta-base'
1540 ] = deltainfo.base # pytype: disable=attribute-error
1540 ] = deltainfo.base # pytype: disable=attribute-error
1541 dbg['search_round_count'] = dbg_try_rounds
1541 dbg['search_round_count'] = dbg_try_rounds
1542 dbg['using-cached-base'] = used_cached
1542 dbg['using-cached-base'] = used_cached
1543 dbg['delta_try_count'] = dbg_try_count
1543 dbg['delta_try_count'] = dbg_try_count
1544 dbg['type'] = dbg_type
1544 dbg['type'] = dbg_type
1545 if (
1545 if (
1546 deltainfo.snapshotdepth # pytype: disable=attribute-error
1546 deltainfo.snapshotdepth # pytype: disable=attribute-error
1547 is not None
1547 is not None
1548 ):
1548 ):
1549 dbg[
1549 dbg[
1550 'snapshot-depth'
1550 'snapshot-depth'
1551 ] = deltainfo.snapshotdepth # pytype: disable=attribute-error
1551 ] = deltainfo.snapshotdepth # pytype: disable=attribute-error
1552 else:
1552 else:
1553 dbg['snapshot-depth'] = 0
1553 dbg['snapshot-depth'] = 0
1554 self._dbg_process_data(dbg)
1554 self._dbg_process_data(dbg)
1555 return deltainfo
1555 return deltainfo
1556
1556
1557 def _one_dbg_data(self):
1557 def _one_dbg_data(self):
1558 return {
1558 return {
1559 'duration': None,
1559 'duration': None,
1560 'revision': None,
1560 'revision': None,
1561 'delta-base': None,
1561 'delta-base': None,
1562 'search_round_count': None,
1562 'search_round_count': None,
1563 'using-cached-base': None,
1563 'using-cached-base': None,
1564 'delta_try_count': None,
1564 'delta_try_count': None,
1565 'type': None,
1565 'type': None,
1566 'p1-chain-len': None,
1566 'p1-chain-len': None,
1567 'p2-chain-len': None,
1567 'p2-chain-len': None,
1568 'snapshot-depth': None,
1568 'snapshot-depth': None,
1569 'target-revlog': None,
1569 'target-revlog': None,
1570 }
1570 }
1571
1571
1572 def _dbg_process_data(self, dbg):
1572 def _dbg_process_data(self, dbg):
1573 if self._debug_info is not None:
1573 if self._debug_info is not None:
1574 self._debug_info.append(dbg)
1574 self._debug_info.append(dbg)
1575
1575
1576 if self._write_debug is not None:
1576 if self._write_debug is not None:
1577 msg = (
1577 msg = (
1578 b"DBG-DELTAS:"
1578 b"DBG-DELTAS:"
1579 b" %-12s"
1579 b" %-12s"
1580 b" rev=%d:"
1580 b" rev=%d:"
1581 b" delta-base=%d"
1581 b" delta-base=%d"
1582 b" is-cached=%d"
1582 b" is-cached=%d"
1583 b" - search-rounds=%d"
1583 b" - search-rounds=%d"
1584 b" try-count=%d"
1584 b" try-count=%d"
1585 b" - delta-type=%-6s"
1585 b" - delta-type=%-6s"
1586 b" snap-depth=%d"
1586 b" snap-depth=%d"
1587 b" - p1-chain-length=%d"
1587 b" - p1-chain-length=%d"
1588 b" p2-chain-length=%d"
1588 b" p2-chain-length=%d"
1589 b" - duration=%f"
1589 b" - duration=%f"
1590 b"\n"
1590 b"\n"
1591 )
1591 )
1592 msg %= (
1592 msg %= (
1593 dbg["target-revlog"],
1593 dbg["target-revlog"],
1594 dbg["revision"],
1594 dbg["revision"],
1595 dbg["delta-base"],
1595 dbg["delta-base"],
1596 dbg["using-cached-base"],
1596 dbg["using-cached-base"],
1597 dbg["search_round_count"],
1597 dbg["search_round_count"],
1598 dbg["delta_try_count"],
1598 dbg["delta_try_count"],
1599 dbg["type"],
1599 dbg["type"],
1600 dbg["snapshot-depth"],
1600 dbg["snapshot-depth"],
1601 dbg["p1-chain-len"],
1601 dbg["p1-chain-len"],
1602 dbg["p2-chain-len"],
1602 dbg["p2-chain-len"],
1603 dbg["duration"],
1603 dbg["duration"],
1604 )
1604 )
1605 self._write_debug(msg)
1605 self._write_debug(msg)
1606
1606
1607
1607
1608 def delta_compression(default_compression_header, deltainfo):
1608 def delta_compression(default_compression_header, deltainfo):
1609 """return (COMPRESSION_MODE, deltainfo)
1609 """return (COMPRESSION_MODE, deltainfo)
1610
1610
1611 used by revlog v2+ format to dispatch between PLAIN and DEFAULT
1611 used by revlog v2+ format to dispatch between PLAIN and DEFAULT
1612 compression.
1612 compression.
1613 """
1613 """
1614 h, d = deltainfo.data
1614 h, d = deltainfo.data
1615 compression_mode = COMP_MODE_INLINE
1615 compression_mode = COMP_MODE_INLINE
1616 if not h and not d:
1616 if not h and not d:
1617 # not data to store at all... declare them uncompressed
1617 # not data to store at all... declare them uncompressed
1618 compression_mode = COMP_MODE_PLAIN
1618 compression_mode = COMP_MODE_PLAIN
1619 elif not h:
1619 elif not h:
1620 t = d[0:1]
1620 t = d[0:1]
1621 if t == b'\0':
1621 if t == b'\0':
1622 compression_mode = COMP_MODE_PLAIN
1622 compression_mode = COMP_MODE_PLAIN
1623 elif t == default_compression_header:
1623 elif t == default_compression_header:
1624 compression_mode = COMP_MODE_DEFAULT
1624 compression_mode = COMP_MODE_DEFAULT
1625 elif h == b'u':
1625 elif h == b'u':
1626 # we have a more efficient way to declare uncompressed
1626 # we have a more efficient way to declare uncompressed
1627 h = b''
1627 h = b''
1628 compression_mode = COMP_MODE_PLAIN
1628 compression_mode = COMP_MODE_PLAIN
1629 deltainfo = drop_u_compression(deltainfo)
1629 deltainfo = drop_u_compression(deltainfo)
1630 return compression_mode, deltainfo
1630 return compression_mode, deltainfo
General Comments 0
You need to be logged in to leave comments. Login now