mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-02 02:32:27 +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
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue