mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-02 17:28:30 +00:00
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:
parent
196942084b
commit
f7780de24b
71 changed files with 1301 additions and 640 deletions
2
third_party/dlmalloc/directmap.inc
vendored
2
third_party/dlmalloc/directmap.inc
vendored
|
@ -57,7 +57,7 @@ static mchunkptr mmap_resize(mstate m, mchunkptr oldp, size_t nb, int flags) {
|
|||
size_t newmmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
|
||||
char* cp = (char*)CALL_MREMAP((char*)oldp - offset,
|
||||
oldmmsize, newmmsize, flags);
|
||||
if (cp != CMFAIL) {
|
||||
if (cp != MAP_FAILED) {
|
||||
mchunkptr newp = (mchunkptr)(cp + offset);
|
||||
size_t psize = newmmsize - offset - MMAP_FOOT_PAD;
|
||||
newp->head = psize;
|
||||
|
|
11
third_party/dlmalloc/dlmalloc.c
vendored
11
third_party/dlmalloc/dlmalloc.c
vendored
|
@ -24,8 +24,7 @@
|
|||
#include "libc/thread/tls.h"
|
||||
#include "third_party/dlmalloc/vespene.internal.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/sysv/consts/mremap.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
|
||||
#if !IsTiny()
|
||||
|
@ -41,7 +40,7 @@
|
|||
#endif
|
||||
|
||||
#define HAVE_MMAP 1
|
||||
#define HAVE_MREMAP 0
|
||||
#define HAVE_MREMAP 1
|
||||
#define HAVE_MORECORE 0
|
||||
#define USE_LOCKS 2
|
||||
#define USE_SPIN_LOCKS 1
|
||||
|
@ -197,7 +196,7 @@ static void* sys_alloc(mstate m, size_t nb) {
|
|||
}
|
||||
|
||||
if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */
|
||||
char* mp = (char*)(dlmalloc_requires_more_vespene_gas(asize));
|
||||
char* mp = dlmalloc_requires_more_vespene_gas(asize);
|
||||
if (mp != CMFAIL) {
|
||||
tbase = mp;
|
||||
tsize = asize;
|
||||
|
@ -368,7 +367,7 @@ static int sys_trim(mstate m, size_t pad) {
|
|||
size_t newsize = sp->size - extra;
|
||||
(void)newsize; /* placate people compiling -Wunused-variable */
|
||||
/* Prefer mremap, fall back to munmap */
|
||||
if (CALL_MREMAP(sp->base, sp->size, newsize, 0) != MFAIL ||
|
||||
if (CALL_MREMAP(sp->base, sp->size, newsize, 0) != MAP_FAILED ||
|
||||
(!extra || !CALL_MUNMAP(sp->base + newsize, extra))) {
|
||||
released = extra;
|
||||
}
|
||||
|
@ -1263,7 +1262,7 @@ void* dlrealloc_single(void* oldmem, size_t bytes) {
|
|||
}
|
||||
#endif /* FOOTERS */
|
||||
if (!PREACTION(m)) {
|
||||
mchunkptr newp = try_realloc_chunk(m, oldp, nb, 1);
|
||||
mchunkptr newp = try_realloc_chunk(m, oldp, nb, MREMAP_MAYMOVE);
|
||||
POSTACTION(m);
|
||||
if (newp != 0) {
|
||||
check_inuse_chunk(m, newp);
|
||||
|
|
2
third_party/dlmalloc/platform.inc
vendored
2
third_party/dlmalloc/platform.inc
vendored
|
@ -511,7 +511,7 @@ FORCEINLINE int win32munmap(void* ptr, size_t size) {
|
|||
#define CALL_MREMAP(addr, osz, nsz, mv) MREMAP_DEFAULT((addr), (osz), (nsz), (mv))
|
||||
#endif /* MREMAP */
|
||||
#else /* HAVE_MMAP && HAVE_MREMAP */
|
||||
#define CALL_MREMAP(addr, osz, nsz, mv) MFAIL
|
||||
#define CALL_MREMAP(addr, osz, nsz, mv) MAP_FAILED
|
||||
#endif /* HAVE_MMAP && HAVE_MREMAP */
|
||||
|
||||
/* mstate bit set if continguous morecore disabled or failed */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue