From 02af016c4de1279442df6234f5e6fbd8e0898111 Mon Sep 17 00:00:00 2001 From: lizzie Date: Tue, 14 Apr 2026 20:42:06 +0000 Subject: [PATCH] fix linux readahead --- src/common/fs/file.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/common/fs/file.cpp b/src/common/fs/file.cpp index bad7710ecc..db45214b98 100644 --- a/src/common/fs/file.cpp +++ b/src/common/fs/file.cpp @@ -10,6 +10,7 @@ #include "common/bit_util.h" #include "common/fs/file.h" #include "common/fs/fs.h" +#include "common/fs_types.h" #ifdef ANDROID #include "common/fs/fs_android.h" #endif @@ -301,7 +302,13 @@ static int PlatformMapReadOnly(IOFile& io, const char* path) { close(io.mmap_fd); io.mmap_fd = -1; } else { - posix_madvise(io.mmap_base, io.mmap_size, POSIX_MADV_WILLNEED); + // For small files it is acceptable to use a full readahead + // See https://github.com/torvalds/linux/blob/e80d033851b3bc94c3d254ac66660ddd0a49d72c/include/linux/pagemap.h#L1392 + if (st.st_size >= 256_MiB) { + posix_madvise(io.mmap_base, io.mmap_size, POSIX_MADV_RANDOM); + } else { + posix_madvise(io.mmap_base, io.mmap_size, POSIX_MADV_SEQUENTIAL); + } } } return io.mmap_fd;