forked from premiere/premiere-libtorrent
optimized file_storage::map_block to use lower_bound instead of linear search
This commit is contained in:
parent
9497fe827d
commit
d0546e702f
|
@ -93,29 +93,39 @@ namespace libtorrent
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
bool compare_file_offset(file_entry const& lhs, file_entry const& rhs)
|
||||||
|
{
|
||||||
|
return lhs.offset + lhs.size < rhs.offset + rhs.size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<file_slice> file_storage::map_block(int piece, size_type offset
|
std::vector<file_slice> file_storage::map_block(int piece, size_type offset
|
||||||
, int size_) const
|
, int size) const
|
||||||
{
|
{
|
||||||
TORRENT_ASSERT(num_files() > 0);
|
TORRENT_ASSERT(num_files() > 0);
|
||||||
std::vector<file_slice> ret;
|
std::vector<file_slice> ret;
|
||||||
|
|
||||||
size_type start = piece * (size_type)m_piece_length + offset;
|
|
||||||
size_type size = size_;
|
|
||||||
TORRENT_ASSERT(start + size <= m_total_size);
|
|
||||||
|
|
||||||
// find the file iterator and file offset
|
// find the file iterator and file offset
|
||||||
// TODO: do a binary search on the file offsets
|
file_entry target;
|
||||||
size_type file_offset = start;
|
target.offset = piece * (size_type)m_piece_length + offset;
|
||||||
std::vector<file_entry>::const_iterator file_iter;
|
target.size = 0;
|
||||||
|
TORRENT_ASSERT(target.offset + size <= m_total_size);
|
||||||
|
|
||||||
int counter = 0;
|
std::vector<file_entry>::const_iterator file_iter = std::upper_bound(
|
||||||
for (file_iter = begin();; ++counter, ++file_iter)
|
begin(), end(), target, compare_file_offset);
|
||||||
|
|
||||||
|
if (file_iter == end()) return ret;
|
||||||
|
|
||||||
|
size_type file_offset = file_iter->offset;
|
||||||
|
for (; size > 0; file_offset -= file_iter->size, ++file_iter)
|
||||||
{
|
{
|
||||||
TORRENT_ASSERT(file_iter != end());
|
TORRENT_ASSERT(file_iter != end());
|
||||||
if (file_offset < file_iter->size)
|
if (file_offset < file_iter->size)
|
||||||
{
|
{
|
||||||
file_slice f;
|
file_slice f;
|
||||||
f.file_index = counter;
|
f.file_index = file_iter - begin();
|
||||||
f.offset = file_offset + file_iter->file_base;
|
f.offset = file_offset + file_iter->file_base;
|
||||||
f.size = (std::min)(file_iter->size - file_offset, (size_type)size);
|
f.size = (std::min)(file_iter->size - file_offset, (size_type)size);
|
||||||
size -= f.size;
|
size -= f.size;
|
||||||
|
@ -124,9 +134,6 @@ namespace libtorrent
|
||||||
}
|
}
|
||||||
|
|
||||||
TORRENT_ASSERT(size >= 0);
|
TORRENT_ASSERT(size >= 0);
|
||||||
if (size <= 0) break;
|
|
||||||
|
|
||||||
file_offset -= file_iter->size;
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue