Mercurial > public > mercurial-scm > hg
comparison mercurial/patch.py @ 16803:107a3270a24a
cleanup: use the deque type where appropriate
There have been quite a few places where we pop elements off the
front of a list. This can turn O(n) algorithms into something more
like O(n**2). Python has provided a deque type that can do this
efficiently since at least 2.4.
As an example of the difference a deque can make, it improves
perfancestors performance on a Linux repo from 0.50 seconds to 0.36.
author | Bryan O'Sullivan <bryano@fb.com> |
---|---|
date | Tue, 15 May 2012 10:46:23 -0700 |
parents | c2d9ef43ff6c |
children | 8abee656e14c |
comparison
equal
deleted
inserted
replaced
16802:7e5d94381cd1 | 16803:107a3270a24a |
---|---|
10 import tempfile, zlib, shutil | 10 import tempfile, zlib, shutil |
11 | 11 |
12 from i18n import _ | 12 from i18n import _ |
13 from node import hex, nullid, short | 13 from node import hex, nullid, short |
14 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error | 14 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error |
15 import context | 15 import collections, context |
16 | 16 |
17 gitre = re.compile('diff --git a/(.*) b/(.*)') | 17 gitre = re.compile('diff --git a/(.*) b/(.*)') |
18 | 18 |
19 class PatchError(Exception): | 19 class PatchError(Exception): |
20 pass | 20 pass |
1586 if not node1 and not node2: | 1586 if not node1 and not node2: |
1587 node1 = repo.dirstate.p1() | 1587 node1 = repo.dirstate.p1() |
1588 | 1588 |
1589 def lrugetfilectx(): | 1589 def lrugetfilectx(): |
1590 cache = {} | 1590 cache = {} |
1591 order = [] | 1591 order = collections.deque() |
1592 def getfilectx(f, ctx): | 1592 def getfilectx(f, ctx): |
1593 fctx = ctx.filectx(f, filelog=cache.get(f)) | 1593 fctx = ctx.filectx(f, filelog=cache.get(f)) |
1594 if f not in cache: | 1594 if f not in cache: |
1595 if len(cache) > 20: | 1595 if len(cache) > 20: |
1596 del cache[order.pop(0)] | 1596 del cache[order.popleft()] |
1597 cache[f] = fctx.filelog() | 1597 cache[f] = fctx.filelog() |
1598 else: | 1598 else: |
1599 order.remove(f) | 1599 order.remove(f) |
1600 order.append(f) | 1600 order.append(f) |
1601 return fctx | 1601 return fctx |