Skip to content

Commit e1fc47a

Browse files
committed
perf: read the metadata ahead and map it up front
Mapping the metadata lazily moved the cost of loading it onto the first access of each class, as page faults, and made those accesses noisier. Populating the mapping removes that, and asking for the whole file to be read ahead first keeps a cold start from paying for it one read-ahead window at a time: on the API 36 emulator with the page cache dropped, loading the metadata takes 3.1 ms read into the heap, 3.8 ms populated without the read-ahead and 2.2 ms with it.
1 parent 2488d7f commit e1fc47a

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

‎test-app/runtime/src/main/cpp/MetadataNode.cpp‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,10 +2188,16 @@ namespace {
21882188

21892189
/*
21902190
* Maps one of the metadata files read-only rather than reading it into the
2191-
* heap: only the pages the app touches are loaded, and being clean and
2192-
* file-backed they can be dropped under memory pressure. The extractor
2193-
* replaces these files by renaming a new one over them, never by writing in
2194-
* place, so a mapping keeps the contents it was made from.
2191+
* heap. Its pages are the file's own, clean ones, which the system can drop
2192+
* under memory pressure and load again, where a heap copy is private memory.
2193+
*
2194+
* The whole file is read ahead and mapped up front. Left to fault in on
2195+
* first touch, that cost and its variance land on the first access of each
2196+
* class. Populated without the read-ahead, the pages arrive one read-ahead
2197+
* window at a time and a cold start takes longer than a plain read.
2198+
*
2199+
* The extractor replaces these files by renaming a new one over them, never
2200+
* by writing in place, so a mapping keeps the contents it was made from.
21952201
*/
21962202
MappedMetadataFile MapMetadataFile(const string& dir, const char* name) {
21972203
string path = dir + "/" + name;
@@ -2201,7 +2207,9 @@ namespace {
22012207
int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
22022208
if (fd >= 0) {
22032209
if (fstat(fd, &info) == 0 && info.st_size > 0) {
2204-
data = mmap(nullptr, static_cast<size_t>(info.st_size), PROT_READ, MAP_PRIVATE, fd, 0);
2210+
posix_fadvise(fd, 0, info.st_size, POSIX_FADV_WILLNEED);
2211+
data = mmap(nullptr, static_cast<size_t>(info.st_size), PROT_READ,
2212+
MAP_PRIVATE | MAP_POPULATE, fd, 0);
22052213
}
22062214
int error = errno;
22072215
close(fd);

0 commit comments

Comments
 (0)