Skip to content

Commit eaec9b6

Browse files
committed
Advise the kernel to preload the mapped memory
1 parent 437e778 commit eaec9b6

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

llama.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,20 @@ static void *mmap_file(const char *fname, uint64_t *mm_length) {
327327
void *addr = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
328328
CloseHandle(hMapping);
329329
if (!addr) return 0;
330+
// Advise the kernel to preload the mapped memory
331+
WIN32_MEMORY_RANGE_ENTRY range;
332+
range.VirtualAddress = addr;
333+
range.NumberOfBytes = (SIZE_T)length;
334+
PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0);
330335
#else
331336
int fd = open(fname, O_RDONLY);
332337
if (fd == -1) return 0;
333338
int64_t length = lseek(fd, 0, SEEK_END);
334339
void *addr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
335340
close(fd);
336341
if (addr == MAP_FAILED) return 0;
342+
// Advise the kernel to preload the mapped memory
343+
madvise(addr, length, MADV_WILLNEED);
337344
#endif
338345
*mm_length = length;
339346
return addr;

0 commit comments

Comments
 (0)