Skip to content

Read ahead blocks only within the records' cluster - #132

Merged
mvassilev merged 3 commits into
developfrom
cluster-read-ahead
Nov 9, 2026
Merged

mvassilev merged 3 commits into
developfrom
cluster-read-ahead

Conversation

@mvassilev

Copy link
Copy Markdown
Collaborator

A full read of a RAM file reads each cluster several times: strace counts 2.04 GB of pread
for chromosome 20's 418 MB file, and 6.6 GB for the 2.2 GB file of NA12878's HiSeq chromosome
20. The cause is ROOT's cluster cache and the block readers. On a request for a cluster, the
cache keeps that cluster and the next one and drops the rest (RClusterPool::GetCluster). The
readers of names, qualities and bases read ahead two, four and one blocks, each at the last row
of its block. Near the end of a cluster those rows are in the next cluster, so the request drops
the cluster the records are still in, and the next page a record needs from it reads it again.

Every block lies in one cluster: on chromosome 20 (138 clusters, 1,863 blocks), HiSeq (148,
1,479), the whole genome converted from BAM (5,006, 78,644) and a file written on one thread,
since the writers end a cluster with the input block and the quality block with it.

The change

  • FlaggedBlockReader and NameBlockReader know the cluster of each block, and read ahead
    only blocks of the cluster of the block they are in. The first block of the next cluster is
    decoded when the records reach it.
  • Except a block of fqzcomp, 25 times slower to decode than rANS: waiting for the first block
    of each cluster made a full read of 2 million records 5.5 to 5.9 s instead of 4.4 to 4.5 s,
    so after such a block the quality reader reads ahead from the next cluster as before.
  • Only with the cluster cache, which RAMFile::UsesClusterCache() now says. ramdump reads
    regions without it, page by page: there the cache drops nothing, and the wait made a 10 Mb
    region 3.07 to 3.18 s instead of 2.89 to 3.03 s.
  • A test writes clusters of 16 rows with pages of 64 bytes and checks with ROOT's metrics
    that a full read reads no more than the file holds: 21,396 bytes of 8,886 before.

Tried and dropped: pinning the records' cluster with RNTupleReader::CreateActiveEntryToken().
The cache then keeps it, but without implicit multithreading ROOT keeps every page released
from a pinned cluster until the reader closes, all of the decoded file in a full read. A
second reader for the block columns would double the cost of opening, 0.36 s and 115 MB on
the whole genome, for every region.

What it does

z840 socket 0, on top of #131, against #131, in turn, both on 0.2.0's files; the outputs are
the same, and ramdump -h gives back the SAM files' md5 on chromosome 20 and HiSeq:

#131 this change
HiSeq chr20, full read 23.79 to 24.35 s 19.32 to 19.90 s
read from the file 6.60 GB 2.17 GB
system time 5.84 to 6.08 s 1.24 to 1.30 s
user time 32.4 to 33.2 s 30.9 to 31.7 s
peak memory 143 to 159 MB 132 to 149 MB
chr20, full read 15.07 to 15.97 s 14.49 to 15.24 s
read from the file 2.04 GB 0.42 GB
system time 1.33 to 1.40 s 0.62 to 0.65 s
peak memory 93 to 105 MB 97 to 100 MB
chr20 2M, fqzcomp, full read 4.40 to 4.62 s 4.44 to 4.71 s
chr20 2M, written on one thread 1.71 to 1.84 s 1.68 to 1.71 s

Three to six runs each. Unchanged: -@ 8 on chromosome 20 (4.97 to 5.16 s), a 10 Mb region
of chromosome 20 (2.96 to 3.01 s against 2.90 to 2.97 s) and of chromosome 1 on the whole
genome (2.59 to 2.69 s against 2.58 to 2.61 s), a 1 kb region in a process of its own (0.06 s
on chromosome 20, 0.44 to 0.53 s on the whole genome).

14 of 14 tests pass on z840 (ROOT 6.40.04, htslib 1.25); the new test fails without the change.

This branch starts from #131's, so it is best merged after it.

Changelog

  • Changed: a full read reads each cluster once; HiSeq chromosome 20 in 19.3 to 19.9 s instead
    of 23.8 to 24.4 s.

A full read read each cluster several times: 2.04 GB for chromosome 20's
418 MB file, 6.6 GB for HiSeq chromosome 20's 2.2 GB. ROOT's cluster
cache keeps the cluster it is asked for and the next one; the block
readers read ahead at the last row of each block, near the end of a
cluster in the next one, and so dropped the cluster the records were
still in, which they then read again.

Every block lies in one cluster, so the readers of names, qualities and
bases now read ahead only blocks of their block's cluster, when the file
is read with the cluster cache. A block of fqzcomp, 25 times slower to
decode, still reads ahead from the next cluster. RAMFile says whether
its reader uses the cache. A test checks with ROOT's metrics that a full
read reads no more than the file holds.

On z840, against #131: HiSeq chromosome 20 reads 2.17 GB, in 19.3 to
19.9 s instead of 23.8 to 24.4 s, with 1.3 s of system time instead of
6.0 s; chromosome 20 reads 0.42 GB, in 14.5 to 15.2 s instead of 15.1 to
16.0 s. Regions, -@ N and fqzcomp files take the same time.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

Comment thread test/record_blocks_test.cxx Outdated
const std::string bases = "ACGT";
std::vector<Read> reads;
for (std::size_t i = 0; i < kReads; i++)
reads.push_back({0, std::string(kLength, static_cast<char>('!' + i % 40)), "read" + std::to_string(i),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: '%' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]

Suggested change
reads.push_back({0, std::string(kLength, static_cast<char>('!' + i % 40)), "read" + std::to_string(i),
reads.push_back({0, std::string(kLength, static_cast<char>('!' + (i % 40))), "read" + std::to_string(i),

Comment thread test/record_blocks_test.cxx Outdated
const std::string bases = "ACGT";
std::vector<Read> reads;
for (std::size_t i = 0; i < kReads; i++)
reads.push_back({0, std::string(kLength, static_cast<char>('!' + i % 40)), "read" + std::to_string(i),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-inefficient-vector-operation]

test/record_blocks_test.cxx:380:

-    for (std::size_t i = 0; i < kReads; i++)
+    reads.reserve(kReads);
+ for (std::size_t i = 0; i < kReads; i++)

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.54930% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.41%. Comparing base (7d71704) to head (6717eed).
⚠️ Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
test/record_blocks_test.cxx 87.50% 2 Missing and 3 partials ⚠️
src/ramcore/RecordBlocks.cxx 96.15% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #132      +/-   ##
===========================================
+ Coverage    89.38%   89.41%   +0.02%     
===========================================
  Files           26       26              
  Lines         3722     3789      +67     
  Branches      1267     1289      +22     
===========================================
+ Hits          3327     3388      +61     
- Misses         203      204       +1     
- Partials       192      197       +5     
Flag Coverage Δ
unittests 89.41% <91.54%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
inc/ramcore/RecordBlocks.h 100.00% <ø> (ø)
inc/rntuple/RAMFile.h 100.00% <100.00%> (ø)
src/rntuple/RAMFile.cxx 87.71% <100.00%> (+0.21%) ⬆️
src/ramcore/RecordBlocks.cxx 91.00% <96.15%> (-0.04%) ⬇️
test/record_blocks_test.cxx 95.65% <87.50%> (-1.39%) ⬇️
Files with missing lines Coverage Δ
inc/ramcore/RecordBlocks.h 100.00% <ø> (ø)
inc/rntuple/RAMFile.h 100.00% <100.00%> (ø)
src/rntuple/RAMFile.cxx 87.71% <100.00%> (+0.21%) ⬆️
src/ramcore/RecordBlocks.cxx 91.00% <96.15%> (-0.04%) ⬇️
test/record_blocks_test.cxx 95.65% <87.50%> (-1.39%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

clang-tidy review says "All clean, LGTM! 👍"

@mvassilev
mvassilev merged commit 3c7b9ef into develop Nov 9, 2026
6 of 8 checks passed
@mvassilev
mvassilev deleted the cluster-read-ahead branch November 9, 2026 08:12
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