What is a Memory-Mapped File in Python

From Python's official documentation, be sure to checkout Python's mmap module:

A memory-mapped file object behaves like both strings and like file objects. Unlike normal string objects, however, these are mutable.

Basically, a memory-mapped (using Python's mmap module) file object maps a normal file object into memory. This allows you to modify a file object's content directly in memory. Since a memory-mapped file object also behaves like a mutable string object, you can modify the content of a file object like you modify the content of a list of characters:

  • obj[1] = 'a' - Assigns a character 'a' to the the second character of the file object's content.
  • obj[1:4] = 'abc' - Assigns a character list 'abc' to a range of three characters starting at the second character of the file object's content.

In a nutshell, memory-mapping a file with Python's mmap module us use the operating system's virtual memory to access the data on the filesystem directly. Instead of making system calls such as open, read and lseek to manipulate a file, memory-mapping puts the data of the file into memory which allows you to directly manipulate files in memory. This greatly improves I/O performance.

Comparison of Memory-Mapped Files vs Normal Files with Python

Assume we have a binary file test.out that is larger than 10 MB and there's a certain kind of algorithm that requires us to process the data of the file in such a manner that needs us to repeat the process of:

  • From the current position, seek 64 bytes and process the data at the beginning of current position.
  • From the current position, seek -32 bytes and process the data at the beginning of the current position.

The actual process of the data is replaced by a pass statement since it does not affect the relative performance comparison between mmap and normal file access. The algorithm keeps processing the data of the file until it reaches a point that is larger than 10MB. The code that uses the normal file objects to perform the algorithm is listed in the file normal_process.py:

The code that uses mmap to process the data of the file is listed in the file mmap_process.py:

Now you can compare normal_process.py and mmap_process.py in a simple for-loop in your shell:

As you can see, mmap_process.py is about 17% faster than normal_process.py on average, because the seek function calls are performed directly against the memory in mmap_process.py while they are performed using filesystem calls in normal_process.py.

Modify a Memory-Mapped File in Python with mmap

In the following file mmap_write.py, we modify the content of a file write_test.txt using mmap and flush the changes back to disk:

Suppose write_test.txt contains one line of text:

After we run python mmap_write.py, its content will change to (note the first character of the sentence):

mmap Summary and Suggestions

Although mmap is a cool feature, keep in mind that mmap has to find a contiguous block of addresses in your process's address space that is large enough to fit an entire file object. Suppose you're working with large files on a system that does not have enough continuous memory region available to fit those files, then creating a mmap will fail. In additions, mmap does not work on certain special file objects like pipes and ttys.

The following list summarizes when you should use mmap:

  • During multithreaded programming, if you have multiple processes accessing data in a read only way from the same file, then using mmap can save you lots of memory.
  • mmap allows the operating system to optimize paging operations, which enables the program's memory in pages to be efficiently re-used by the operating system.

About The Author