From 283b34c439ce2444a330e082e151d79d423857bf Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 22 Mar 2011 02:27:54 +0000 Subject: [PATCH] improve torrent file ordering optimization --- src/file_storage.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/file_storage.cpp b/src/file_storage.cpp index 87a585d86..02a877cf9 100644 --- a/src/file_storage.cpp +++ b/src/file_storage.cpp @@ -465,20 +465,26 @@ namespace libtorrent if (pad_file_limit >= 0 && pad_file_limit < alignment) pad_file_limit = alignment; - // put the largest file at the front, to make sure - // it's aligned - std::vector::iterator i = std::max_element(m_files.begin(), m_files.end() - , &compare_file_entry_size); - - int index = file_index(*i); - reorder_file(index, 0); - size_type off = 0; int padding_file = 0; for (std::vector::iterator i = m_files.begin(); i != m_files.end(); ++i) { - if (pad_file_limit >= 0 + if ((off & (alignment-1)) == 0) + { + // this file position is aligned, pick the largest + // available file to put here + std::vector::iterator best_match + = std::max_element(i, m_files.end() + , &compare_file_entry_size); + + if (best_match != i) + { + int index = file_index(*best_match); + reorder_file(index, file_index(*i)); + } + } + else if (pad_file_limit >= 0 && (off & (alignment-1)) != 0 && i->size > pad_file_limit && i->pad_file == false)