Skip to content

[CAS] Read files in CASProvidingFileSystem instead of mapping them - #237

Draft
tagolog wants to merge 1 commit into
swift/stable/23.xfrom
cas-read-not-mmap
Draft

tagolog wants to merge 1 commit into
swift/stable/23.xfrom
cas-read-not-mmap

Conversation

@tagolog

@tagolog tagolog commented Dec 1, 2026

Copy link
Copy Markdown

openCASBackedFileForRead asks for a buffer without a null terminator, so MemoryBuffer maps every file of 16 KB or more. The buffer is hashed once by storeFromString and dropped, so every page of the mapping faults in once and is unmapped right after. On Windows that is a fault per 4 KB page; on Linux fault-around maps up to 16 pages per fault.

Read files up to 1 MB instead. shouldUseMmap() skips mmap for a volatile file only when a null terminator is requested, so pass both flags. The terminator lies past the end of getBuffer() and does not change the stored object; the new test checks that the ref of a real file is the ref of its contents on both sides of the limit. Larger files stay mapped: a read buffer would commit their whole size, and on Linux mapping them is cheaper. The size comes from status() of the open file, which takes the place of the fstat MemoryBuffer makes itself.

Measured per file, from open to release: for files of 16 KB to 1 MB, reading is 22-52% faster than mapping on Windows. On Linux it is up to 17% faster for small files and about even (within 2%) at 512 KB-1 MB.

openCASBackedFileForRead asks for a buffer without a null terminator,
so MemoryBuffer maps every file of 16 KB or more. The buffer is hashed
once by storeFromString and dropped, so every page of the mapping
faults in once and is unmapped right after. On Windows that is a fault
per 4 KB page; on Linux fault-around maps up to 16 pages per fault.

Read files up to 1 MB instead. shouldUseMmap() skips mmap for a
volatile file only when a null terminator is requested, so pass both
flags. The terminator lies past the end of getBuffer() and does not
change the stored object; the new test checks that the ref of a real
file is the ref of its contents on both sides of the limit. Larger
files stay mapped: a read buffer would commit their whole size, and on
Linux mapping them is cheaper. The size comes from status() of the open
file, which takes the place of the fstat MemoryBuffer makes itself.

Measured per file, from open to release: for files of 16 KB to 1 MB,
reading is 22-52% faster than mapping on Windows. On Linux it is up to
17% faster for small files and about even (within 2%) at 512 KB-1 MB.
@tagolog
tagolog requested review from andreil99, asl and slydiman December 1, 2026 01:38
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

Hello @tagolog 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

@slydiman

slydiman commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Why 1MB?
I don't like this approach.

Maybe it is better just update shouldUseMmap() inside llvm/lib/Support/MemoryBuffer.cpp
It already contains the following

  // We don't use mmap for small files because this can severely fragment our
  // address space.
  if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
    return false;

We can define something like

#ifdef _WIN32
  if (MapSize < 1024 * 1024 || MapSize < (unsigned)PageSize)
#else
  if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
#endif
    return false;

or even

#ifdef _WIN32
  return false;
#endif

Are you sure we still have any advantages of using mmap in Windows for files larger than 1MB at all, not only for CAS hash?

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.

2 participants