Advise the kernel to preload the mapped memory

This commit is contained in:
Pavol Rusnak 2023-04-03 12:28:49 +02:00
parent 437e77855a
commit 888db62c80
No known key found for this signature in database
GPG key ID: 91F3B339B9A02A3D

View file

@ -327,13 +327,24 @@ static void *mmap_file(const char *fname, uint64_t *mm_length) {
void *addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
CloseHandle(hMapping);
if (!addr) return 0;
// Advise the kernel to preload the mapped memory
WIN32_MEMORY_RANGE_ENTRY range;
range.VirtualAddress = addr;
range.NumberOfBytes = (SIZE_T)length;
PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0);
#else
int fd = open(fname, O_RDONLY);
if (fd == -1) return 0;
int64_t length = lseek(fd, 0, SEEK_END);
#ifdef __linux__
void *addr = mmap(NULL, length, PROT_READ, MAP_SHARED | MAP_POPULATE, fd, 0);
#else // MAP_POPULATE is only supported on Linux
void *addr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
#endif
close(fd);
if (addr == MAP_FAILED) return 0;
// Advise the kernel to preload the mapped memory
madvise(addr, length, MADV_WILLNEED);
#endif
*mm_length = length;
return addr;