-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
Currently it allocates and zeros a scratch buffer of up to 1MB. Instead we should declare a global like _Alignas(4096) static char zeros[1024*1024]; and we will have a semi-magical zero buffer to use. This will have some nice properties:
- There will be a single zero buffer for the whole process rather than one per call.
- It will be in the .bss section (or other platforms' equivalent) so it won't take up any space in the binary.
- At least on linux, when the virtual memory gets populated, it will all be backed by the single special "zero page", so it won't actually take up any physical memory. You could have an arbitrary amount of zero bytes for free using this trick.
It is very important that it isn't declared const but is treated as-if it is and never written to, even with zeros. If it is declared const, it goes in .rodata rather than .bss and takes up space (I filed a gcc bug to do this as an optimization). If it is written to with zeros, it breaks the magic single-page backing.