Make realloc() go 100x faster on Linux/NetBSD

Cosmopolitan now supports mremap(), which is only supported on Linux and
NetBSD. First, it allows memory mappings to be relocated without copying
them; this can dramatically speed up data structures like std::vector if
the array size grows larger than 256kb. The mremap() system call is also
10x faster than munmap() when shrinking large memory mappings.

There's now two functions, getpagesize() and getgransize() which help to
write portable code that uses mmap(MAP_FIXED). Alternative sysconf() may
be called with our new _SC_GRANSIZE. The madvise() system call now has a
better wrapper with improved documentation.
This commit is contained in:
Justine Tunney 2024-07-07 12:24:25 -07:00
parent 196942084b
commit f7780de24b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
71 changed files with 1301 additions and 640 deletions

View file

@ -1,2 +0,0 @@
#include "libc/sysv/macros.internal.h"
.scall __sys_mremap,0x19bffffffffff019,216,4095,4095,globl,hidden

View file

@ -0,0 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_mremap,0x19bffffffffff019,216,4095,4095,globl,hidden

View file

@ -228,6 +228,7 @@ syscon mmap MAP_LOCKED 0x00002000 0x00002000 0 0 0 0 0 0
syscon mmap MAP_NORESERVE 0x00004000 0x00004000 0x00000040 0x00000040 0 0 0x00000040 0 # Linux calls it "reserve"; NT calls it "commit"? which is default?
syscon mmap MAP_POPULATE 0x00008000 0x00008000 0 0 0x00040000 0 0 0 # MAP_PREFAULT_READ on FreeBSD; can avoid madvise(MADV_WILLNEED) on private file mapping
syscon mmap MAP_NONBLOCK 0x00010000 0x00010000 0 0 0 0 0 0
syscon mmap MAP_NOFORK 0 0 0 0 0 0 0 0x10000000 # used on pages internal to our mmap() implemention on windows
syscon mmap MAP_SYNC 0x00080000 0x00080000 0 0 0 0 0 0 # perform synchronous page faults for mapping (Linux 4.15+)
syscon mmap MAP_HUGETLB 0x00040000 -1 -1 -1 -1 -1 -1 -1 # make it inherit across execve()
syscon mmap MAP_INHERIT -1 -1 -1 -1 -1 -1 0x00000080 -1 # make it inherit across execve()
@ -294,13 +295,6 @@ syscon mprot PROT_WRITE 2 2 2 2 2 2 2 2 # mmap, mprotect, uni
syscon mprot PROT_EXEC 4 4 4 4 4 4 4 4 # mmap, mprotect, unix consensus
syscon mprot PROT_GUARD 0 0 0 0 0 0 0 0x100 # mmap, mprotect, unix consensus
# mremap() flags
# the revolutionary praxis of realloc()
#
# group name GNU/Systemd GNU/Systemd (Aarch64) XNU's Not UNIX! MacOS (Arm64) FreeBSD OpenBSD NetBSD The New Technology Commentary
syscon mremap MREMAP_MAYMOVE 1 1 1 1 1 1 1 1 # faked non-linux (b/c linux only)
syscon mremap MREMAP_FIXED 2 2 2 2 2 2 2 2 # faked non-linux (b/c linux only)
# sigprocmask() flags
#
# group name GNU/Systemd GNU/Systemd (Aarch64) XNU's Not UNIX! MacOS (Arm64) FreeBSD OpenBSD NetBSD The New Technology Commentary

View file

@ -0,0 +1,2 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon mmap,MAP_NOFORK,0,0,0,0,0,0,0,0x10000000

View file

@ -1,2 +0,0 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon mremap,MREMAP_FIXED,2,2,2,2,2,2,2,2

View file

@ -1,2 +0,0 @@
#include "libc/sysv/consts/syscon.internal.h"
.syscon mremap,MREMAP_MAYMOVE,1,1,1,1,1,1,1,1

View file

@ -19,6 +19,7 @@ extern const int MAP_JIT;
extern const int MAP_LOCKED;
extern const int MAP_NOCACHE;
extern const int MAP_NOEXTEND;
extern const int MAP_NOFORK;
extern const int MAP_NONBLOCK;
extern const int MAP_NORESERVE;
extern const int MAP_NOSYNC;

View file

@ -59,7 +59,7 @@ scall __sys_pipe 0x02a10721e202a016 0x03b globl hidden # NOTE: pipe2() on FreeB
scall sys_select 0x9a184785d285d817 0xfff globl hidden
scall sys_pselect 0x9b486ea0a298a90e 0x848 globl hidden # pselect6() on gnu/systemd
scall sys_sched_yield 0x15e12a14bf25d018 0x07c globl hidden # select() on XNU (previously swtch() but removed in 12.4)
scall __sys_mremap 0x19bffffffffff019 0x0d8 globl hidden
scall sys_mremap 0x19bffffffffff019 0x0d8 globl hidden
scall sys_mincore 0x04e04e04e204e01b 0x0e8 globl hidden
scall sys_madvise 0x04b04b04b204b01c 0x0e9 globl hidden
scall sys_shmget 0x0e71210e7210901d 0x0c2 globl # no wrapper