Skip unavailable malloc_trim offloads - #1788
Merged
xeophon merged 1 commit intoJun 21, 2026
Merged
Conversation
Contributor
ApprovabilityVerdict: Approved Simple optimization that adds an early return to skip scheduling a thread call when malloc_trim is unavailable. The change is minimal, self-contained, and doesn't alter meaningful runtime behavior. You can customize Macroscope's approvability policy. Learn more. |
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.
Overview
Stop scheduling recurring thread-pool work after a worker has definitively established that
malloc_trimis unavailable. The first probe and the Linux trimming cadence remain unchanged.Reasoning
_malloc_trimis a process-global tri-state: unresolved (None), available (the libc function), or unavailable (False). Previously, the periodic helper advanced its 16-rollout cadence in all three states. On unsupported platforms, every sixteenth completed rollout therefore submittedtrim_memorythroughasyncio.to_thread, even though the cachedFalsemade the worker function return without doing anything.Those no-op offloads still copy the current context, allocate and queue executor work, wake a worker thread, signal completion across threads, and suspend/resume the rollout coroutine. An exact
is Falseguard before cadence accounting removes that work only after the probe is definitive. The unresolved path still performs its first probe in a worker thread, while a callablemalloc_trimstill runsmalloc_trim(0)every 16 rollouts off the event loop.Performance impact
A standalone PEP 723 benchmark exercised 100,000 completed-rollout helper calls per repetition, using seven repetitions and the median:
This is a CPU and scheduling improvement rather than an RSS reduction. It is most relevant to long-running workers on macOS or other platforms without glibc
malloc_trim.Note
Low Risk
Single guard on a best-effort memory helper; behavior on glibc/Linux is unchanged after the first successful probe.
Overview
Adds an early return in
trim_memory_periodicallywhen the cached probe showsmalloc_trimis unavailable (_malloc_trim is False).Previously, every 16th finished rollout still scheduled
trim_memoryviaasyncio.to_threadeven though that call was a no-op off glibc—wasting executor work and context switches on macOS and similar workers. The first probe and the every-16-rollouts trim on Linux are unchanged.Reviewed by Cursor Bugbot for commit 6ea1b89. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Skip
trim_memory_periodicallywork whenmalloc_trimis unavailableAdds an early return in
trim_memory_periodicallyin memory.py when_malloc_trimisFalse. Previously, the function would still increment_rollouts_since_trimand potentially scheduletrim_memoryviaasyncio.to_threadeven when trimming was unavailable.Macroscope summarized 6ea1b89.