Saving a table normally means converting it: a CSV is text, a pickle is Python's own format. Arrow's file format does neither. It writes the buffers as they sit in memory, so loading is just pointing at the bytes. A multi-gigabyte tick table can open in milliseconds because nothing is parsed, and several processes can map the same file and share one copy in the operating system's cache.
The idea in plain words
Where we are. Topic 5 shared memory between libraries inside one process. This topic extends the same idea to disk and to other processes: the IPC format is the buffers of topic 4 with a little framing around them. The Parquet skill covered the other file format, which compresses harder and is cheaper to store; IPC is the one to pick when speed to open matters most.
Asking the operating system to expose a file as if it were part of the program's memory. Pages load only when touched and are shared between processes, so a big file opens at once and costs no extra RAM of its own.
A file: save it, then read it mapped or not
write_feather writes the Feather version 2 file, which is simply an IPC file; lz4 and zstd compress each buffer and trade open speed for size. read_ipc_file(path, True) maps the file and builds the table on its pages, so the numbers below show no new allocation, while a normal read copies every byte into freshly allocated memory. A compressed file cannot be used in place: it must be decompressed first.
# Three Feather files of the same table: sizes by codecfor codec in ["uncompressed", "lz4", "zstd"]: print(f"{codec:12} {write_feather(table, codec + '.arrow', codec):>9,} bytes")
uncompressed 1,711,442 bytes lz4 1,111,738 bytes zstd 911,258 bytes
# Read the uncompressed file mapped and then normally, and watch Arrow's allocation counterbefore = pa.total_allocated_bytes()mapped = read_ipc_file("uncompressed.arrow", True)print("mapped: equal", mapped.equals(table), "| new allocation:", pa.total_allocated_bytes() - before, "bytes")before = pa.total_allocated_bytes()copied = read_ipc_file("uncompressed.arrow", False)print("copied: equal", copied.equals(table), "| new allocation:", f"{pa.total_allocated_bytes() - before:,}", "bytes")
mapped: equal True | new allocation: 0 bytes copied: equal True | new allocation: 1,710,592 bytes
A stream: batches over time
A file needs a footer, so it can only be finished once everything has been written. A stream has no footer: a schema, then batches in order, readable as they arrive. That is the shape for a socket or a pipe between two processes, or for a tick feed. count_stream_rows reads one batch at a time and never holds the whole stream, which is the point of the format.
# The bars as an IPC stream in batches of 10,000 rows, then read back one batch at a timewritten = write_stream(table, "bars.stream", 10000)batches, rows = count_stream_rows("bars.stream")print(f"{os.path.getsize('bars.stream'):,} bytes in the stream file | {written} batches written | read back: {batches} batches, {rows} rows")
1,710,872 bytes in the stream file | 5 batches written | read back: 5 batches, 33493 rows
IPC opens fastest and can be used in place, but its files are larger. Parquet is smaller and can skip data by column, row group and statistics, but it has to be decoded. Use IPC for hot data that many processes read on one machine, and for passing batches between processes; use Parquet for the long-term lake.
What the tasks ask
- 1`write_feather` and `read_ipc_file`
feather.write_feather(table, path, compression=...); read withipc.open_file(source).read_all(), wheresourceispa.memory_map(path, "r")or the path. - 2`write_stream` and `count_stream_rows`
ipc.new_stream(sink, schema)andwrite_batchfor eachto_batches(...)slice;ipc.open_stream(source)and iterate it.
Lab · what's graded
4 tasks · 100 pts- Save a Feather file:
write_feather25 pts - Read it, mapped or not:
read_ipc_file25 pts - Write a stream of batches:
write_stream25 pts - Read it batch by batch:
count_stream_rows25 pts