Sub topic 10
Memory-Mapped Reads of Arrow Files
This chapter assumes you can write and read Arrow IPC files with pyarrow, and that you know the difference between memory held by a process and data stored in a file on disk.
What a memory map does
A memory map asks the operating system to expose a file as a range of virtual memory. The process reads the file as if it were an ordinary byte array. The OS loads pages from disk only when the code touches them, and it can drop those pages later without writing anything back.
Arrow's IPC file format suits this well. The file stores each column's buffers in the same layout they have in memory, with padding to keep them aligned. When pyarrow reads a memory-mapped file, the resulting arrays can point straight into the mapped region. No decoding step runs, and no new memory is allocated for the column data.
Writing and reading a mapped file
The following code writes a small table to an IPC file and reads it back through a memory map.
import pyarrow as pa
table = pa.table({"id": [1, 2, 3], "name": ["a", "b", "c"]})
with pa.OSFile("example.arrow", "wb") as sink:
with pa.ipc.new_file(sink, table.schema) as writer:
writer.write_table(table)
with pa.memory_map("example.arrow", "r") as source:
mapped = pa.ipc.open_file(source).read_all()
print(mapped.num_rows)pa.memory_map returns a MemoryMappedFile. pa.ipc.open_file reads the footer and schema, and read_all builds a table whose buffers reference the mapping. Only the metadata is parsed. The values are not copied.
Why it helps
- Fast startup. Opening a large file takes about as long as reading its footer, because no column data is read up front.
- Lower memory use. Pages are loaded on demand, so a table larger than free RAM can still be opened. Untouched columns never leave the disk.
- Sharing between processes. Several processes that map the same file share the same physical pages in the OS cache, so each one does not hold its own copy.
Common errors and limits
The mapping must outlive the table. The arrays borrow the mapped memory. In pyarrow, the table keeps a reference to the buffers, so the buffers stay valid while you hold the table. Problems appear when you pass raw pointers to other code that does not hold that reference.
Only the IPC file format maps well. The stream format is meant for sequential reading. Parquet is encoded and compressed, so reading it always decodes into new memory. Compressed IPC buffers also have to be decompressed into new memory, which removes the benefit. Write uncompressed IPC files if you plan to map them.
First access can still be slow. A mapped read is lazy, not free. The first touch of a page triggers a disk read. On a slow or network file system, scans can stall unpredictably.
Do not change the file while it is mapped. If another process rewrites or truncates the file, your table may show changed values or the process may crash. Treat mapped files as read-only and replace them by writing a new file and renaming it.
Conversions can copy. Calling to_pandas() on a mapped table usually copies the data into NumPy-owned memory. Strings and columns with nulls need conversion anyway. Pass zero_copy_only=True to to_numpy on a single array if you want an error instead of a silent copy.
Exercise
You store a 50 GB Arrow dataset as a compressed IPC file and open it with pa.memory_map. Memory use grows far more than you expected as you scan it. Why, and what would you change?
Show answer
Compressed IPC buffers cannot be used in place. Arrow must decompress each buffer into newly allocated memory, so the zero-copy benefit is lost. Write the file without compression, so the buffers in the file match the in-memory layout and the arrays can point into the mapping.