SQLite Forum

memory vs mmap
You're assuming that all memory is equally fast. RAM mapped into the local process by `malloc()` is *not necessarily* as fast as memory that was previously allocated only to the OS's buffer cache and is now `mmap()`ed into your process's memory space.

The first situation requires only a single user-to-kernel space transition per `malloc()` call at most, possibly less because `malloc()` implementations call [`brk(2)`](https://linux.die.net/man/2/brk) less often than they simply assign chunks from a previously allocated slab and return without calling into the kernel.

While it is possible that a given kernel could literally map every page belonging to a file into your process's RAM on the initial `mmap()` call, there's no actual requirement that it do so. It *could* simply take your arguments, remember your requirements, and then page chunks of that file's buffer cache space into your process as necessary. Page faults and user-to-kernel space transitions aren't free.

Even if it did map everything into your process's virtual address space as a single unbroken buffer, as you're apparently assuming, it would almost certainly do so in a fragmented way as compared to the `malloc()` based method used by SQLite's `:memory:` DB option. That's going to involve the CPU's [TLB](https://en.wikipedia.org/wiki/Translation_lookaside_buffer) mechanism, which is also very much not free to use.

You'd have to dig into your OS's implementation of `mmap()` to find out how it actually operates. Beware: you'll probably find it one of the hairiest parts of the kernel. Memory management on modern systems is *complicated*.