Skip to content

Retain large allocations on the heap by default#1213

Merged
yspolyakov merged 3 commits into
devfrom
issue1212
Jul 2, 2026
Merged

Retain large allocations on the heap by default#1213
yspolyakov merged 3 commits into
devfrom
issue1212

Conversation

@pascoec

@pascoec pascoec commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

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.

@pascoec pascoec added this to the Release 1.6.0 milestone Jun 30, 2026
@pascoec pascoec self-assigned this Jun 30, 2026
@pascoec pascoec added the optimization Improves performance label Jun 30, 2026
@pascoec pascoec requested a review from yspolyakov June 30, 2026 19:59
@yspolyakov

Copy link
Copy Markdown
Contributor

Please update Best_Performance.md to discuss the usage of AllocTrim().

@pascoec pascoec linked an issue Jul 2, 2026 that may be closed by this pull request
Clarify memory allocation behavior and performance impact.
@yspolyakov yspolyakov merged commit ce642b3 into dev Jul 2, 2026
@pascoec pascoec deleted the issue1212 branch July 8, 2026 17:32
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimization Improves performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add mallopt static initializer to reduce the number of page faults

2 participants