changeset 3953:2174de498a69 stable

stablerange: build closure a bit less inefficiently The new way make me a bit less sad than the old one.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 16 Aug 2018 20:22:19 +0200
parents a7794f5abacd
children b53bf9942e0a
files hgext3rd/evolve/stablerangecache.py
diffstat 1 files changed, 20 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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)