mercurial/lsprof.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Wed, 16 Oct 2019 17:49:30 +0200
changeset 43254 181d28ba05da
parent 43108 dc6d866b8cb8
child 43503 313e3a279828
permissions -rw-r--r--
copies: avoid instancing more changectx to access parent revisions We just need to know the revision numbers of the parents, creating full context is needlessly expensive. This provide a small, but noticeable performance boost. revision: large amount; added files: large amount; rename small amount; c3b14617fbd7 9ba6ab77fd29 before: ! wall 2.885636 comb 2.900000 user 2.870000 sys 0.030000 (median of 10) after: ! wall 2.702270 comb 2.710000 user 2.690000 sys 0.020000 (median of 10) revision: large amount; added files: small amount; rename small amount; c3b14617fbd7 f650a9b140d2 before: ! wall 4.298271 comb 4.290000 user 4.240000 sys 0.050000 (median of 10) after: ! wall 3.976610 comb 3.970000 user 3.920000 sys 0.050000 (median of 10) revision: large amount; added files: large amount; rename large amount; 08ea3258278e d9fa043f30c0 before: ! wall 0.773397 comb 0.770000 user 0.770000 sys 0.000000 (median of 11) after: ! wall 0.701634 comb 0.700000 user 0.700000 sys 0.000000 (median of 13) revision: small amount; added files: large amount; rename large amount; df6f7a526b60 a83dc6a2d56f before: ! wall 0.013585 comb 0.010000 user 0.010000 sys 0.000000 (median of 217) after: ! wall 0.013550 comb 0.010000 user 0.010000 sys 0.000000 (median of 218) revision: small amount; added files: large amount; rename small amount; 4aa4e1f8e19a 169138063d63 before: ! wall 0.003202 comb 0.000000 user 0.000000 sys 0.000000 (median of 929) after: ! wall 0.002993 comb 0.010000 user 0.010000 sys 0.000000 (median of 992) revision: small amount; added files: small amount; rename small amount; 4bc173b045a6 964879152e2e before: ! wall 0.000077 comb 0.000000 user 0.000000 sys 0.000000 (median of 12060) after: ! wall 0.000072 comb 0.000000 user 0.000000 sys 0.000000 (median of 12804) revision: medium amount; added files: large amount; rename medium amount; c95f1ced15f2 2c68e87c3efe before: ! wall 0.510614 comb 0.500000 user 0.500000 sys 0.000000 (median of 18) after: ! wall 0.473681 comb 0.470000 user 0.470000 sys 0.000000 (median of 20) revision: medium amount; added files: medium amount; rename small amount; d343da0c55a8 d7746d32bf9d before: ! wall 0.126552 comb 0.130000 user 0.130000 sys 0.000000 (median of 77) after: ! wall 0.115240 comb 0.110000 user 0.110000 sys 0.000000 (median of 85) Differential Revision: https://phab.mercurial-scm.org/D7122
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27617
b1a59b80e1a3 lsprof: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27061
diff changeset
     1
from __future__ import absolute_import, print_function
27061
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
     2
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
     3
import _lsprof
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     4
import sys
27061
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
     5
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
     6
from .pycompat import getattr
43106
d783f945a701 py3: finish porting iteritems() to pycompat and remove source transformer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43089
diff changeset
     7
from . import pycompat
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
     8
27061
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
     9
Profiler = _lsprof.Profiler
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
    10
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
    11
# PyPy doesn't expose profiler_entry from the module.
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
    12
profiler_entry = getattr(_lsprof, 'profiler_entry', None)
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    13
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    14
__all__ = [b'profile', b'Stats']
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    15
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    16
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    17
def profile(f, *args, **kwds):
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    18
    """XXX docstring"""
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    19
    p = Profiler()
5992
30c40ba10963 updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 2497
diff changeset
    20
    p.enable(subcalls=True, builtins=True)
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    21
    try:
5992
30c40ba10963 updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 2497
diff changeset
    22
        f(*args, **kwds)
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    23
    finally:
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    24
        p.disable()
5992
30c40ba10963 updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 2497
diff changeset
    25
    return Stats(p.getstats())
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    26
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    27
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    28
class Stats(object):
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    29
    """XXX docstring"""
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    30
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    31
    def __init__(self, data):
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    32
        self.data = data
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    33
35836
d4e5b2653693 lsprof: use native string when peeking in __dict__
Augie Fackler <augie@google.com>
parents: 27617
diff changeset
    34
    def sort(self, crit=r"inlinetime"):
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    35
        """XXX docstring"""
27061
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
    36
        # profiler_entries isn't defined when running under PyPy.
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
    37
        if profiler_entry:
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
    38
            if crit not in profiler_entry.__dict__:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    39
                raise ValueError(b"Can't sort by %s" % crit)
27061
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
    40
        elif self.data and not getattr(self.data[0], crit, None):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    41
            raise ValueError(b"Can't sort by %s" % crit)
27061
9c75daf89450 lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 18642
diff changeset
    42
9032
1fa80c5428b8 compat: use 'key' argument instead of 'cmp' when sorting a list
Alejandro Santos <alejolp@alejolp.com>
parents: 7875
diff changeset
    43
        self.data.sort(key=lambda x: getattr(x, crit), reverse=True)
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    44
        for e in self.data:
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    45
            if e.calls:
9032
1fa80c5428b8 compat: use 'key' argument instead of 'cmp' when sorting a list
Alejandro Santos <alejolp@alejolp.com>
parents: 7875
diff changeset
    46
                e.calls.sort(key=lambda x: getattr(x, crit), reverse=True)
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    47
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    48
    def pprint(self, top=None, file=None, limit=None, climit=None):
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    49
        """XXX docstring"""
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    50
        if file is None:
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    51
            file = sys.stdout
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    52
        d = self.data
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    53
        if top is not None:
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    54
            d = d[:top]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    55
        cols = b"% 12d %12d %11.4f %11.4f   %s\n"
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    56
        hcols = b"% 12s %12s %12s %12s %s\n"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    57
        file.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    58
            hcols
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    59
            % (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    60
                b"CallCount",
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    61
                b"Recursive",
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    62
                b"Total(s)",
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    63
                b"Inline(s)",
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    64
                b"module:lineno(function)",
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    65
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    66
        )
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    67
        count = 0
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    68
        for e in d:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    69
            file.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    70
                cols
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    71
                % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    72
                    e.callcount,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    73
                    e.reccallcount,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    74
                    e.totaltime,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    75
                    e.inlinetime,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    76
                    label(e.code),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    77
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    78
            )
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    79
            count += 1
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    80
            if limit is not None and count == limit:
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    81
                return
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    82
            ccount = 0
16263
be92ddc636e3 profile: add undocumented config options for profiler output
Matt Mackall <mpm@selenic.com>
parents: 14959
diff changeset
    83
            if climit and e.calls:
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    84
                for se in e.calls:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    85
                    file.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    86
                        cols
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    87
                        % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    88
                            se.callcount,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    89
                            se.reccallcount,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    90
                            se.totaltime,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    91
                            se.inlinetime,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    92
                            b"    %s" % label(se.code),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    93
                        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
    94
                    )
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    95
                    count += 1
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    96
                    ccount += 1
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    97
                    if limit is not None and count == limit:
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    98
                        return
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    99
                    if climit is not None and ccount == climit:
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   100
                        break
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   101
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   102
    def freeze(self):
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   103
        """Replace all references to code objects with string
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   104
        descriptions; this makes it possible to pickle the instance."""
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   105
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   106
        # this code is probably rather ickier than it needs to be!
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   107
        for i in range(len(self.data)):
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   108
            e = self.data[i]
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   109
            if not isinstance(e.code, str):
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   110
                self.data[i] = type(e)((label(e.code),) + e[1:])
5992
30c40ba10963 updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 2497
diff changeset
   111
            if e.calls:
30c40ba10963 updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 2497
diff changeset
   112
                for j in range(len(e.calls)):
30c40ba10963 updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 2497
diff changeset
   113
                    se = e.calls[j]
30c40ba10963 updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 2497
diff changeset
   114
                    if not isinstance(se.code, str):
30c40ba10963 updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 2497
diff changeset
   115
                        e.calls[j] = type(se)((label(se.code),) + se[1:])
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   116
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
   117
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   118
_fn2mod = {}
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   119
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40202
diff changeset
   120
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   121
def label(code):
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   122
    if isinstance(code, str):
40202
56ea22fa55f0 py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40193
diff changeset
   123
        if sys.version_info.major >= 3:
56ea22fa55f0 py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40193
diff changeset
   124
            code = code.encode('latin-1')
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   125
        return code
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   126
    try:
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   127
        mname = _fn2mod[code.co_filename]
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   128
    except KeyError:
43106
d783f945a701 py3: finish porting iteritems() to pycompat and remove source transformer
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43089
diff changeset
   129
        for k, v in list(pycompat.iteritems(sys.modules)):
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   130
            if v is None:
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   131
                continue
14959
b1dcc5ab86cd lsprof: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 12842
diff changeset
   132
            if not isinstance(getattr(v, '__file__', None), str):
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   133
                continue
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   134
            if v.__file__.startswith(code.co_filename):
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   135
                mname = _fn2mod[code.co_filename] = k
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   136
                break
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   137
        else:
40202
56ea22fa55f0 py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40193
diff changeset
   138
            mname = _fn2mod[code.co_filename] = r'<%s>' % code.co_filename
56ea22fa55f0 py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40193
diff changeset
   139
56ea22fa55f0 py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40193
diff changeset
   140
    res = r'%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name)
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   141
40202
56ea22fa55f0 py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40193
diff changeset
   142
    if sys.version_info.major >= 3:
56ea22fa55f0 py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40193
diff changeset
   143
        res = res.encode('latin-1')
2422
6aa75e77cafe add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   144
40202
56ea22fa55f0 py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40193
diff changeset
   145
    return res