From 79893fdc23ed4d0f970a676c306cba8f09e639fa Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Sun, 15 Jan 2017 20:47:33 +0100 Subject: [PATCH] Performance tuning objects to be downloaded - rely on dict quasi-random order instead of an additional shuffle - request an object once per minute - stop check after count objects have been found --- src/inventory.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/inventory.py b/src/inventory.py index e2a3eb01..b4fcce6d 100644 --- a/src/inventory.py +++ b/src/inventory.py @@ -106,23 +106,15 @@ class Missing(object): raise ValueError("Must be at least one") with self.lock: now = time.time() - since = now - 300 # once every 5 minutes - try: - matchingHashes = {k:v for k, v in self.hashes.iteritems() if current_thread().peer in self.hashes[k]['peers'] and self.hashes[k]['requested'] < since} - if count > len(matchingHashes): - count = len(matchingHashes) - objectHashes = random.sample(matchingHashes, count) - except (IndexError, KeyError): # list is empty - return None - for objectHash in objectHashes: - try: + since = now - 60 # once every minute + objectHashes = [] + for objectHash in self.hashes.keys(): + if current_thread().peer in self.hashes[objectHash]['peers'] and self.hashes[objectHash]['requested'] < since: + objectHashes.append(objectHash) self.hashes[objectHash]['peers'].remove(current_thread().peer) - except ValueError: - pass - if len(self.hashes[objectHash]['peers']) == 0: - self.delete(objectHash) - else: self.hashes[objectHash]['requested'] = now + if len(objectHashes) >= count: + break return objectHashes def delete(self, objectHash):