Skip to content

Make HEAP_CAP and MAX_THREADS configurable via compile-time defines#49

Open
lancejpollard wants to merge 1 commit intoHigherOrderCO:mainfrom
cluesurf:configurable-heap-threads
Open

Make HEAP_CAP and MAX_THREADS configurable via compile-time defines#49
lancejpollard wants to merge 1 commit intoHigherOrderCO:mainfrom
cluesurf:configurable-heap-threads

Conversation

@lancejpollard
Copy link

@lancejpollard lancejpollard commented Mar 2, 2026

Wraps HEAP_CAP and MAX_THREADS in #ifndef guards so they can be overridden at compile time with -D flags. Adds a compile-time check that HEAP_CAP_BITS does not exceed VAL_BITS.

Usage

clang -O2 -DHEAP_CAP_BITS=28 -DMAX_THREADS=4 -o main main.c

Without -D flags, defaults are identical to current behavior (HEAP_CAP_BITS=38, MAX_THREADS=64).

Why

HEAP_CAP is 1ULL << 38 (256GB virtual). This works on desktop/server but exceeds the address space on some targets:

  • WASM linear memory: 4GB (32-bit) or 16GB (memory64)
  • iOS apps: typically 1-4GB virtual
  • Embedded systems: varies

MAX_THREADS is 64. Not all targets have 64 cores. Embedded and mobile targets benefit from a lower default.

Making these configurable lets embedders tune for their target without patching source. This also sets a precedent for future platform targets (CUDA #33, Metal #30) that may need different defaults.

Safety

A #error fires if HEAP_CAP_BITS > VAL_BITS, since heap addresses must fit in the 38-bit VAL field of a term.

Changes

clang/hvm.c only. 9 lines added, 1 removed. Zero changes to core logic.

Risk

Don't think there are any. Defaults are unchanged. Only affects builds that explicitly pass -D flags. All existing tests pass.

Wrap HEAP_CAP and MAX_THREADS in #ifndef guards so embedders can
tune them at compile time without modifying source.

Add a compile-time check that HEAP_CAP_BITS does not exceed VAL_BITS,
since heap addresses must fit in the 38-bit VAL field of a term.

Usage: clang -O2 -DHEAP_CAP_BITS=28 -DMAX_THREADS=4 -o main main.c

Defaults are unchanged. No behavior change on existing platforms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant