[PATCH 2 of 3] util: add a mmapread method

Jun Wu quark at fb.com
Tue Oct 4 12:49:59 EDT 2016


Excerpts from Gregory Szorc's message of 2016-10-04 09:10:26 -0700:
> On Tue, Oct 4, 2016 at 7:59 AM, Jun Wu <quark at fb.com> wrote:
> 
> > # HG changeset patch
> > # User Jun Wu <quark at fb.com>
> > # Date 1475580982 -3600
> > #      Tue Oct 04 12:36:22 2016 +0100
> > # Node ID 5ebdfd4ffbedbfe66b7a36cbd06b1e8e624ae7ad
> > # Parent  663bae87a82f1468faf979bacfec30a426bebae1
> > # Available At https://bitbucket.org/quark-zju/hg-draft 
> > #              hg pull https://bitbucket.org/quark-zju/hg-draft  -r
> > 5ebdfd4ffbed
> > util: add a mmapread method
> >
> > This method uses mmap to read file contents. It's useful for big files.
> >
> > diff --git a/mercurial/util.py b/mercurial/util.py
> > --- a/mercurial/util.py
> > +++ b/mercurial/util.py
> > @@ -24,4 +24,5 @@ import gc
> >  import hashlib
> >  import imp
> > +import mmap
> >  import os
> >  import re as remod
> > @@ -338,4 +339,10 @@ class bufferedinputpipe(object):
> >              self._buffer.append(data)
> >
> > +def mmapread(fp):
> > +    try:
> > +        return mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ)
> > +    except ValueError: # cannot mmap an empty file
> > +        return ''
> > +
> >  def popen2(cmd, env=None, newlines=False):
> >      # Setting bufsize to -1 lets the system decide the buffer size.
> >
> 
> The 3rd argument to mmap.mmap() varies between Windows and non-Windows
> Python implementations. You'll need to take this into consideration, likely
> by moving this utility function to the platform layer.

This has already been taken care of by the library:

> For both the Unix and Windows versions of the constructor, access may be
> specified as an optional keyword parameter. access accepts one of three
> values: ACCESS_READ, ACCESS_WRITE, or ACCESS_COPY to specify read-only,
> write-through or copy-on-write memory respectively.

The platform-specific way would be "prot=mmap.PROT_READ".


More information about the Mercurial-devel mailing list