setdiscovery: extract sample limitation in a `_limitsample` function
We need to reuse this logic for the initial query. We extract it in a function
to unsure sample limiting is applied consistently in all cases.
--- a/mercurial/setdiscovery.py Fri Oct 24 17:24:46 2014 -0500
+++ b/mercurial/setdiscovery.py Mon Oct 27 17:40:32 2014 +0100
@@ -105,14 +105,19 @@
# update from roots
_updatesample(dag.inverse(), nodes, sample, always)
assert sample
- if len(sample) > desiredlen:
- sample = set(random.sample(sample, desiredlen))
- elif len(sample) < desiredlen:
+ sample = _limitsample(sample, desiredlen)
+ if len(sample) < desiredlen:
more = desiredlen - len(sample)
sample.update(random.sample(list(nodes - sample - always), more))
sample.update(always)
return sample
+def _limitsample(sample, desiredlen):
+ """return a random subset of sample of at most desiredlen item"""
+ if len(sample) > desiredlen:
+ sample = set(random.sample(sample, desiredlen))
+ return sample
+
def findcommonheads(ui, local, remote,
initialsamplesize=100,
fullsamplesize=200,