Retain large allocations on the heap by default#1213
Merged
Conversation
Contributor
|
Please update |
Clarify memory allocation behavior and performance impact.
yspolyakov
approved these changes
Jul 2, 2026
pascoec
added a commit
that referenced
this pull request
Jul 8, 2026
Make the process-wide allocator tuning introduced in #1213 opt-out via a new build flag, since it changes malloc policy for the whole host application (freed memory is retained in-process, raising resident set size for workloads with large transient peaks). - Gate the glibc mallopt(M_MMAP_MAX=0, M_TRIM_THRESHOLD=-1) startup tuning behind WITH_MALLOC_TUNING and propagate the flag through config_core.h; build with -DWITH_MALLOC_TUNING=OFF to disable. - Document per-platform behavior in explicit branches: macOS libmalloc already retains freed memory (madvise-reusable pages plus the large cache), and the Windows process heap exposes no retention knob. - Rewrite the __APPLE__ branch of AllocTrim() to use the supported malloc_zone_pressure_relief(NULL, 0) all-zones call instead of the malloc_get_all_zones()/mach introspection API, dropping <mach/mach.h>. - Docs: add the new flag and the previously undocumented options (GIT_SUBMOD_AUTO, WITH_COVTEST, WITH_NOISE_DEBUG, WITH_REDUCED_NOISE, USE_MACPORTS) to the CMake flags table, and fix stale defaults (WITH_BE2 is OFF; the default math backend is 4, not 2).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OpenFHE operations repeatedly allocate and free large (~0.5–30 MB) polynomial buffers. By default glibc returns each freed buffer to the OS (brk trim / munmap) and re-faults the pages on the next operation; single-threaded, this page-fault/kernel overhead can dominate runtime (~2× on allocation-bound ops such as EvalAdd).
This change sets, once at library load (glibc only):
M_MMAP_MAX = 0 — keep large allocations on the heap
M_TRIM_THRESHOLD = -1 — never auto-return the heap to the OS so freed buffers are reused instead of re-faulted.
Trade-off is higher retained RSS. To bound it, a new AllocTrim() helper explicitly returns free memory to the OS and is called at quiescent teardown points (ClearStaticMapsAndVectors, ReleaseAllContexts).
Notes:
glibc-only; a no-op on other platforms.
AllocTrim() is a performance/RSS utility, not secure erasure — use secure_memset() to wipe sensitive data. It is not async-signal-safe.