0

The VirtualProtect() function in the Win32 API allows one to make memory pages read-only, write-only, executable-only, and a wide range of other settings.

I can see the security motivation, but if I had some memory I'd allocated, say on the heap, and I knew my application would only be reading from that memory, would setting the page to read-only improve access performance?

Likewise, does the same hold for setting it to write-only if I know the application will only be writing to that memory?

I ask, as I have a dim memory from my research into the Vulkan API, that marking certain memory objects, like attachments, as having certain access patterns, tells the driver to optimize that memory object and its layout.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
FShrike
  • 323
  • 1
  • 10
  • 2
    A CPU could in theory take advantage of this, but in practice is is unlikely to. The CPU already knows that all the operations are reads (by observing that that's all you've done), so the cache line remains clean even if the underlying page is R/W. And even though it's R/O to you, it could still be R/W via another mapping, so the CPU would have to be prepared for writes anyway. – Raymond Chen Feb 17 '21 at 21:47
  • @RaymondChen thank you - do you know of any methods to fiddle with the paging or allocation to improve performance through pure winapi calls? Or will VirtualAlloc and HeapAlloc always return similarly performing memory regardless of their inputs? – FShrike Feb 17 '21 at 21:51
  • 1
    Allocating `MEM_LARGE_PAGES` will reduce the number of TLB entries required, which improves address lookup performance. – Raymond Chen Feb 17 '21 at 21:57
  • @RaymondChen would that only be advisable if the application use case was the same as what’s intended by MEM_LARGE_PAGES - am I right in assuming that it’s bad practise to use this insensitively, or can one just generically apply “large pages” to everything and hope for a speed gain – FShrike Feb 17 '21 at 22:00
  • 6
    The point of `MEM_LARGE_PAGES` is so that all the memory in the large page shares a single TLB entry. if you don't pack your data onto that page, then there was no benefit. "You can save money by ordering a large storage container." The point is that you order a large container *instead of a lot of small ones*. If you instead order just as many large containers as you did small ones, then you just wasted money on large containers. – Raymond Chen Feb 17 '21 at 22:05
  • @RaymondChen great analogy thank you – FShrike Feb 17 '21 at 22:19
  • @RaymondChen I read your article “some remarks about VirtualAlloc and MEM_LARGE_PAGES”: is this large pages optimisation feature for servers and servers only, in practise? The article referenced older versions of Windows so I wonder if it still applies today - could I use efficiently use mem_large_pages on my home windows 10 machine? – FShrike Feb 17 '21 at 23:43
  • The types of apps that use `MEM_LARGE_PAGES` are the types of apps that run on dedicated servers. I mean, there's nothing stopping you from running SQL Server on your Home system, but your home system is probably not at the point where TLB misses are the source of your problems. – Raymond Chen Feb 18 '21 at 02:13

0 Answers0