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 Prefetch­Virtual­Memory 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 Prefetch­Virtual­Memory was exactly what they needed.

Comments (13)
  1. IanBoyd says:

    This is the kind of function that i wrack my brain trying to find a case where i can use it; just because its neat.

    Premature optimization notwithstanding.

    1. skSdnW says:

      A foxy browser reads some of its files in a background thread at startup to improve performance.

      1. cheong00 says:

        A SQL server loads your most frequently used tables on start if the data size is bigger than the memory available to it.

  2. Brian_EE says:

    With so many large corporate enterprises still on / planning to be on Windows 7 for a while, I hope the customer isn’t a building a business-line application.

    1. PetriL says:

      Since the function is purely a performance hint, calling it conditionally on Windows 8+ should have no functional difference, just a perf gain on newer systems. Of course there’s the test cost, but that would seem pretty minor.

    2. Antonio Rodríguez says:

      I guess there aren’t many business line applications that take performance seriously. Yes, performance is sometimes in the requirements (specially if you are replacing an older, sluggish application), but most of the times it is so low in the list you have to dig to find it. And if you try to justify a design decision basing it on performance, you either get a “who cares?” or a “spend your time in doing real work!”.

      1. DWalker says:

        If you deliver a line-of-business application that performs horribly, the customer will very likely be unhappy.

        1. Kirby FC says:

          Unfortunately, there are too many people who don’t care if the customer is unhappy.

        2. smf says:

          The customer won’t usually be the one using the software, they usually don’t have too much respect for the lowly end users.

        3. MarcK4096 says:

          If you deliver a business line of application that performs horribly, the customer may have no alternative to go to. I am amazed at how poor quality business software tends to be. I used to think that software that cost six figures would be the best ever. But, I found out that it’s usually actually worse quality than common off the shelf software. I guess all that extra money goes into marketing and executive bonuses. With little or no competition, these vendors have captive audiences.

    3. Brian says:

      It’s probably a server app. We’re still on Win7 on the desktop, but we moved all our server apps to Server 2012r2 last year as part of our Server 2003 migration (we didn’t just move the Win2k3 stuff, we moved *everything* – so much fun).

    4. McBucket says:

      OK, so the customer does what developers do and have done since time immemorial: use the new way when you can (in this case, Win 8 and beyond) and use the old way when you must, Raymond said that the new way was better that the old way, but never said that the old way didn’t work.

      But more to the point: “the customer wrote back that Prefetch­Virtual­Memory was exactly what they needed”, which means to me that they are targeting Win 8 and beyond.

  3. Myria says:

    Related functionality is MEM_RESET with VirtualAlloc. Combined with PrefetchVirtualMemory, these give most of the functionality of POSIX madvise.

    Windows has its own feature that POSIX doesn’t in the form of MEM_RESET_UNDO.

Comments are closed.

Skip to main content