cosmopolitan/examples
Justine Tunney 3609f65de3
Make malloc() go 200x faster
If pthread_create() is linked into the binary, then the cosmo runtime
will create an independent dlmalloc arena for each core. Whenever the
malloc() function is used it will index `g_heaps[sched_getcpu() / 2]`
to find the arena with the greatest hyperthread / numa locality. This
may be configured via an environment variable. For example if you say
`export COSMOPOLITAN_HEAP_COUNT=1` then you can restore the old ways.
Your process may be configured to have anywhere between 1 - 128 heaps

We need this revision because it makes multithreaded C++ applications
faster. For example, an HTTP server I'm working on that makes extreme
use of the STL went from 16k to 2000k requests per second, after this
change was made. To understand why, try out the malloc_test benchmark
which calls malloc() + realloc() in a loop across many threads, which
sees a a 250x improvement in process clock time and 200x on wall time

The tradeoff is this adds ~25ns of latency to individual malloc calls
compared to MODE=tiny, once the cosmo runtime has transitioned into a
fully multi-threaded state. If you don't need malloc() to be scalable
then cosmo provides many options for you. For starters the heap count
variable above can be set to put the process back in single heap mode
plus you can go even faster still, if you include tinymalloc.inc like
many of the programs in tool/build/.. are already doing since that'll
shave tens of kb off your binary footprint too. Theres also MODE=tiny
which is configured to use just 1 plain old dlmalloc arena by default

Another tradeoff is we need more memory now (except in MODE=tiny), to
track the provenance of memory allocation. This is so allocations can
be freely shared across threads, and because OSes can reschedule code
to different CPUs at any time.
2024-06-05 02:02:14 -07:00
..
package Make malloc() go 200x faster 2024-06-05 02:02:14 -07:00
pyapp Stop using .com extension in monorepo 2024-03-03 03:12:19 -08:00
pylife Stop using .com extension in monorepo 2024-03-03 03:12:19 -08:00
asantest.c Refactor some code 2024-05-24 11:44:44 -07:00
blas.cc Improve AVX512 feature detection 2024-05-07 03:19:49 -07:00
BUILD.mk Make malloc() go 200x faster 2024-06-05 02:02:14 -07:00
clear.c Overhaul process spawning 2023-09-10 08:17:44 -07:00
crashreport.c Improve backtraces 2024-05-30 15:23:11 -07:00
crashreport2.cc Improve crash backtrace reliability 2024-05-07 18:10:28 -07:00
ctrlc.c Fix some additional Windows TTY issues 2023-10-04 02:17:25 -07:00
date.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
dlopen.c Fix dlopen() for FreeBSD and NetBSD 2023-11-18 04:35:48 -08:00
env.c Run clang-format (#1197) 2024-06-01 16:30:43 -04:00
greenbean.c Refactor and improve CTL and other code 2024-06-04 05:45:48 -07:00
gui.c Make examples folder somewhat more focused 2021-03-05 06:09:12 -08:00
hangman.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
hello.c Remove printf() linking hack 2023-06-17 10:13:50 -07:00
hello2.c Introduce new linker for fat ape binaries 2023-08-11 04:39:19 -07:00
hellolua.c Fix warnings 2023-09-01 20:50:18 -07:00
hellolua.lua Add Hello World example for LUA C API (#97) 2021-03-08 14:15:59 -08:00
hiredis.c Run clang-format (#1197) 2024-06-01 16:30:43 -04:00
kilo.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
life.c Always initialize thread local storage 2022-07-19 00:21:46 -07:00
linenoise.c Clean up some code 2023-10-11 11:45:31 -07:00
localtime.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
ls.c Bring back gc() function 2024-01-08 10:26:28 -08:00
nc.c Use DNS implementation from Musl Libc 2023-12-28 23:04:35 -08:00
nesemu1.cc Implement proper time zone support 2024-05-04 23:06:37 -07:00
parsefloat.c Run clang-format (#1197) 2024-06-01 16:30:43 -04:00
pause.c Write more tests and improve kill() on Windows 2023-10-13 04:38:45 -07:00
picol.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
portscan.c Clean up some code 2023-11-16 17:31:07 -08:00
print-struct.c Cull the examples folder 2023-10-11 21:45:32 -07:00
printargs.c Make improvements 2022-04-24 10:06:05 -07:00
rusage.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
script.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
script.txt Add script.com and whois.com courtesy of FreeBSD 2022-09-13 20:26:13 -07:00
seq.c Cull the examples folder 2023-10-11 21:45:32 -07:00
setcontext.c Implement swapcontext() and makecontext() 2023-07-02 09:01:44 -07:00
setitimer.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
spawn_bench.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
stackexplorer.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
stat.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
statfs.c Mint APE Loader v1.7 2023-08-17 09:04:50 -07:00
stringbuffer.c Clean up some code 2023-11-16 17:31:07 -08:00
sysconf.c Clean up some code 2023-10-11 11:45:31 -07:00
sysinfo.c Clean up some code 2023-10-11 11:45:31 -07:00
system.c Make improvements 2024-02-12 10:23:00 -08:00
trapping.c Introduce support for trapping math 2024-04-30 13:38:43 -07:00
ttyinfo.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
ucontext-sigfpe-recovery.c Cull the examples folder 2023-10-11 21:45:32 -07:00
uname.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
unbourne.c Run clang-format (#1197) 2024-06-01 16:30:43 -04:00
vga.c Mint APE Loader v1.5 2023-07-26 13:54:49 -07:00
vga2.c [metal] Add a uprintf() routine, for non-emergency boot logging (#905) 2023-10-25 14:32:20 -07:00
walk.c Run clang-format (#1197) 2024-06-01 16:30:43 -04:00
wall.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
whois.c Release Cosmopolitan v3.3 2024-02-20 13:27:59 -08:00