Skip to content

[C++][Python] Option to read a memory-mapped IPC stream without MADV_WILLNEED #55656

Open

Description

@behroozazarkhalili

Describe the enhancement requested

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.

Reproducer (pyarrow 25.0.1, Linux):

import os
import pyarrow as pa

path = "data.arrows"
batch = pa.record_batch({"x": pa.array(range(1000), pa.int64()), "s": pa.array(["a" * 32] * 1000)})
with pa.OSFile(path, "wb") as sink, pa.ipc.new_stream(sink, batch.schema) as writer:
    for _ in range(1000):
        writer.write_batch(batch)

with pa.memory_map(path) as source:
    table = pa.ipc.open_stream(source).read_all()

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.

Component(s)

C++, Python

Activity

  1. adiroy28852 commented on Nov 23, 2026

    @adiroy28852

    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++

    • 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:

    1. 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.
    2. 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.

    I'll start working on an MR if they sound good.

  2. pitrou commented on Nov 23, 2026

    @pitrou
    Member

    @adiroy28852 Please read our guidelines around AI-generated contributions. They also apply to GitHub comments.

  3. pitrou commented on Nov 23, 2026

    @pitrou
    Member

    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).

  4. adiroy28852 commented on Nov 23, 2026

    @adiroy28852

    @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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions