Patchwork [1,of,5] revsetlang: avoid string concatenation in formatspec()

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 7, 2018, 6:31 a.m.
Message ID <48cdc456784aa0e7a39f.1515306711@mimosa>
Download mbox | patch
Permalink /patch/26594/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 7, 2018, 6:31 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1491033011 -32400
#      Sat Apr 01 16:50:11 2017 +0900
# Node ID 48cdc456784aa0e7a39f89f63f1d5142b20637d7
# Parent  9aa28a9a49871922fd48f1ff6811573a191d47f9
revsetlang: avoid string concatenation in formatspec()
Gregory Szorc - Jan. 8, 2018, 3:48 a.m.
On Sat, Jan 6, 2018 at 10:31 PM, Yuya Nishihara <yuya@tcha.org> wrote:

> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1491033011 -32400
> #      Sat Apr 01 16:50:11 2017 +0900
> # Node ID 48cdc456784aa0e7a39f89f63f1d5142b20637d7
> # Parent  9aa28a9a49871922fd48f1ff6811573a191d47f9
> revsetlang: avoid string concatenation in formatspec()
>

Queued this series. Thanks.


>
> diff --git a/mercurial/revsetlang.py b/mercurial/revsetlang.py
> --- a/mercurial/revsetlang.py
> +++ b/mercurial/revsetlang.py
> @@ -620,7 +620,7 @@ def formatspec(expr, *args):
>          return '(%s or %s)' % (listexp(s[:m], t), listexp(s[m:], t))
>
>      expr = pycompat.bytestr(expr)
> -    ret = ''
> +    ret = []
>      pos = 0
>      arg = 0
>      while pos < len(expr):
> @@ -629,24 +629,24 @@ def formatspec(expr, *args):
>              pos += 1
>              d = expr[pos]
>              if d == '%':
> -                ret += d
> +                ret.append(d)
>              elif d in 'dsnbr':
> -                ret += argtype(d, args[arg])
> +                ret.append(argtype(d, args[arg]))
>                  arg += 1
>              elif d == 'l':
>                  # a list of some type
>                  pos += 1
>                  d = expr[pos]
> -                ret += listexp(list(args[arg]), d)
> +                ret.append(listexp(list(args[arg]), d))
>                  arg += 1
>              else:
>                  raise error.Abort(_('unexpected revspec format character
> %s')
>                                    % d)
>          else:
> -            ret += c
> +            ret.append(c)
>          pos += 1
>
> -    return ret
> +    return ''.join(ret)
>
>  def prettyformat(tree):
>      return parser.prettyformat(tree, ('string', 'symbol'))
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>

Patch

diff --git a/mercurial/revsetlang.py b/mercurial/revsetlang.py
--- a/mercurial/revsetlang.py
+++ b/mercurial/revsetlang.py
@@ -620,7 +620,7 @@  def formatspec(expr, *args):
         return '(%s or %s)' % (listexp(s[:m], t), listexp(s[m:], t))
 
     expr = pycompat.bytestr(expr)
-    ret = ''
+    ret = []
     pos = 0
     arg = 0
     while pos < len(expr):
@@ -629,24 +629,24 @@  def formatspec(expr, *args):
             pos += 1
             d = expr[pos]
             if d == '%':
-                ret += d
+                ret.append(d)
             elif d in 'dsnbr':
-                ret += argtype(d, args[arg])
+                ret.append(argtype(d, args[arg]))
                 arg += 1
             elif d == 'l':
                 # a list of some type
                 pos += 1
                 d = expr[pos]
-                ret += listexp(list(args[arg]), d)
+                ret.append(listexp(list(args[arg]), d))
                 arg += 1
             else:
                 raise error.Abort(_('unexpected revspec format character %s')
                                   % d)
         else:
-            ret += c
+            ret.append(c)
         pos += 1
 
-    return ret
+    return ''.join(ret)
 
 def prettyformat(tree):
     return parser.prettyformat(tree, ('string', 'symbol'))