flawed calculation of first and last file pieces #321

Closed
opened 2026-02-21 15:01:28 -05:00 by deekerman · 3 comments
Owner

Originally created by @integer-division-what on GitHub (Jan 27, 2013).

There are some flaws in the piece calculations starting around line 65 of src/qtlibtorrent/qtorrenthandle.cpp.

First, integer division should be used, as its much cleaner. No need for messy floats.

Second, the calculation of num_pieces_in_file is incorrect in some cases when the file starts near the end of a piece. But it's unnecessary to compute this number anyway-- last_piece can be computed directly.

Third, some files need more than just their last piece in order to be previewable. For instance, some retarded mp4 files need almost 1% of the tail of the file in order to play. So I propose that the first and last 1% of each file be prioritized. Something like the following:

int first_piece1 = file.offset / piece_size;
int first_piece2 = (file.offset + file.size / 100) / piece_size;

int last_piece1 = (file.offset + file.size - 1 - file.size / 100) / piece_size;
int last_piece2 = (file.offset + file.size - 1) / piece_size;

Then set the priority on the pieces in the ranges [first_piece1, first_piece2] and [last_piece1, last_piece2].

Originally created by @integer-division-what on GitHub (Jan 27, 2013). There are some flaws in the piece calculations starting around line 65 of src/qtlibtorrent/qtorrenthandle.cpp. First, integer division should be used, as its much cleaner. No need for messy floats. Second, the calculation of num_pieces_in_file is incorrect in some cases when the file starts near the end of a piece. But it's unnecessary to compute this number anyway-- last_piece can be computed directly. Third, some files need more than just their last piece in order to be previewable. For instance, some retarded mp4 files need almost 1% of the tail of the file in order to play. So I propose that the first and last 1% of each file be prioritized. Something like the following: int first_piece1 = file.offset / piece_size; int first_piece2 = (file.offset + file.size / 100) / piece_size; int last_piece1 = (file.offset + file.size - 1 - file.size / 100) / piece_size; int last_piece2 = (file.offset + file.size - 1) / piece_size; Then set the priority on the pieces in the ranges [first_piece1, first_piece2] and [last_piece1, last_piece2].
Author
Owner

@Gelmir commented on GitHub (Jan 27, 2013):

Partially resolved in pull #189 which is kind of old

@Gelmir commented on GitHub (Jan 27, 2013): Partially resolved in pull #189 which is kind of old
Author
Owner

@thalieht commented on GitHub (Jan 24, 2017):

@ngosang close?

@thalieht commented on GitHub (Jan 24, 2017): @ngosang close?
Author
Owner

@ngosang commented on GitHub (Jan 25, 2017):

Already fixed in #3816

@ngosang commented on GitHub (Jan 25, 2017): Already fixed in #3816
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/qBittorrent#321
No description provided.