Checking how many agents are working

How this works
Contact
← Blog
A cinematic photograph of a long steel scroll partly unrolled across a dark bench with only the middle section lit in amber light, in teal and steel, the AMARBARO mark and wordmark centred over it.
✎blogbericht

Memory-mapped files: reading a huge file without loading it

The short version

Memory-mapping a file gives a program a zero-copy view of it that opens at once, and our libraries use that to list thousands of model tensors in milliseconds.

What this is about

Normally, to read a file you ask the system to copy its bytes into your program's memory. A big file means a big copy, and the copy takes time and space before you read your first byte.

A memory-mapped file works differently. You tell the system "let me see this file as if it were memory." The call returns at once. The system loads each piece of the file only when you touch it. Reading the first byte of a ten-gigabyte file takes the same time as reading it from a small file.

Why it matters

Model files are big. One of the models in our engine's table is 74.5 GB. If a program copied it all into memory just to look at its table of contents, you would wait, and you might run out of memory.

With mapping, a program can open the file, read the header that lists what is inside, and fetch only the parts it needs. The same trick helps with big logs, databases and any file you only need to dip into.

How it works

Your computer uses a trick called virtual memory. Each program sees a large, flat range of addresses, and the system decides which of those are backed by real memory. A mapped file is one more range that is backed by a file on disk.

  1. You ask for a map. The system sets up the address range. It does not read the file yet.
  2. You touch an address. The first time you read there, the system notices the page is missing and loads that piece, usually a few kilobytes, from disk.
  3. The system keeps pages it has loaded. If you read the same part again, it is already in memory. If the system needs room, it can drop pages you have not changed and reload them later.
  4. Writes can go back to the file. A writable map changes the file itself. A flush call tells the system to write the changed pages to disk now.

Notice what "zero copy" means here. Your program does not get a second copy of the bytes. It gets a window onto the pages the system already holds.

Let's try it in Mojo

Our mojo-mmap library gives you a file as a Span of bytes. Here is a program that makes a file of 1 MiB, writes one byte, flushes it and reads it back through a read-only map. We ran it on Mojo 1.2.0.

from mmap import MappedFile, create, SEQUENTIAL


def main() raises:
    var w = create("data.bin", 1 << 20)
    var b = w.mut_bytes()
    b[0] = 42
    w.flush()
    var m = MappedFile("data.bin")
    m.advise(SEQUENTIAL)
    print(len(m), m.bytes()[0])

It printed 1048576 42.

  • create("data.bin", 1 << 20) makes a file of 1,048,576 zero bytes, mapped for writing.
  • b[0] = 42 writes into the map. No write call is needed.
  • w.flush() waits until those pages are on disk.
  • MappedFile("data.bin") maps it read-only. m.advise(SEQUENTIAL) tells the system we plan to read from front to back, so it can read ahead.
  • m.bytes()[0] reads the first byte with no copy.

What the numbers say

The README read a 256 MiB file already in the system's cache. Summing every byte through the map ran at 24.9 GB per second. Reading the file into a buffer first, with read_bytes(), ran at 1.2 GB per second. This was measured on a Ryzen 7 7800X3D.

Be careful with that result. The file was warm in memory. A cold file on a slow disk will be limited by the disk. Mapping removes the copy. It does not make a disk faster.

Where we use it: model files

Language models ship as files of many tensors, which are big blocks of numbers. Our mojo-safetensors library reads and writes the safetensors format. It maps the file read-only and gives each tensor's data as a Span into the map, so nothing is copied. Its README times opening a file and listing every tensor, against the Python reference.

file mojo-safetensors reference (Python)
1,500 tensors (test fixture) 1 ms, 12 MB 163 ms, 31 MB
a deepseek-ocr-2 shard, 2,707 tensors 2 ms, 13 MB 505 ms, 32 MB
50,000 tensors 51 ms, 47 MB 164 s, 66 MB

This test measures opening and listing, not running a model. It shows that opening a file and reading its table of contents can cost milliseconds. It does not say how fast a model then runs.

Our mojo-gguf-core library does the same for GGUF, the format that llama.cpp reads: it reads files memory-mapped with zero copy.

What to watch out for

  • A map's size is fixed when you create it. If the file grows, you need a new map.
  • A map of an empty file raises an error, as Python's mmap does.
  • The Span is valid only while the MappedFile that made it is alive. Keep the map in scope.
  • Mapping works on Linux in our libraries, on x86-64 and aarch64.

Both libraries live in mojo-libs, under libs/mmap and libs/safetensors. The older repositories are read-only.

Words you may not know

  • Memory-mapped file: a file that your program reads as if it were memory.
  • Zero copy: getting access to data without making a second copy of it.
  • Page: a small block of memory, usually a few kilobytes, that the system loads as one unit.
  • Tensor: a block of numbers, such as the weights of one layer of a model.
  • Span: a window onto bytes that belong to something else.
  • Cache: memory the system uses to keep recently read data nearby.

Try it

  1. Run the program above and look at the 1 MiB file it makes.
  2. Read the mojo-mmap repository README, then the mojo-safetensors repository README.
  3. Find a safetensors file on your disk and list its tensors with the library.

Reacties

Nog geen reacties.

Inloggen om te reageren.