How do I preload a chunk of memory into a memory-mapped file?
A customer had a moderate-sized memory-mapped file (a few dozen megabytes), and they wanted to preload the entire file into memory because their program accesses the contents of the file non-sequentially, which means the normal prefetcher cannot anticipate which page to preload. They are forcing the pages to be preloaded by manually walking through the file and touching every page. They wanted to know if there was a better way of accomplishing this.
Windows 8 introduces the
PrefetchVirtualMemory function. It allows you to request that specific sections of your application’s virtual address space be preloaded from disk. By submitting a prefetch request, the memory manager can issue block I/O reads in order to satisfy the request, which is more efficient than reading each page one at a time as you touch them.
Note that prefetching virtual memory is purely advisory. The memory manager is free to ignore your request, or to honor only parts of it.
The customer wrote back that
PrefetchVirtualMemory was exactly what they needed.