156
192
revision_format_num = None
194
# The search regex used by xml based repositories to determine what things
195
# where changed in a single commit.
196
_file_ids_altered_regex = lazy_regex.lazy_compile(
197
r'file_id="(?P<file_id>[^"]+)"'
198
r'.* revision="(?P<revision_id>[^"]+)"'
158
201
def _check_revisions(self, inv):
159
202
"""Extension point for subclasses to check during serialisation.
161
204
:param inv: An inventory about to be serialised, to be checked.
162
:raises: AssertionError if an error has occured.
205
:raises: AssertionError if an error has occurred.
164
207
if inv.revision_id is None:
165
raise AssertionError()
208
raise AssertionError("inv.revision_id is None")
166
209
if inv.root.revision is None:
167
raise AssertionError()
210
raise AssertionError("inv.root.revision is None")
212
def _check_cache_size(self, inv_size, entry_cache):
213
"""Check that the entry_cache is large enough.
215
We want the cache to be ~2x the size of an inventory. The reason is
216
because we use a FIFO cache, and how Inventory records are likely to
217
change. In general, you have a small number of records which change
218
often, and a lot of records which do not change at all. So when the
219
cache gets full, you actually flush out a lot of the records you are
220
interested in, which means you need to recreate all of those records.
221
An LRU Cache would be better, but the overhead negates the cache
224
One way to look at it, only the size of the cache > len(inv) is your
225
'working' set. And in general, it shouldn't be a problem to hold 2
226
inventories in memory anyway.
228
:param inv_size: The number of entries in an inventory.
230
if entry_cache is None:
232
# 1.5 times might also be reasonable.
233
recommended_min_cache_size = inv_size * 1.5
234
if entry_cache.cache_size() < recommended_min_cache_size:
235
recommended_cache_size = inv_size * 2
236
trace.mutter('Resizing the inventory entry cache from %d to %d',
237
entry_cache.cache_size(), recommended_cache_size)
238
entry_cache.resize(recommended_cache_size)
169
240
def write_inventory_to_lines(self, inv):
170
241
"""Return a list of lines with the encoded inventory."""
350
422
revision_id = cache_utf8.encode(revision_id)
351
423
inv = inventory.Inventory(root_id=None, revision_id=revision_id)
353
ie = self._unpack_entry(e)
425
ie = self._unpack_entry(e, entry_cache=entry_cache,
426
return_from_cache=return_from_cache)
428
self._check_cache_size(len(inv), entry_cache)
357
def _unpack_entry(self, elt):
431
def _unpack_entry(self, elt, entry_cache=None, return_from_cache=False):
433
file_id = elt_get('file_id')
434
revision = elt_get('revision')
435
# Check and see if we have already unpacked this exact entry
436
# Some timings for "repo.revision_trees(last_100_revs)"
438
# unmodified 4.1s 40.8s
440
# using fifo 2.83s 29.1s
444
# no_copy 2.00s 20.5s
445
# no_c,dict 1.95s 18.0s
446
# Note that a cache of 10k nodes is more than sufficient to hold all of
447
# the inventory for the last 100 revs for bzr, but not for mysql (20k
448
# is enough for mysql, which saves the same 2s as using a dict)
450
# Breakdown of mysql using time.clock()
451
# 4.1s 2 calls to element.get for file_id, revision_id
452
# 4.5s cache_hit lookup
453
# 7.1s InventoryFile.copy()
454
# 2.4s InventoryDirectory.copy()
455
# 0.4s decoding unique entries
456
# 1.6s decoding entries after FIFO fills up
457
# 0.8s Adding nodes to FIFO (including flushes)
458
# 0.1s cache miss lookups
460
# 4.1s 2 calls to element.get for file_id, revision_id
461
# 9.9s cache_hit lookup
462
# 10.8s InventoryEntry.copy()
463
# 0.3s cache miss lookus
464
# 1.2s decoding entries
465
# 1.0s adding nodes to LRU
466
if entry_cache is not None and revision is not None:
467
key = (file_id, revision)
469
# We copy it, because some operations may mutate it
470
cached_ie = entry_cache[key]
474
# Only copying directory entries drops us 2.85s => 2.35s
475
if return_from_cache:
476
if cached_ie.kind == 'directory':
477
return cached_ie.copy()
479
return cached_ie.copy()
359
482
if not InventoryEntry.versionable_kind(kind):
360
483
raise AssertionError('unsupported entry kind %s' % kind)
362
485
get_cached = _get_utf8_or_ascii
364
parent_id = elt.get('parent_id')
487
file_id = get_cached(file_id)
488
if revision is not None:
489
revision = get_cached(revision)
490
parent_id = elt_get('parent_id')
365
491
if parent_id is not None:
366
492
parent_id = get_cached(parent_id)
367
file_id = get_cached(elt.get('file_id'))
369
494
if kind == 'directory':
370
495
ie = inventory.InventoryDirectory(file_id,
373
498
elif kind == 'file':
374
499
ie = inventory.InventoryFile(file_id,
377
ie.text_sha1 = elt.get('text_sha1')
378
if elt.get('executable') == 'yes':
502
ie.text_sha1 = elt_get('text_sha1')
503
if elt_get('executable') == 'yes':
379
504
ie.executable = True
380
v = elt.get('text_size')
505
v = elt_get('text_size')
381
506
ie.text_size = v and int(v)
382
507
elif kind == 'symlink':
383
508
ie = inventory.InventoryLink(file_id,
386
ie.symlink_target = elt.get('symlink_target')
511
ie.symlink_target = elt_get('symlink_target')
388
513
raise errors.UnsupportedInventoryKind(kind)
389
revision = elt.get('revision')
390
if revision is not None:
391
revision = get_cached(revision)
392
514
ie.revision = revision
515
if revision is not None and entry_cache is not None:
516
# We cache a copy() because callers like to mutate objects, and
517
# that would cause the item in cache to mutate as well.
518
# This has a small effect on many-inventory performance, because
519
# the majority fraction is spent in cache hits, not misses.
520
entry_cache[key] = ie.copy()
441
569
raise AssertionError("repeated property %r" % name)
442
570
rev.properties[name] = value
572
def _find_text_key_references(self, line_iterator):
573
"""Core routine for extracting references to texts from inventories.
575
This performs the translation of xml lines to revision ids.
577
:param line_iterator: An iterator of lines, origin_version_id
578
:return: A dictionary mapping text keys ((fileid, revision_id) tuples)
579
to whether they were referred to by the inventory of the
580
revision_id that they contain. Note that if that revision_id was
581
not part of the line_iterator's output then False will be given -
582
even though it may actually refer to that key.
584
if not self.support_altered_by_hack:
585
raise AssertionError(
586
"_find_text_key_references only "
587
"supported for branches which store inventory as unnested xml"
588
", not on %r" % self)
591
# this code needs to read every new line in every inventory for the
592
# inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
593
# not present in one of those inventories is unnecessary but not
594
# harmful because we are filtering by the revision id marker in the
595
# inventory lines : we only select file ids altered in one of those
596
# revisions. We don't need to see all lines in the inventory because
597
# only those added in an inventory in rev X can contain a revision=X
599
unescape_revid_cache = {}
600
unescape_fileid_cache = {}
602
# jam 20061218 In a big fetch, this handles hundreds of thousands
603
# of lines, so it has had a lot of inlining and optimizing done.
604
# Sorry that it is a little bit messy.
605
# Move several functions to be local variables, since this is a long
607
search = self._file_ids_altered_regex.search
608
unescape = _unescape_xml
609
setdefault = result.setdefault
610
for line, line_key in line_iterator:
614
# One call to match.group() returning multiple items is quite a
615
# bit faster than 2 calls to match.group() each returning 1
616
file_id, revision_id = match.group('file_id', 'revision_id')
618
# Inlining the cache lookups helps a lot when you make 170,000
619
# lines and 350k ids, versus 8.4 unique ids.
620
# Using a cache helps in 2 ways:
621
# 1) Avoids unnecessary decoding calls
622
# 2) Re-uses cached strings, which helps in future set and
624
# (2) is enough that removing encoding entirely along with
625
# the cache (so we are using plain strings) results in no
626
# performance improvement.
628
revision_id = unescape_revid_cache[revision_id]
630
unescaped = unescape(revision_id)
631
unescape_revid_cache[revision_id] = unescaped
632
revision_id = unescaped
634
# Note that unconditionally unescaping means that we deserialise
635
# every fileid, which for general 'pull' is not great, but we don't
636
# really want to have some many fulltexts that this matters anyway.
639
file_id = unescape_fileid_cache[file_id]
641
unescaped = unescape(file_id)
642
unescape_fileid_cache[file_id] = unescaped
645
key = (file_id, revision_id)
646
setdefault(key, False)
647
if revision_id == line_key[-1]:
445
652
serializer_v8 = Serializer_v8()