Skip to content

pguillory/elixir-mmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mmap

This library allows you to mmap a file to an Elixir binary resource. It provides the same interface as File.read/1 (or File.read!/1) in the Elixir standard library. The value it returns looks like a regular binary, and you can treat it like a regular binary (using the String module for example). However unlike normal binaries, which are immutable in Elixir, it is actually using mmap to present a live view of the contents of the file.

:ok = File.write!("file.txt", "hello")
contents = Mmap.read!("file.txt")
assert contents == "hello"
:ok = File.write!("file.txt", "world")
assert contents == "world"

There are several potential advantages to using mmap over regular disk reads:

  1. Code (like parsers) designed to process data in memory can be used unmodified to process data on disk.

  2. Speed. Grabbing slices of a file using mmap and Kernel.binary_part is often much faster than something like :file.pread.

  3. You can process a large file without reading the entire thing into memory at once.

Under the hood, the file handle is held open until the binary resource is garbage collected, at which point it is automatically munmap-ed and closed.

Note that this is a read-only view of the file. It's simply a result of the choice to represent the file contents as a binary. Elixir/Erlang have no functionality for writing to a binary, because they are typically immutable.

Installation

def deps do
  [
    {:mmap, "~> 0.1.0"}
  ]
end

About

Replacement for File.read using mmap

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published