comparison mercurial/context.py @ 29223:c04ad3d3c651

annotate: optimize line counting We used len(text.splitlines()) to count lines. This allocates, copies, and deallocates an object for every line in a file. Instead, we use count("\n") to count newlines and adjust based on whether there's a trailing newline. This improves the speed of annotating localrepo.py from 4.2 to 4.0 seconds.
author Matt Mackall <mpm@selenic.com>
date Wed, 18 May 2016 16:37:32 -0500
parents 92d37fb3f1aa
children 576ff900fcc7
comparison
equal deleted inserted replaced
29222:ed4bd789fc55 29223:c04ad3d3c651
928 in the managed file. 928 in the managed file.
929 To reduce annotation cost, 929 To reduce annotation cost,
930 this returns fixed value(False is used) as linenumber, 930 this returns fixed value(False is used) as linenumber,
931 if "linenumber" parameter is "False".''' 931 if "linenumber" parameter is "False".'''
932 932
933 def lines(text):
934 if text.endswith("\n"):
935 return text.count("\n")
936 return text.count("\n") + 1
937
933 if linenumber is None: 938 if linenumber is None:
934 def decorate(text, rev): 939 def decorate(text, rev):
935 return ([rev] * len(text.splitlines()), text) 940 return ([rev] * lines(text), text)
936 elif linenumber: 941 elif linenumber:
937 def decorate(text, rev): 942 def decorate(text, rev):
938 size = len(text.splitlines()) 943 return ([(rev, i) for i in xrange(1, lines(text) + 1)], text)
939 return ([(rev, i) for i in xrange(1, size + 1)], text)
940 else: 944 else:
941 def decorate(text, rev): 945 def decorate(text, rev):
942 return ([(rev, False)] * len(text.splitlines()), text) 946 return ([(rev, False)] * lines(text), text)
943 947
944 def pair(parent, child): 948 def pair(parent, child):
945 blocks = mdiff.allblocks(parent[1], child[1], opts=diffopts, 949 blocks = mdiff.allblocks(parent[1], child[1], opts=diffopts,
946 refine=True) 950 refine=True)
947 for (a1, a2, b1, b2), t in blocks: 951 for (a1, a2, b1, b2), t in blocks: