Skip to content

Commit 3683c88

Browse files
committed
AODNative: read the directory records and key lists of the next DFs ahead
Listing a DF took two dependent reads, its directory record and then its key list, each a round trip on a remote file. Read those of the next 16 DFs, in read order, with them: one vector read for the records and one for the key lists, at most 1 MiB of key lists. Metadata only; the cache is replaced at each read-ahead.
1 parent b322b15 commit 3683c88

2 files changed

Lines changed: 85 additions & 4 deletions

File tree

‎Framework/AODNative/include/AODNative/Container.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ class RootFile
8181
KeyInfo parseKey(std::span<const uint8_t> buf, uint64_t fileOffset);
8282
DirectoryInfo readDirectory(uint64_t offset);
8383
std::vector<KeyInfo> readKeyList(DirectoryInfo const&);
84+
/// The bytes at @a offset: from the head, the metadata read ahead, or the source.
85+
std::vector<uint8_t> readMetadata(uint64_t offset, size_t len);
86+
/// Reads the directory records and key lists of DF @a df and of the next
87+
/// ones not listed yet, in two vector reads, into mAhead.
88+
void listAhead(std::string const& df);
8489
std::optional<KeyInfo> findTopKey(std::string const& name);
8590
std::vector<uint8_t> readStored(const KeyInfo&);
8691

@@ -94,6 +99,9 @@ class RootFile
9499
// Stored bytes of the TTrees of the last DF listed, by key offset, until read.
95100
std::map<uint64_t, std::vector<uint8_t>> mPrefetched;
96101
std::map<std::string, std::vector<KeyInfo>> mKeysOf;
102+
// Directory records and key lists of the next DFs, by offset (small and capped).
103+
std::map<uint64_t, std::vector<uint8_t>> mAhead;
104+
std::vector<std::string> mOrder; // dataFrames(), for the read-ahead
97105
std::vector<std::string> mTreesToPrefetch;
98106
std::map<std::string, std::map<std::string, std::string>> mStringMaps;
99107
};

‎Framework/AODNative/src/Container.cxx‎

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ constexpr size_t kDirectoryReadSize = 64;
2828
// Read at open: the header, the top directory record and, as ROOT writes them
2929
// first, the key and directory record of the first DF, in one round trip.
3030
constexpr size_t kHeadReadSize = 4096;
31+
// Directory records and key lists of the next DFs read along with those of a
32+
// DF: at most this many DFs and this many bytes of key lists.
33+
constexpr size_t kListAheadDFs = 16;
34+
constexpr uint64_t kListAheadBytes = 1024 * 1024;
3135
// Largest gap between the TTrees of a DF still read together with them.
3236
constexpr uint64_t kMetadataGap = 4 * 1024;
3337
// Smallest possible key header: 32-bit seeks and three empty strings.
@@ -103,8 +107,7 @@ DirectoryInfo RootFile::readDirectory(uint64_t offset)
103107
if (offset >= mSource->size()) {
104108
throw CorruptData(mUrl, offset, "directory record past the end of the file");
105109
}
106-
auto len = std::min<uint64_t>(kDirectoryReadSize, mSource->size() - offset);
107-
auto buf = offset + len <= mHead.size() ? std::vector<uint8_t>(mHead.begin() + offset, mHead.begin() + offset + len) : readRange(*mSource, offset, len);
110+
auto buf = readMetadata(offset, std::min<uint64_t>(kDirectoryReadSize, mSource->size() - offset));
108111
Cursor c{buf, mUrl, offset};
109112
DirectoryInfo d;
110113
d.version = c.i16();
@@ -124,7 +127,7 @@ std::vector<KeyInfo> RootFile::readKeyList(DirectoryInfo const& dir)
124127
if (dir.seekKeys == 0 || dir.nbytesKeys <= 0) {
125128
return {};
126129
}
127-
auto buf = readRange(*mSource, dir.seekKeys, dir.nbytesKeys);
130+
auto buf = readMetadata(dir.seekKeys, dir.nbytesKeys);
128131
auto header = parseKey(buf, dir.seekKeys);
129132
Cursor c{buf, mUrl, dir.seekKeys};
130133
c.seek(header.keylen);
@@ -144,6 +147,67 @@ std::vector<KeyInfo> RootFile::readKeyList(DirectoryInfo const& dir)
144147
return keys;
145148
}
146149

150+
std::vector<uint8_t> RootFile::readMetadata(uint64_t offset, size_t len)
151+
{
152+
if (offset + len <= mHead.size()) {
153+
return {mHead.begin() + offset, mHead.begin() + offset + len};
154+
}
155+
if (auto it = mAhead.find(offset); it != mAhead.end() && it->second.size() == len) {
156+
return it->second;
157+
}
158+
return readRange(*mSource, offset, len);
159+
}
160+
161+
void RootFile::listAhead(std::string const& df)
162+
{
163+
mAhead.clear();
164+
auto fetch = [this](std::vector<std::pair<uint64_t, size_t>> const& ranges) {
165+
std::vector<ReadRequest> requests;
166+
for (auto [offset, len] : ranges) {
167+
auto& bytes = mAhead[offset];
168+
bytes.resize(len);
169+
requests.push_back({offset, len, bytes.data()});
170+
}
171+
if (!requests.empty()) {
172+
mSource->readvInto(requests);
173+
}
174+
};
175+
// The directory records of this DF and of the next ones, in read order.
176+
std::vector<uint64_t> records;
177+
std::vector<std::pair<uint64_t, size_t>> ranges;
178+
if (mOrder.empty()) {
179+
mOrder = dataFrames();
180+
}
181+
for (auto it = std::ranges::find(mOrder, df); it != mOrder.end() && records.size() < kListAheadDFs; ++it) {
182+
auto key = findTopKey(*it);
183+
auto offset = key ? key->seekKey + key->keylen : 0;
184+
if (!key || (*it != df && mKeysOf.contains(*it)) || (key->className != "TDirectoryFile" && key->className != "TDirectory") || key->compressed() || offset + kDirectoryReadSize > mSource->size()) {
185+
continue;
186+
}
187+
records.push_back(offset);
188+
if (offset + kDirectoryReadSize > mHead.size()) {
189+
ranges.emplace_back(offset, kDirectoryReadSize);
190+
}
191+
}
192+
fetch(ranges);
193+
// Then their key lists. Anything odd is left to the normal path, which reports it.
194+
ranges.clear();
195+
uint64_t bytes = 0;
196+
for (auto offset : records) {
197+
try {
198+
auto dir = readDirectory(offset);
199+
if (dir.seekKeys == 0 || dir.nbytesKeys <= 0 || dir.seekKeys > mSource->size() || (uint64_t)dir.nbytesKeys > mSource->size() - dir.seekKeys ||
200+
(!ranges.empty() && bytes + dir.nbytesKeys > kListAheadBytes)) {
201+
continue;
202+
}
203+
bytes += dir.nbytesKeys;
204+
ranges.emplace_back(dir.seekKeys, dir.nbytesKeys);
205+
} catch (FormatError const&) {
206+
}
207+
}
208+
fetch(ranges);
209+
}
210+
147211
const std::vector<KeyInfo>& RootFile::topKeys()
148212
{
149213
if (!mTopKeys) {
@@ -235,7 +299,16 @@ std::vector<KeyInfo> RootFile::keysOf(const std::string& df)
235299
if (key->compressed()) {
236300
throw UnsupportedLayout("compressed directory record for " + df + " in " + mUrl);
237301
}
238-
auto dir = readDirectory(key->seekKey + key->keylen);
302+
// The records of the next DFs are read ahead with those of this one; the
303+
// first DF's directory record comes with the head, its key list does not.
304+
auto offset = key->seekKey + key->keylen;
305+
if (!mSource->canMap() && offset + kDirectoryReadSize > mHead.size() && !mAhead.contains(offset)) {
306+
listAhead(df);
307+
}
308+
auto dir = readDirectory(offset);
309+
if (!mSource->canMap() && !mAhead.contains(dir.seekKeys)) {
310+
listAhead(df);
311+
}
239312
auto keys = highestCycles(readKeyList(dir));
240313

241314
// The TTrees of a DF are written next to each other: ranges less than

0 commit comments

Comments
 (0)