Download optimisations

- don't make empty download requests
- use smaller chunks when they can be spread across multiple
connections
This commit is contained in:
Peter Šurda 2018-01-01 12:51:35 +01:00
parent 1864762a0a
commit 36cc5b9cf5
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

View File

@ -43,7 +43,7 @@ class DownloadThread(threading.Thread, StoppableThread):
connections = BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values() connections = BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values()
random.shuffle(connections) random.shuffle(connections)
try: try:
requestChunk = max(int(DownloadThread.maxRequestChunk / len(connections)), 1) requestChunk = max(int(min(DownloadThread.maxRequestChunk, len(missingObjects)) / len(connections)), 1)
except ZeroDivisionError: except ZeroDivisionError:
requestChunk = 1 requestChunk = 1
for i in connections: for i in connections:
@ -63,15 +63,14 @@ class DownloadThread(threading.Thread, StoppableThread):
except KeyError: except KeyError:
continue continue
random.shuffle(request) random.shuffle(request)
if len(request) > requestChunk - downloadPending:
request = request[:max(1, requestChunk - downloadPending)]
if not request: if not request:
continue continue
if len(request) > requestChunk - downloadPending:
request = request[:requestChunk - downloadPending]
# mark them as pending # mark them as pending
for k in request: for k in request:
i.objectsNewToMe[k] = False i.objectsNewToMe[k] = False
missingObjects[k] = now missingObjects[k] = now
payload = bytearray() payload = bytearray()
payload.extend(addresses.encodeVarint(len(request))) payload.extend(addresses.encodeVarint(len(request)))
for chunk in request: for chunk in request: