Skip to content

V2 block hashes#8395

Open
arvidn wants to merge 4 commits into
RC_2_1from
v2-block-hashes
Open

V2 block hashes#8395
arvidn wants to merge 4 commits into
RC_2_1from
v2-block-hashes

Conversation

@arvidn
Copy link
Copy Markdown
Owner

@arvidn arvidn commented May 19, 2026

No description provided.

@arvidn arvidn requested a review from Copilot May 19, 2026 07:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for caching/consuming precomputed v2 (SHA-256) block hashes via the storage layer so hash jobs can avoid re-reading blocks from disk, and updates tests accordingly.

Changes:

  • Introduce per-piece storage of precomputed v2 block hashes in pread_storage and mmap_storage, consumed by hash/hash2 jobs.
  • Update disk I/O backends (pread_disk_io, mmap_disk_io) and disk_cache to produce/consume these precomputed hashes and drop them on clear-piece.
  • Adjust tests: move end-to-end v2 block hash validation to test_disk_io and remove cache-local v2 assertions/tests.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
test/test_disk_io.cpp Adds explicit expected v1/v2 hash verification for async hashing.
test/test_disk_cache.cpp Removes v2 hash assertions/tests that depended on cache-owned v2 hashes; adds explanatory comments.
test/disk_cache_test_utils.hpp Updates piece_entry_params construction to pass the new storage field.
src/pread_storage.cpp Implements storage-backed precomputed v2 hash cache (store/take/drop).
src/pread_disk_io.cpp Consumes storage-backed v2 hashes for hash and adds a precomputed fast-path for hash2.
src/mmap_storage.cpp Implements storage-backed precomputed v2 hash cache (store/take/drop) and stores v1/v2 flags.
src/mmap_disk_io.cpp Computes v2 hashes inline during writes, consumes them during hash/hash2, and drops them on clear-piece.
src/disk_cache.cpp Routes v2 block-hash production/consumption through pread_storage instead of cache-local arrays.
include/libtorrent/aux_/pread_storage.hpp Declares precomputed v2 hash APIs and backing container.
include/libtorrent/aux_/mmap_storage.hpp Declares precomputed v2 hash APIs and backing container + v1/v2 flags.
include/libtorrent/aux_/disk_cache.hpp Updates cache entry/params to carry storage weak_ptr; removes cache-local v2 hash snapshot plumbing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/libtorrent/aux_/mmap_storage.hpp
Comment thread src/pread_disk_io.cpp
Comment on lines +1058 to +1059
int const ret =
j->storage->hash2(m_settings, h, len, a.piece, a.offset, file_mode, j->flags, j->error);
Comment thread src/pread_storage.cpp
Comment on lines +101 to +109
std::optional<sha256_hash> pread_storage::take_precomputed_v2_block(
piece_index_t const piece, int const block)
{
std::lock_guard<std::mutex> l(m_precomputed_v2_mutex);
auto const it = m_precomputed_v2.find(piece);
if (it == m_precomputed_v2.end()) return std::nullopt;
auto& entry = it->second;
if (block >= entry.present.size() || !entry.present.get_bit(block)) return std::nullopt;
sha256_hash const h = entry.hashes[block];
Comment thread test/test_disk_io.cpp
Comment on lines +142 to +157
std::vector<lt::sha256_hash> expected_v2;
lt::sha1_hash expected_v1;
if (need_v2)
{
expected_v2.resize(blocks_in_piece);
int const v2_blocks = fs.blocks_in_piece2(p);
int const v2_size = fs.piece_size2(p);
for (int b = 0; b < v2_blocks; ++b)
{
int const off = b * block_size;
int const bsize = std::min(block_size, v2_size - off);
lt::hasher256 hh;
hh.update({buffer.data() + off, bsize});
expected_v2[b] = hh.final();
}
}
Comment thread src/disk_cache.cpp
Comment on lines +371 to +376
auto pc = storage->take_precomputed_v2(loc.piece);
int const to_copy =
std::min(int(job2.block_hashes.size()), int(pc.hashes.size()));
for (int idx = 0; idx < to_copy; ++idx)
if (idx < pc.present.size() && pc.present.get_bit(idx))
job2.block_hashes[idx] = pc.hashes[idx];
Comment thread src/pread_disk_io.cpp
Comment on lines +1044 to +1052
int const blk = a.offset / default_block_size;
if (auto h = j->storage->take_precomputed_v2_block(a.piece, blk))
{
a.piece_hash2 = *h;
std::int64_t const read_time = total_microseconds(clock_type::now() - start_time);
m_stats_counters.inc_stats_counter(counters::disk_hash_time, read_time);
m_stats_counters.inc_stats_counter(counters::disk_job_time, read_time);
return {};
}
@arvidn arvidn force-pushed the v2-block-hashes branch from b2e0fa8 to 682f5ac Compare May 19, 2026 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants