summaryrefslogtreecommitdiffstats
path: root/deluge/core/torrent.py
diff options
context:
space:
mode:
authorbendikro <bendikro@gmail.com>2012-11-26 00:23:00 +0100
committerChase Sterling <chase.sterling@gmail.com>2013-01-02 20:20:25 -0500
commit6313ff19b322a2801c633324bb0d77dc4f59b9a1 (patch)
tree2ec614c8947d8be7e244d3045d7915945657a4f5 /deluge/core/torrent.py
parent61bd8aa15405d5582d67e01d3e8cdde634056344 (diff)
downloaddeluge-6313ff19b322a2801c633324bb0d77dc4f59b9a1.tar.gz
deluge-6313ff19b322a2801c633324bb0d77dc4f59b9a1.tar.bz2
deluge-6313ff19b322a2801c633324bb0d77dc4f59b9a1.zip
Improved the speed of set_prioritize_first_last in torrent.py
set_prioritize_first_last is rewritten for better performance, and is now only called when necessary. It should now properly set the priority for the beginning and end of each file in the torrent.
Diffstat (limited to 'deluge/core/torrent.py')
-rw-r--r--deluge/core/torrent.py68
1 files changed, 46 insertions, 22 deletions
diff --git a/deluge/core/torrent.py b/deluge/core/torrent.py
index ba6ec285a..c5fd81955 100644
--- a/deluge/core/torrent.py
+++ b/deluge/core/torrent.py
@@ -232,8 +232,13 @@ class Torrent(object):
"max_upload_speed": self.set_max_upload_speed,
"prioritize_first_last_pieces": self.set_prioritize_first_last,
"sequential_download": self.set_sequential_download
-
}
+
+ # set_prioritize_first_last is called by set_file_priorities,
+ # so remove if file_priorities is set in options.
+ if "file_priorities" in options:
+ del OPTIONS_FUNCS["prioritize_first_last_pieces"]
+
for (key, value) in options.items():
if OPTIONS_FUNCS.has_key(key):
OPTIONS_FUNCS[key](value)
@@ -296,26 +301,44 @@ class Torrent(object):
def set_prioritize_first_last(self, prioritize):
self.options["prioritize_first_last_pieces"] = prioritize
- if self.handle.has_metadata():
- if self.options["compact_allocation"]:
- log.debug("Setting first/last priority with compact "
- "allocation does not work!")
- return
-
- paths = {}
- ti = self.handle.get_torrent_info()
- for n in range(ti.num_pieces()):
- slices = ti.map_block(n, 0, ti.piece_size(n))
- for slice in slices:
- if self.handle.file_priority(slice.file_index):
- paths.setdefault(slice.file_index, []).append(n)
-
- priorities = self.handle.piece_priorities()
- for pieces in paths.itervalues():
- two_percent = int(0.02*len(pieces)) or 1
- for piece in pieces[:two_percent]+pieces[-two_percent:]:
- priorities[piece] = 7 if prioritize else 1
- self.handle.prioritize_pieces(priorities)
+ if not prioritize:
+ # If we are turning off this option, call set_file_priorities to
+ # reset all the piece priorities
+ self.set_file_priorities(self.options["file_priorities"])
+ return
+ if not self.handle.has_metadata():
+ return
+ if self.options["compact_allocation"]:
+ log.debug("Setting first/last priority with compact "
+ "allocation does not work!")
+ return
+ # A list of priorities for each piece in the torrent
+ priorities = self.handle.piece_priorities()
+ prioritized_pieces = []
+ ti = self.handle.get_torrent_info()
+ for i in range(ti.num_files()):
+ f = ti.file_at(i)
+ two_percent_bytes = int(0.02 * f.size)
+ # Get the pieces for the byte offsets
+ first_start = ti.map_file(i, 0, 0).piece
+ first_end = ti.map_file(i, two_percent_bytes, 0).piece
+ last_start = ti.map_file(i, f.size - two_percent_bytes, 0).piece
+ last_end = ti.map_file(i, max(f.size - 1, 0), 0).piece
+
+ first_end += 1
+ last_end += 1
+ prioritized_pieces.append((first_start, first_end))
+ prioritized_pieces.append((last_start, last_end))
+
+ # Creating two lists with priorites for the first/last pieces
+ # of this file, and insert the priorities into the list
+ first_list = [7] * (first_end - first_start)
+ last_list = [7] * (last_end - last_start)
+ priorities[first_start:first_end] = first_list
+ priorities[last_start:last_end] = last_list
+ # Setting the priorites for all the pieces of this torrent
+ self.handle.prioritize_pieces(priorities)
+ return prioritized_pieces, priorities
def set_sequential_download(self, set_sequencial):
self.options["sequential_download"] = set_sequencial
@@ -372,7 +395,8 @@ class Torrent(object):
log.warning("File priorities were not set for this torrent")
# Set the first/last priorities if needed
- self.set_prioritize_first_last(self.options["prioritize_first_last_pieces"])
+ if self.options["prioritize_first_last_pieces"]:
+ self.set_prioritize_first_last(self.options["prioritize_first_last_pieces"])
def set_trackers(self, trackers):
"""Sets trackers"""