Skip to content

Commit 888db62

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

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

llama.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,24 @@ 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);
339+
#ifdef __linux__
340+
void *addr = mmap(NULL, length, PROT_READ, MAP_SHARED | MAP_POPULATE, fd, 0);
341+
#else // MAP_POPULATE is only supported on Linux
334342
void *addr = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
343+
#endif
335344
close(fd);
336345
if (addr == MAP_FAILED) return 0;
346+
// Advise the kernel to preload the mapped memory
347+
madvise(addr, length, MADV_WILLNEED);
337348
#endif
338349
*mm_length = length;
339350
return addr;

0 commit comments

Comments
 (0)