Skip to content

Improve memory mapping performance on POSIX [GH #648] - #649

Open
wchilders-nvidia wants to merge 1 commit into
mainfrom
fix/648
Open

wchilders-nvidia wants to merge 1 commit into
mainfrom
fix/648

Conversation

@wchilders-nvidia

@wchilders-nvidia wchilders-nvidia commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Use anonymous memory-mappings on Linux and BSD to avoid temporary backing-file creation overhead during incremental growth of the heap.

Summary by CodeRabbit

  • Improvements
    • On Linux and BSD, memory-region allocations that use memory mapping now use anonymous mappings when supported, instead of temporary backing files. This includes memory used during precompiled-header creation and use when memory mapping is enabled.
    • Systems without anonymous-mapping support continue to use file-backed mappings. Input-file mappings remain file-backed.

@wchilders-nvidia wchilders-nvidia added this to the 7.1 milestone Dec 3, 2026
@wchilders-nvidia
wchilders-nvidia requested a review from a team December 3, 2026 17:53
@wchilders-nvidia wchilders-nvidia added the enhancement New feature or request label Dec 3, 2026
@wchilders-nvidia wchilders-nvidia linked an issue Dec 3, 2026 that may be closed by this pull request
1 task done
@wchilders-nvidia wchilders-nvidia removed this from the 7.1 milestone Dec 3, 2026
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

POSIX memory-region expansion now uses anonymous mappings when available. Otherwise, the code extends and flushes a backing file before mapping. Input-file mappings remain file-backed.

Changes: POSIX memory mapping

Layer / File(s) Summary
Mapping source representation and calls
src/host_envir.c
A mapping-source representation distinguishes anonymous and file-backed mappings. mmap_posix uses the source descriptor, offset, and flags. Input-file mapping passes a file-backed source.
Memory-region expansion and fallback
src/host_envir.c, src/mem_manage.c, src/pending_changes/gh-648-anonymous-mmap.txt
Memory-region expansion uses anonymous mappings when available. Otherwise, it extends and flushes the backing file before mapping. Temporary-file handling and memory-management comments reflect the optional backing file. The change note records the change.

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant MemoryRegion
  participant mmap_posix
  participant POSIX
  MemoryRegion->>mmap_posix: provide anonymous or file-backed source
  mmap_posix->>POSIX: request mapping with source descriptor, offset, and flags
Loading

Possibly related issues

Suggested reviewers: darkarc

Merge Risk: Minimal · up to b7693

This change switches POSIX memory-region growth to anonymous mappings where the platform supports them, which avoids creating temporary backing files. No concrete defect was found, and the file-backed path is kept when anonymous mappings are unavailable. The merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main goal (better memory mapping performance on POSIX) and the linked issue GH #648. The change set matches it: on Linux and BSD, memory regions now use anonymous MAP_PRIVATE mappi…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main goal (better memory mapping performance on POSIX) and the linked issue GH #648. The change set matches it: on Linux and BSD, memory regions now use anonymous MAP_PRIVATE mappings instead of temporary backing files. The title does not say how the speedup is achieved, but it refers to the primary change and is not misleading.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 70bde59 and b769339.

Files selected for processing: 3

  • src/host_envir.c
  • src/mem_manage.c
  • src/pending_changes/gh-648-anonymous-mmap.txt
Files skipped from review as they are similar to previous changes: 1

  • src/mem_manage.c

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@Michael137 Michael137 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks!

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 73b88247e0b2:

CI success.

@daveedvdv-nvidia daveedvdv-nvidia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks right to me: The temporary file never contributed anything but its size (the mappings are MAP_PRIVATE, so the pages written never go back to it), and nothing reads it back: When a PCH is created, write_memory_used_for_memory_regions writes the mapped memory itself, and when one is used, the PCH file is mapped rather than the temporary file. close_memory_region_tmp_file already handles a NULL handle.

I checked this with release builds of this PR and of its base, creating a PCH from a generated header with 2000 classes and using it. (Using it required disabling ASLR, with and without this change; otherwise there is a memory usage conflict.) The generated C is the same with and without this change, both when creating and when using the PCH. Creating a 138 MB PCH went from 0.63–0.67 s to 0.53–0.58 s.

A few small things are inline. The one I'd most like to see fixed is the scope of the Changes entry: USE_MMAP_FOR_MEMORY_REGIONS makes the mapping code available, but it is only used when precompiled_header_processing_required is set, i.e., with --create_pch, --use_pch, or --pch. Otherwise, alloc_mem_block gets its blocks from malloc, and map_memory_region_file is never called. So this only speeds up compilations that create or use a PCH.

Comment thread src/host_envir.c Resolved
Comment thread src/host_envir.c Resolved
Comment thread src/host_envir.c Resolved
Comment thread src/host_envir.c Resolved
Comment thread src/host_envir.c Resolved
@wchilders-nvidia
wchilders-nvidia force-pushed the fix/648 branch 2 times, most recently from 34d68c5 to 70bde59 Compare December 3, 2026 21:41
Use anonymous memory-mappings on Linux and BSD to avoid temporary backing-file
creation overhead during incremental growth of the heap.

@daveedvdv-nvidia daveedvdv-nvidia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — it all looks good now.

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for b769339f0880:

CI success.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve MMAP Based Allocation

4 participants