@@ -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.
3030constexpr 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.
3236constexpr 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+
147211const 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