You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C++][Python] Option to read a memory-mapped IPC stream without MADV_WILLNEED #55656
MemoryMappedFile::ReadAt calls MemoryAdviseWillNeed on every range it returns (cpp/src/arrow/io/file.cc). With the IPC stream reader that's one call per message header and one per batch body, so reading a memory-mapped file with read_all() asks the kernel to prefetch the whole file, even when the caller only wants the zero-copy table and will touch a small part of it.
This is the case in Hugging Face datasets: load_from_disk and the dataset cache open each Arrow file with pa.ipc.open_stream(pa.memory_map(path)).read_all(), and the rows are only read later, often a few at a time. On a cold page cache the whole dataset is read from disk at load time (huggingface/datasets#2547). On a local xfs disk, with a 596 MiB dataset (2 files of 1000 batches), 100% of the files were read when opening it (1.23 s). Reading the same stream from a Python mmap advised MADV_RANDOM read 1.4% (0.13 s), but datasets can't use that as a fix because it also turns off readahead for the later reads.
strace -f -e trace=madvise python repro.py shows 2001 MADV_WILLNEED calls for this 1000-batch file.
This was discussed in #18920 / #11588 for random access in an IPC file. There, @pitrou suggested an option on the IPC reader to disable the advisory hints, and @westonpace suggested an auto_willneed flag on MemoryMappedFile::Open. The issue was closed as stale. Would one of these be acceptable now, with the default unchanged (prefetch on)? Either one would let datasets open memory-mapped files without reading them, and keep the normal readahead for later reads.
I would like to work on this. I read through this, and the issue that mentions this (#18920). I am thinking we could follow the approach suggested there: a per-file flag on the memory map, with the current behaviour kept as the default.
The idea is:
C++
Add MemoryMappedFile::set_auto_will_need(bool) and bool auto_will_need() const, defaulting to true.
In MemoryMappedFile::ReadAt, call MemoryAdviseWillNeed only when the flag is set. Read() goes through ReadAt, so it would be covered too.
Store the flag as an std::atomic<bool> on the shared MemoryMap, since ReadAt is documented as thread-safe.
Explicit WillNeed() calls would not be affected, and the Open/Create signatures would stay the same, so the change is purely additive.
Python
Add a keyword-only argument: pa.memory_map(path, mode='r', *, auto_will_need=True).
Add a MemoryMappedFile.auto_will_need property (getter and setter).
Then datasets, or any caller that wants lazy zero-copy access, could do:
with pa.memory_map(path, auto_will_need=False) as source:
table = pa.ipc.open_stream(source).read_all()
Two other options I considered:
An option on IpcReadOptions. This is more targeted, but the advice comes from the IO layer, so the flag would have to be passed down to the file anyway. It also wouldn't help non-IPC users of memory maps.
Turning the advice off by default. This is simpler, but it changes behaviour for people who currently benefit from the prefetch, so keeping it opt-in seems safer.
There, @pitrou suggested an option on the IPC reader to disable the advisory hints, and @westonpace suggested an auto_willneed flag on MemoryMappedFile::Open.
Either is ok to me (we can also do both if we want to give extra flexibility to the user).
@adiroy28852 Please read our guidelines around AI-generated contributions. They also apply to GitHub comments.
I understand.
Of the core idea, storing the flag in MemoryMap, keeping explicit WillNeed() calls unaffected, and MemoryMappedFile::set_auto_will_need(bool) came from me. I used LLMs to review the correctness of these points.
The MemoryAdviseWillNeed point, improvements in Python, the IpcReadOptions alternative and changing the default, were researched with LLMs.
Usage of LLMs will also be disclosed in the PR. I can debug and maintain the change myself.
Activity
adiroy28852 commentedon Nov 23, 2026
Hi @behroozazarkhalili
I would like to work on this. I read through this, and the issue that mentions this (#18920). I am thinking we could follow the approach suggested there: a per-file flag on the memory map, with the current behaviour kept as the default.
The idea is:
C++
MemoryMappedFile::set_auto_will_need(bool)andbool auto_will_need() const, defaulting totrue.MemoryMappedFile::ReadAt, callMemoryAdviseWillNeedonly when the flag is set.Read()goes throughReadAt, so it would be covered too.std::atomic<bool>on the sharedMemoryMap, sinceReadAtis documented as thread-safe.WillNeed()calls would not be affected, and theOpen/Createsignatures would stay the same, so the change is purely additive.Python
pa.memory_map(path, mode='r', *, auto_will_need=True).MemoryMappedFile.auto_will_needproperty (getter and setter).Then
datasets, or any caller that wants lazy zero-copy access, could do:Two other options I considered:
IpcReadOptions. This is more targeted, but the advice comes from the IO layer, so the flag would have to be passed down to the file anyway. It also wouldn't help non-IPC users of memory maps.I'll start working on an MR if they sound good.
pitrou commentedon Nov 23, 2026
@adiroy28852 Please read our guidelines around AI-generated contributions. They also apply to GitHub comments.
pitrou commentedon Nov 23, 2026
Either is ok to me (we can also do both if we want to give extra flexibility to the user).
adiroy28852 commentedon Nov 23, 2026
I understand.
Of the core idea, storing the flag in
MemoryMap, keeping explicitWillNeed()calls unaffected, andMemoryMappedFile::set_auto_will_need(bool)came from me. I used LLMs to review the correctness of these points.The
MemoryAdviseWillNeedpoint, improvements in Python, theIpcReadOptionsalternative and changing the default, were researched with LLMs.Usage of LLMs will also be disclosed in the PR. I can debug and maintain the change myself.