Patchwork [1,of,2,V2] revset: remove existence check from min() and max()

login
register
mail settings
Submitter Durham Goode
Date Sept. 22, 2015, 2:17 a.m.
Message ID <ab2a19c6961805bc67b0.1442888273@dev2000.prn2.facebook.com>
Download mbox | patch
Permalink /patch/10564/
State Accepted
Headers show

Comments

Durham Goode - Sept. 22, 2015, 2:17 a.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1442802473 25200
#      Sun Sep 20 19:27:53 2015 -0700
# Node ID ab2a19c6961805bc67b0db2d615dc3fdfa949cb8
# Parent  a0eff7ebc2aebb32cf4c8da276104102ff37d786
revset: remove existence check from min() and max()

min() and max() would first do an existence check. Unfortunately existence
checks can be slow in certain situations (like if the smartset is a list, and
quickly iterable in both ascending and descending directions, then doing an
existence check will start from the bottom, even if you want to check the
max()).

The fix is to not do the check, and just handle the error if it happens. In a
large repo, this speeds up:

hg log -r 'max(parents(. + .^) - (. + .^)  & ::master)'

from 3.5s to 0.85s. That revset is contrived and just for testing. In our
real case we used 'bundle()' in place of '. + .^'

Interesting perf numbers for the revset benchmarks:

max(draft() and ::tip) =>  0.027s to 0.0005s
max(author(lmoscovicz)) => 2.48s to 0.57s

min doesn't show any perf changes, but changing it as well will prevent a perf
regression in my next patch.

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -1277,10 +1277,14 @@  def maxrev(repo, subset, x):
     Changeset with highest revision number in set.
     """
     os = getset(repo, fullreposet(repo), x)
-    if os:
+    try:
         m = os.max()
         if m in subset:
             return baseset([m])
+    except ValueError:
+        # os.max() throws a ValueError when the collection is empty.
+        # Same as python's max().
+        pass
     return baseset()
 
 def merge(repo, subset, x):
@@ -1316,10 +1320,14 @@  def minrev(repo, subset, x):
     Changeset with lowest revision number in set.
     """
     os = getset(repo, fullreposet(repo), x)
-    if os:
+    try:
         m = os.min()
         if m in subset:
             return baseset([m])
+    except ValueError:
+        # os.min() throws a ValueError when the collection is empty.
+        # Same as python's min().
+        pass
     return baseset()
 
 def modifies(repo, subset, x):