changeset 32820:653d60455dbe

smartset: micro optimize baseset.slice() to use slice of list -1ms isn't significant, but seems not bad. revset #1: limit(0::9999, 100, 9000) 5) 0.001681 6) 0.000790
author Yuya Nishihara <yuya@tcha.org>
date Thu, 09 Apr 2015 23:56:06 +0900
parents 4710cc4dac99
children 9b7d615108d7
files mercurial/smartset.py tests/test-revset.t
diffstat 2 files changed, 59 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/smartset.py	Tue Mar 24 00:14:53 2015 +0900
+++ b/mercurial/smartset.py	Thu Apr 09 23:56:06 2015 +0900
@@ -372,6 +372,18 @@
     def __sub__(self, other):
         return self._fastsetop(other, '__sub__')
 
+    def _slice(self, start, stop):
+        # creating new list should be generally cheaper than iterating items
+        if self._ascending is None:
+            return baseset(self._list[start:stop], istopo=self._istopo)
+
+        data = self._asclist
+        if not self._ascending:
+            start, stop = max(len(data) - stop, 0), max(len(data) - start, 0)
+        s = baseset(data[start:stop], istopo=self._istopo)
+        s._ascending = self._ascending
+        return s
+
     def __repr__(self):
         d = {None: '', False: '-', True: '+'}[self._ascending]
         s = _formatsetrepr(self._datarepr)
--- a/tests/test-revset.t	Tue Mar 24 00:14:53 2015 +0900
+++ b/tests/test-revset.t	Thu Apr 09 23:56:06 2015 +0900
@@ -1037,6 +1037,49 @@
   6
   7
 
+ (using baseset)
+
+  $ hg debugrevspec --no-show-revs -s 0+1+2+3+4+5+6+7
+  * set:
+  <baseset [0, 1, 2, 3, 4, 5, 6, 7]>
+  $ hg debugrevspec --no-show-revs -s 0::7
+  * set:
+  <baseset+ [0, 1, 2, 3, 4, 5, 6, 7]>
+  $ log 'limit(0+1+2+3+4+5+6+7, 3, 4)'
+  4
+  5
+  6
+  $ log 'limit(sort(0::7, rev), 3, 4)'
+  4
+  5
+  6
+  $ log 'limit(sort(0::7, -rev), 3, 4)'
+  3
+  2
+  1
+  $ log 'last(sort(0::7, rev), 2)'
+  6
+  7
+  $ hg debugrevspec -s 'limit(sort(0::7, rev), 3, 6)'
+  * set:
+  <baseset+ [6, 7]>
+  6
+  7
+  $ hg debugrevspec -s 'limit(sort(0::7, rev), 3, 9)'
+  * set:
+  <baseset+ []>
+  $ hg debugrevspec -s 'limit(sort(0::7, -rev), 3, 6)'
+  * set:
+  <baseset- [0, 1]>
+  1
+  0
+  $ hg debugrevspec -s 'limit(sort(0::7, -rev), 3, 9)'
+  * set:
+  <baseset- []>
+  $ hg debugrevspec -s 'limit(0::7, 0)'
+  * set:
+  <baseset+ []>
+
 Test order of first/last revisions
 
   $ hg debugrevspec -s 'first(4:0, 3) & 3:'
@@ -1967,8 +2010,7 @@
     define)
   * set:
   <filteredset
-    <baseset slice=0:1
-      <baseset [1, 0, 2]>>,
+    <baseset [1]>,
     <spanset- 0:3>>
   1
 
@@ -2003,8 +2045,7 @@
   <filteredset
     <spanset- 0:3>,
     <not
-      <baseset slice=0:1
-        <baseset [1, 2, 0]>>>>
+      <baseset [1]>>>
   2
   0
 
@@ -3628,8 +3669,7 @@
       ('symbol', '2')))
   * set:
   <filteredset
-    <baseset slice=0:2
-      <baseset [1, 2, 3]>>,
+    <baseset [1, 2]>,
     <not
       <baseset [2]>>>
   1
@@ -3683,8 +3723,7 @@
       ('symbol', '2')))
   * set:
   <filteredset
-    <baseset slice=0:1
-      <baseset [2, 1]>>,
+    <baseset [2]>,
     <not
       <baseset [2]>>>