tests/printrevset.py
author Martin von Zweigbergk <martinvonz@google.com>
Wed, 26 Jan 2022 10:11:01 -0800
changeset 48692 f1ed5c304f45
parent 45568 c1d0f83d62c4
child 48966 6000f5b25c9b
permissions -rw-r--r--
encoding: fix trim() to be O(n) instead of O(n^2) `encoding.trim()` iterated over the possible lengths smaller than the input and created a slice for each. It then calculated the column width of the result, which is of course O(n), so the overall algorithm was O(n). This patch rewrites it to iterate over the unicode characters, keeping track of the length so far. Also, the old algorithm started from the end of the string, which made it much worse when the input is large and the limit is small (such as the typical 72 we pass to it). You can time it by running something like this: ``` time python3 -c 'from mercurial.utils import stringutil; print(stringutil.ellipsis(b"0123456789" * 1000, 5))' ``` That drops from 4.05 s to 83 ms with this patch (and most of that is of course startup time). Differential Revision: https://phab.mercurial-scm.org/D12089
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39095
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
from __future__ import absolute_import
45568
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
     2
from mercurial.thirdparty import attr
39095
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
from mercurial import (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
     4
    cmdutil,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
     5
    commands,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
     6
    extensions,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
     7
    logcmdutil,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
     8
    revsetlang,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
     9
    smartset,
39095
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
)
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    12
from mercurial.utils import stringutil
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    13
39095
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
45568
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
    15
def logrevset(repo, wopts):
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
    16
    revs = logcmdutil._initialrevs(repo, wopts)
39095
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
    if not revs:
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
        return None
45568
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
    19
    match, pats, slowpath = logcmdutil._makematcher(repo, revs, wopts)
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
    20
    wopts = attr.evolve(wopts, pats=pats)
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
    21
    return logcmdutil._makerevset(repo, wopts, slowpath)
39095
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    23
39095
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
def uisetup(ui):
45568
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
    25
    def printrevset(orig, repo, wopts):
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
    26
        revs, filematcher = orig(repo, wopts)
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
    27
        if wopts.opts.get(b'print_revset'):
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
    28
            expr = logrevset(repo, wopts)
39095
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
            if expr:
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
                tree = revsetlang.parse(expr)
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
                tree = revsetlang.analyze(tree)
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
            else:
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
                tree = []
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
            ui = repo.ui
45568
c1d0f83d62c4 log: introduce struct that carries log traversal options
Yuya Nishihara <yuya@tcha.org>
parents: 45567
diff changeset
    35
            ui.write(b'%s\n' % stringutil.pprint(wopts.opts.get(b'rev', [])))
39095
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
            ui.write(revsetlang.prettyformat(tree) + b'\n')
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
            ui.write(stringutil.prettyrepr(revs) + b'\n')
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
            revs = smartset.baseset()  # display no revisions
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
        return revs, filematcher
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    40
39095
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
    extensions.wrapfunction(logcmdutil, 'getrevs', printrevset)
a271466cb53a tests: extract printrevset extension from test-glog-beautifygraph.t
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
    aliases, entry = cmdutil.findcmd(b'log', commands.table)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    43
    entry[1].append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    44
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    45
            b'',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    46
            b'print-revset',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    47
            False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    48
            b'print generated revset and exit (DEPRECATED)',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    49
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 39095
diff changeset
    50
    )