3 ms·
Virtual memory is often misunderstood and, in my opinion, underused by application programmers. For example, using a std vec is a very common pattern for a sim
by stephc_int13 5y ago
Virtual memory is often misunderstood and, in my opinion, underused by application programmers.
For example, using a std vec is a very common pattern for a simple dynamic array, but the whole array content is copied every time it resizes, a cost that is more or less amortized because of the doubling in size every time.
Yet, a very large virtual memory block will have the added benefit of address stability, simplicity of implementation and zero copy, of course there is no free lunch as the page fault triggering the page allocation when needed will interrupt the process.
I should make some benchmark to compare the different approaches at some point…
- someguy101010 5y agoI'd be interested
- legulere 5y agoYou will still pay for at least 4 KB or whatever the page size in your system is. If you reuse the buffer memory use will never go down, if you reallocate on reuse you pay the price of communicating with the operating system. On x86 the virtual memory is just 48 bit big, which means you can have only 2^16 32 bit space entries. To me what you are proposing seems like a clever way of allocating that has a pretty restricted use.
- dataflow 5y ago> Yet, a very large virtual memory block ...of what size? is the problem
- flohofwoe 5y agoOne problem with using the virtual memory system this way is that it will not be portable to platforms without virtual memory (like WASM). Admittedly, probably not a problem for most people, but it should be at least a consideration for library authors (ideally libraries should delegate all their memory management to the library user anyway though).