Patchwork [5,of,5] revset: rely on built in iterator when possible in _generatorset.__iter__

login
register
mail settings
Submitter Pierre-Yves David
Date Sept. 19, 2014, 10:33 p.m.
Message ID <fbf00761fdc4d6d161b7.1411166038@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/5889/
State Accepted
Commit 44dce874de97a2b3d47cc8be97432cbfcffddbe1
Headers show

Comments

Pierre-Yves David - Sept. 19, 2014, 10:33 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1398902183 25200
#      Wed Apr 30 16:56:23 2014 -0700
# Node ID fbf00761fdc4d6d161b7d22d2cfe5ae01e7feab3
# Parent  bfaab5271eb80fd282784864193e0d57a1ceef89
revset: rely on built in iterator when possible in _generatorset.__iter__

Doing manual iteration is expensible. We rely on built in list iteration
whenever possible. The other case has to become a closure we cannot have a both
yield and return in the same function.

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2643,30 +2643,30 @@  class _generatorset(object):
         self._cache[x] = False
         return False
 
     def __iter__(self):
         if self._finished:
-            for x in self._genlist:
-                yield x
-            return
+            return iter(self._genlist)
 
         # we have to use this complexe iteration strategy to allow multiple
         # iteration at the same time. We need to be able to catch revision
         # removed from `consumegen` and added to genlist in another instance.
         #
         # Getting ride of it would provide an about 15% speed up on this
         # iteration.
-        i = 0
         genlist = self._genlist
         nextrev = self._consumegen().next
         _len = len # cache global lookup
-        while True:
-            if i < _len(genlist):
-                yield genlist[i]
-            else:
-                yield nextrev()
-            i += 1
+        def gen():
+            i = 0
+            while True:
+                if i < _len(genlist):
+                    yield genlist[i]
+                else:
+                    yield nextrev()
+                i += 1
+        return gen()
 
     def _consumegen(self):
         cache = self._cache
         genlist = self._genlist.append
         for item in self._gen: