stablerange: build closure a bit less inefficiently stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 16 Aug 2018 20:22:19 +0200
branchstable
changeset 3953 2174de498a69
parent 3952 a7794f5abacd
child 3954 b53bf9942e0a
stablerange: build closure a bit less inefficiently The new way make me a bit less sad than the old one.
hgext3rd/evolve/stablerangecache.py
--- a/hgext3rd/evolve/stablerangecache.py	Thu Aug 16 22:19:19 2018 +0200
+++ b/hgext3rd/evolve/stablerangecache.py	Thu Aug 16 20:22:19 2018 +0200
@@ -9,6 +9,7 @@
 
 import abc
 import heapq
+import random
 import sqlite3
 import time
 import weakref
@@ -119,10 +120,17 @@
                      FROM subranges
                      WHERE (suprev = ? AND supidx = ?)
                      ORDER BY listidx;"""
-_querysuperranges = """SELECT suprev, supidx
-                       FROM subranges
-                       WHERE (subrev = ? AND subidx = ?)
-                       ORDER BY listidx;"""
+
+_querysuperrangesmain = """SELECT DISTINCT suprev, supidx
+                           FROM subranges
+                           WHERE %s;"""
+
+_querysuperrangesbody = '(subrev = %d and subidx = %d)'
+
+def _make_querysuperranges(ranges):
+    # building a tree of OR would allow for more ranges
+    body = ' OR '.join(_querysuperrangesbody % r for r in ranges)
+    return _querysuperrangesmain % body
 
 class stablerangesqlbase(stablerange.stablerangecached):
     """class that can handle all the bits needed to store range into sql
@@ -151,10 +159,14 @@
             new.add((r, 0))
         con = self._con
         while new and con is not None:
-            # many execute is not efficient
-            next = new.pop()
-            known.add(next)
-            ranges = set(con.execute(_querysuperranges, next).fetchall())
+            new
+            if len(new) < 300:
+                sample = new
+            else:
+                sample = random.sample(new, 300)
+            known.update(sample)
+            query = _make_querysuperranges(sample)
+            ranges = set(con.execute(query).fetchall())
             new.update(ranges)
             new -= known
         return sorted(known)