Is there any way to get regexes to work on non-string/unicode objects. I would like to split large files by regex and it seems relatively hard to do so without having the whole file in memory. Even with buffers it seems hard to get regexes to indicate that they failed because of buffer termination and getting a partial match to be resumable seems out of the question.
What interface does re actually need for its src objects? -- Robin Becker
Map the file into RAM by using the mmap module. The file's contents than is availabel as a seachable string.
HTH, Gerald
Robin Becker schrieb:
> Is there any way to get regexes to work on non-string/unicode objects. I > would like to split large files by regex and it seems relatively hard to > do so without having the whole file in memory. Even with buffers it > seems hard to get regexes to indicate that they failed because of buffer > termination and getting a partial match to be resumable seems out of the > question.
> What interface does re actually need for its src objects?
Gerald Klix wrote: > Map the file into RAM by using the mmap module. > The file's contents than is availabel as a seachable string.
that's a good idea, but I wonder if it actually saves on memory? I just tried regexing through a 25Mb file and end up with 40Mb as working set (it rose linearly as the loop progessed through the file). Am I actually saving anything by not letting normal vm do its thing?
> HTH, > Gerald
> Robin Becker schrieb:
>> Is there any way to get regexes to work on non-string/unicode objects. >> I would like to split large files by regex and it seems relatively >> hard to do so without having the whole file in memory. Even with >> buffers it seems hard to get regexes to indicate that they failed >> because of buffer termination and getting a partial match to be >> resumable seems out of the question.
>> What interface does re actually need for its src objects?
> Gerald Klix wrote: > > Map the file into RAM by using the mmap module. > > The file's contents than is availabel as a seachable string.
> that's a good idea, but I wonder if it actually saves on memory? I just tried > regexing through a 25Mb file and end up with 40Mb as working set (it rose > linearly as the loop progessed through the file). Am I actually saving anything > by not letting normal vm do its thing?
You aren't saving memory in that sense, no. If you have any RAM spare the file will end up in it. However, if you are short on memory though, mmaping the file gives the VM the opportunity to discard pages from the file, instead of paging them out. Try again with a 25Gb file and watch the difference ;) YMMV.
>>>Map the file into RAM by using the mmap module. >>>The file's contents than is availabel as a seachable string.
>>that's a good idea, but I wonder if it actually saves on memory? I just tried >>regexing through a 25Mb file and end up with 40Mb as working set (it rose >>linearly as the loop progessed through the file). Am I actually saving anything >>by not letting normal vm do its thing?
> You aren't saving memory in that sense, no. If you have any RAM spare the > file will end up in it. However, if you are short on memory though, mmaping the > file gives the VM the opportunity to discard pages from the file, instead of paging > them out. Try again with a 25Gb file and watch the difference ;) YMMV.
:)
So we avoid dirty page writes etc etc. However, I still think I could get away with a small window into the file which would be more efficient. -- Robin Becker
>>>> Map the file into RAM by using the mmap module. >>>> The file's contents than is availabel as a seachable string.
>>> that's a good idea, but I wonder if it actually saves on memory? I >>> just tried >>> regexing through a 25Mb file and end up with 40Mb as working set (it >>> rose >>> linearly as the loop progessed through the file). Am I actually >>> saving anything >>> by not letting normal vm do its thing?
>> You aren't saving memory in that sense, no. If you have any RAM spare the >> file will end up in it. However, if you are short on memory though, >> mmaping the >> file gives the VM the opportunity to discard pages from the file, >> instead of paging >> them out. Try again with a 25Gb file and watch the difference ;) YMMV.
> :)
> So we avoid dirty page writes etc etc. However, I still think I could > get away with a small window into the file which would be more efficient.
I seem to remember that the Medusa code contains a fairly good overlapped search for a terminator string, if you want to chunk the file.
Take a look at the handle_read() method of class async_chat in the standard library's asynchat.py.
> You should be able to adapt the logic fairly easily, I hope.
....
The buffering logic is half the problem; doing it quickly is the other half. The third half of the problem is getting re to co-operate and probably halves 4 5..... :) -- Robin Becker
Robin> So we avoid dirty page writes etc etc. However, I still think I Robin> could get away with a small window into the file which would be Robin> more efficient.
It's hard to imagine how sliding a small window onto a file within Python would be more efficient than the operating system's paging system. ;-)
Skip Montanaro wrote: > Robin> So we avoid dirty page writes etc etc. However, I still think I > Robin> could get away with a small window into the file which would be > Robin> more efficient.
> It's hard to imagine how sliding a small window onto a file within Python > would be more efficient than the operating system's paging system. ;-)
> Skip
well it might be if I only want to scan forward through the file (think lexical analysis). Most lexical analyzers use a buffer and produce a stream of tokens ie a compressed version of the input. There are problems crossing buffers etc, but we never normally need the whole file in memory.
If the lexical analyzer reads the whole file into memory then we need more pages. The mmap thing might help as we need only read pages (for a lexical scanner).
Scanners work by detecting the transitions between tokens so even if the tokens are very long we don't need to store them twice (in the input stream and token accumulator); I suppose that could be true of regex pattern matchers, but it doesn't seem to be for re ie we need the entire pattern in the input before we can match and extract to an accumulator. -- Robin Becker
On Tue, 26 Apr 2005 19:32:29 +0100, Robin Becker wrote: > Skip Montanaro wrote: >> Robin> So we avoid dirty page writes etc etc. However, I still think I >> Robin> could get away with a small window into the file which would be >> Robin> more efficient.
>> It's hard to imagine how sliding a small window onto a file within Python >> would be more efficient than the operating system's paging system. ;-)
>> Skip > well it might be if I only want to scan forward through the file (think lexical > analysis). Most lexical analyzers use a buffer and produce a stream of tokens ie > a compressed version of the input. There are problems crossing buffers etc, but > we never normally need the whole file in memory.
I think you might have a misunderstanding here. mmap puts a file into *virtual* memory. It does *not* read the whole thing into physical memory; if it did, there would be no purpose to mmap support in the OS in the first place, as a thin wrapper around existing file calls would work.
> If the lexical analyzer reads the whole file into memory then we need more > pages. The mmap thing might help as we need only read pages (for a lexical scanner).
The read-write status of the pages is not why mmap is an advantage; the advantage is that the OS naturally and transparent is taking care of loading just the portions you want, and intelligently discarding them when you are done (more intelligently than you could, even in theory, since it can take advantage of knowing the entire state of the system, your program can't).
In other words, as Skip was trying to tell you, mmap *already does* what you are saying might be better, and it does it better than you can, even in theory, from inside a process (as the OS will not reveal to you the data structures it has that you would need to match that performance).
As you try to understand mmap, make sure your mental model can take into account the fact that it is easy and quite common to mmap a file several times larger than your physical memory, and it does not even *try* to read the whole thing in at any given time. You may benefit from reviewing/studying the difference between virtual memory and physical memory.
>> It's hard to imagine how sliding a small window onto a file within Python >> would be more efficient than the operating system's paging system. ;-)
Robin> well it might be if I only want to scan forward through the file Robin> (think lexical analysis). Most lexical analyzers use a buffer and Robin> produce a stream of tokens ie a compressed version of the Robin> input. There are problems crossing buffers etc, but we never Robin> normally need the whole file in memory.
If I mmap() a file, it's not slurped into main memory immediately, though as you pointed out, it's charged to my process's virtual memory. As I access bits of the file's contents, it will page in only what's necessary. If I mmap() a huge file, then print out a few bytes from the middle, only the page containing the interesting bytes is actually copied into physical memory.
> If I mmap() a file, it's not slurped into main memory immediately, though as > you pointed out, it's charged to my process's virtual memory. As I access > bits of the file's contents, it will page in only what's necessary. If I > mmap() a huge file, then print out a few bytes from the middle, only the > page containing the interesting bytes is actually copied into physical > memory.
.... my simple rather stupid experiment indicates that windows mmap at least will reserve 25Mb of paged file for a linear scan through a 25Mb file. I probably only need 4096b to scan. That's a lot less than even the page table requirement. This isn't rocket science just an old style observation. -- Robin Becker
On Tue, 26 Apr 2005 20:54:53 +0000, Robin Becker wrote: > Skip Montanaro wrote: > ... >> If I mmap() a file, it's not slurped into main memory immediately, though as >> you pointed out, it's charged to my process's virtual memory. As I access >> bits of the file's contents, it will page in only what's necessary. If I >> mmap() a huge file, then print out a few bytes from the middle, only the >> page containing the interesting bytes is actually copied into physical >> memory. > .... > my simple rather stupid experiment indicates that windows mmap at least > will reserve 25Mb of paged file for a linear scan through a 25Mb file. I > probably only need 4096b to scan. That's a lot less than even the page > table requirement. This isn't rocket science just an old style observation.
Are you trying to claim Skip is wrong, or what? There's little value in saying that by mapping a file of 25MB into VM pages, you've increased your allocated paged file space by 25MB. That's effectively tautological.
If you are trying to claim Skip is wrong, you *do not understand* what you are talking about. Talk less, listen and study more. (This is my best guess, as like I said, observing that allocating things increases the number of things that are allocated isn't worth posting so my thought is you think you are proving something. If you really are just posting something tautological, my apologies and disregard this paragraph but, well, it's certainly not out of line at this point.)
On Mon, 25 Apr 2005 16:01:45 +0100, Robin Becker <ro...@reportlab.com> wrote: >Is there any way to get regexes to work on non-string/unicode objects. I would >like to split large files by regex and it seems relatively hard to do so without >having the whole file in memory. Even with buffers it seems hard to get regexes >to indicate that they failed because of buffer termination and getting a partial >match to be resumable seems out of the question.
>What interface does re actually need for its src objects?
ISTM splitting is a special situation where you can easily chunk through a file and split as you go, since if splitting the current chunk succeeds, you can be sure that all but the tail piece is valid[1]. So you can make an iterator that yields all but the last and then sets the buffer to last+newchunk and goes on until there are no more chunks, and the tail part will be a valid split piece. E.g., (not tested beyond what you see ;-)
>>> def frxsplit(path, rxo, chunksize=8192): ... buffer = '' ... for chunk in iter((lambda f=open(path): f.read(chunksize)),''): ... buffer += chunk ... pieces = rxo.split(buffer) ... for piece in pieces[:-1]: yield piece ... buffer = pieces[-1] ... yield buffer ... >>> import re >>> rxo = re.compile('XXXXX')
The test file:
>>> print '----\n%s----'%open('tsplit.txt').read() ---- This is going to be split on five X's like XXXXX but we will use a buffer of XXXXX length 2 to force buffer appending. We'll try a splitter at the end: XXXXX ----
>>> for piece in frxsplit('tsplit.txt', rxo, 2): print repr(piece) ... "This is going to be split on five X's\nlike " ' but we will use a buffer of\n' " length 2 to force buffer appending.\nWe'll try a splitter at the end: " '\n'
>>> rxo = re.compile('(XXXXX)') >>> for piece in frxsplit('tsplit.txt', rxo, 2): print repr(piece) ... "This is going to be split on five X's\nlike " 'XXXXX' ' but we will use a buffer of\n' 'XXXXX' " length 2 to force buffer appending.\nWe'll try a splitter at the end: " 'XXXXX' '\n'
[1] In some cases of regexes with lookahead context, you might have to check that the last piece not only exists but exceeds max lookahead length, in case there is a <withlookahead>|<plain> kind of thing in the regex where <lookahead> would have succeeded with another chunk appended to buffer, but <plain> did the split.
Jeremy Bowers wrote: > On Tue, 26 Apr 2005 20:54:53 +0000, Robin Becker wrote:
>>Skip Montanaro wrote: >>...
>>>If I mmap() a file, it's not slurped into main memory immediately, though as >>>you pointed out, it's charged to my process's virtual memory. As I access >>>bits of the file's contents, it will page in only what's necessary. If I >>>mmap() a huge file, then print out a few bytes from the middle, only the >>>page containing the interesting bytes is actually copied into physical >>>memory.
>>.... >>my simple rather stupid experiment indicates that windows mmap at least >>will reserve 25Mb of paged file for a linear scan through a 25Mb file. I >>probably only need 4096b to scan. That's a lot less than even the page >>table requirement. This isn't rocket science just an old style observation.
> Are you trying to claim Skip is wrong, or what? There's little value in > saying that by mapping a file of 25MB into VM pages, you've increased your > allocated paged file space by 25MB. That's effectively tautological.
> If you are trying to claim Skip is wrong, you *do not understand* what you > are talking about. Talk less, listen and study more. (This is my best > guess, as like I said, observing that allocating things increases the > number of things that are allocated isn't worth posting so my thought is > you think you are proving something. If you really are just posting > something tautological, my apologies and disregard this paragraph but, > well, it's certainly not out of line at this point.)
Well I obviously don't understand so perhaps you can explain these results
I implemented a simple scanning algorithm in two ways. First buffered scan tscan0.py; second mmapped scan tscan1.py.
for large file sizes when paging becomes of interest buffered scan wins even though it has to do a lot more python statements. If this were coded in C the results would be plainer still. As I said this isn't about right or wrong it's an observation. If I inspect the performance monitor tscan0 is at 100%, but tscan1 is at 80-90% and all of memory gets used up so paging is important. This may be an effect of the poor design of xp if so perhaps it won't hold for other os's.
C:\code\reportlab\demos\gadflypaper>cat \tmp\tscan0.py import sys, time fn = sys.argv[1] f=open(fn,'rb') n=0 w=0 t0 = time.time() while 1: buf = f.read(4096) lb = len(buf) if not lb: break n += lb for i in xrange(lb): w ^= ord(buf[i]) t1 = time.time()
print "len=%d w=%d time=%.2f" % (n, w, (t1-t0))
C:\code\reportlab\demos\gadflypaper>cat \tmp\tscan1.py import sys, time, mmap, os fn = sys.argv[1] fh=os.open(fn,os.O_BINARY|os.O_RDONLY) s=mmap.mmap(fh,0,access=mmap.ACCESS_READ) n=len(s) w=0 t0 = time.time() for i in xrange(n): w ^= ord(s[i]) t1 = time.time()
I'm not sure why the mmap() solution is so much slower for you. Perhaps on some systems files opened for reading are mmap'd under the covers. I'm sure it's highly platform-dependent. (My results on MacOSX - see below - are somewhat better.)
Let me return to your original problem though, doing regex operations on files. I modified your two scripts slightly:
tscan0.py:
import sys, time, re fn = sys.argv[1] f=open(fn,'rb') n=0 t0 = time.time() while 1: buf = f.read(4096) if not buf: break for i in re.split("XXXXX", buf): n += 1 t1 = time.time()
print "n=%d time=%.2f" % (n, (t1-t0))
tscan1.py:
import sys, time, mmap, os, re fn = sys.argv[1] fh=os.open(fn,os.O_RDONLY) s=mmap.mmap(fh,0,access=mmap.ACCESS_READ) t0 = time.time() n = 0 for mat in re.split("XXXXX", s): n += 1 t1 = time.time()
print "n=%d time=%.2f" % (n, (t1-t0))
The mmap version is almost obviously correct, assuming what we want to do is split the file on "XXXXX". The buffered read version is almost certainly incorrect, given our understanding that corner cases lurk at buffer boundaries.
I took the file from Bengt Richter's example and replicated it a bunch of times to get a 122MB file. I then ran the above two programs against it:
So the mmap'd version is within 15% of the performance of the buffered read version and we don't have to solve the problem of any corner cases (note the different values of n). I'm happy to take the extra runtime in exchange for simpler code.
>I'm not sure why the mmap() solution is so much slower for you. Perhaps on >some systems files opened for reading are mmap'd under the covers. I'm sure >it's highly platform-dependent. (My results on MacOSX - see below - are >somewhat better.)
>Let me return to your original problem though, doing regex operations on >files. I modified your two scripts slightly:
>tscan0.py:
> import sys, time, re > fn = sys.argv[1] > f=open(fn,'rb') > n=0 > t0 = time.time() > while 1: > buf = f.read(4096) > if not buf: break > for i in re.split("XXXXX", buf):
To be fairer, I think you'd want to hoist the re compilation out of the loop. But also to be fairer, maybe include the overhead of splitting correctly, at least for the simple case regex in my example -- or is a you-goofed post for me in the usenet forwarding queues somewhere still? ;-)
> import sys, time, mmap, os, re > fn = sys.argv[1] > fh=os.open(fn,os.O_RDONLY) > s=mmap.mmap(fh,0,access=mmap.ACCESS_READ) > t0 = time.time() > n = 0 > for mat in re.split("XXXXX", s): > n += 1 > t1 = time.time()
> print "n=%d time=%.2f" % (n, (t1-t0))
>The mmap version is almost obviously correct, assuming what we want to do is >split the file on "XXXXX". The buffered read version is almost certainly >incorrect, given our understanding that corner cases lurk at buffer >boundaries. >I took the file from Bengt Richter's example and replicated it a bunch of >times to get a 122MB file. I then ran the above two programs against it:
>So the mmap'd version is within 15% of the performance of the buffered read
with regex recompilation in loop ;-)
>version and we don't have to solve the problem of any corner cases (note the >different values of n). I'm happy to take the extra runtime in exchange for >simpler code.
Agree. Hm, I wonder if the OS notices sequential page faults and schedules speculative read-ahead. Hm2, I wonder if you can just touch bytes from another coordinated thread to cause that, if it isn't happening ;-) Not for 15% though ;-)
> I'm not sure why the mmap() solution is so much slower for you. Perhaps on > some systems files opened for reading are mmap'd under the covers. I'm sure > it's highly platform-dependent. (My results on MacOSX - see below - are > somewhat better.)
I'll have a go at doing the experiment on some other platforms I have available. The problem is certainly paging related. Perhaps the fact that we don't need to write dirty pages is moot when the system is actually writing out other processes' pages to make room for the incoming ones needed by the cpu hog. I do know that I cannot control that in detail. Also it's entirely possible that file caching/readahead etc etc can skew the results.
All my old compiler texts recommend the buffered read approach, but that might be because mmap etc weren't around. Perhaps some compiler expert can say? Also I suspect that in a low level language the minor overhead caused by the book keeping is lower than that for the paging code.
> Let me return to your original problem though, doing regex operations on > files. I modified your two scripts slightly:
...... > I took the file from Bengt Richter's example and replicated it a bunch of > times to get a 122MB file. I then ran the above two programs against it:
> So the mmap'd version is within 15% of the performance of the buffered read > version and we don't have to solve the problem of any corner cases (note the > different values of n). I'm happy to take the extra runtime in exchange for > simpler code.
> Skip
I will have a go at repeating this on my system. Perhaps with Bengt's code in the buffered case as that would be more realistic.
It has been my experience that all systems crawl when driven into the swapping region and some users of our code seem anxious to run huge print jobs. -- Robin Becker
> I'm not sure why the mmap() solution is so much slower for you. Perhaps on > some systems files opened for reading are mmap'd under the covers. I'm sure > it's highly platform-dependent. (My results on MacOSX - see below - are > somewhat better.)
> Let me return to your original problem though, doing regex operations on > files. I modified your two scripts slightly:
I'm sure my results are dependent on something other than the coding style I suspect file/disk cache and paging operates here. Note that we now agree on total match length and split count. However, when the windows VM goes into paging mode the mmap thing falls off the world as I would expect for a thrashing system.
eg small memory (relatively) C:\code\reportlab\demos\gadflypaper>\tmp\sscan0.py xxx_100mb.dat fn=xxx_100mb.dat n=1898737 l=90506416 time=3.55
ie the freeBSD vm seems to thrash just as nastily as xp :(
#################################################################### Here I've implemented slightly modified versions of the scanners that you put forward.
eg
#sscan0.py thanks to Bengt import sys, time, re fn = sys.argv[1] rxo = re.compile('XXXXX')
def frxsplit(path, rxo, chunksize=4096): buffer = '' for chunk in iter((lambda f=open(path,'rb'): f.read(chunksize)),''): buffer += chunk pieces = rxo.split(buffer) for piece in pieces[:-1]: yield piece buffer = pieces[-1] yield buffer l=n=0 t0 = time.time() for mat in frxsplit(fn,rxo): n += 1 l += len(mat) t1 = time.time()
print "fn=%s n=%d l=%d time=%.2f" % (fn, n, l, (t1-t0))
#sscan1.py thanks to Skip import sys, time, mmap, os, re fn = sys.argv[1] fh=os.open(fn,os.O_BINARY|os.O_RDONLY) s=mmap.mmap(fh,0,access=mmap.ACCESS_READ) l=n=0 t0 = time.time() for mat in re.split("XXXXX", s): n += 1 l += len(mat) t1 = time.time()
print "fn=%s n=%d l=%d time=%.2f" % (fn, n, l, (t1 -- Robin Becker
.... > > As you try to understand mmap, make sure your mental model can take into > account the fact that it is easy and quite common to mmap a file several > times larger than your physical memory, and it does not even *try* to read > the whole thing in at any given time. You may benefit from > reviewing/studying the difference between virtual memory and physical > memory. I've been using vm systems for 30 years and I suspect my mental model is a bit decrepit. However, as convincingly demonstrated by testing my mental model seems able to predict low memory problems. When systems run out of memory they tend to perform poorly. I'm not sure the horrible degradation I see with large files is necessary, but I know it occurs on at least two common vm implementations. -- Robin Becker
> Let me return to your original problem though, doing regex operations on > files. I modified your two scripts slightly:
..... > Skip
I'm sure my results are dependent on something other than the coding style I suspect file/disk cache and paging operates here. Note that we now agree on total match length and split count. However, when the windows VM goes into paging mode the mmap thing falls off the world as I would expect for a thrashing system.
eg small memory (relatively) C:\code\reportlab\demos\gadflypaper>\tmp\sscan0.py xxx_100mb.dat fn=xxx_100mb.dat n=1898737 l=90506416 time=3.55
At the end of this run I had to wait quite a long time for other things to become responsive (ie things were entirely paged out).
Here I've implemented slightly modified versions of the scanners that you put forward.
eg
#sscan0.py thanks to Bengt import sys, time, re fn = sys.argv[1] rxo = re.compile('XXXXX')
def frxsplit(path, rxo, chunksize=4096): buffer = '' for chunk in iter((lambda f=open(path,'rb'): f.read(chunksize)),''): buffer += chunk pieces = rxo.split(buffer) for piece in pieces[:-1]: yield piece buffer = pieces[-1] yield buffer l=n=0 t0 = time.time() for mat in frxsplit(fn,rxo): n += 1 l += len(mat) t1 = time.time()
print "fn=%s n=%d l=%d time=%.2f" % (fn, n, l, (t1-t0))
#sscan1.py thanks to Skip import sys, time, mmap, os, re fn = sys.argv[1] fh=os.open(fn,os.O_BINARY|os.O_RDONLY) s=mmap.mmap(fh,0,access=mmap.ACCESS_READ) l=n=0 t0 = time.time() for mat in re.split("XXXXX", s): n += 1 l += len(mat) t1 = time.time()
print "fn=%s n=%d l=%d time=%.2f" % (fn, n, l, (t1-t0)) -- Robin Becker
Robin Becker wrote: > Skip Montanaro wrote: > ......
>> I'm not sure why the mmap() solution is so much slower for you. >> Perhaps on >> some systems files opened for reading are mmap'd under the covers. >> I'm sure >> it's highly platform-dependent. (My results on MacOSX - see below - are >> somewhat better.)
..... as a data point with sscan0/1.py (slight mods of your code) I get this with a 200mb file on freeBSD 4.9
Bengt> To be fairer, I think you'd want to hoist the re compilation out Bengt> of the loop.
The re module compiles and caches regular expressions, so I doubt it would affect the runtime of either version.
Bengt> But also to be fairer, maybe include the overhead of splitting Bengt> correctly, at least for the simple case regex in my example -- or Bengt> is a you-goofed post for me in the usenet forwarding queues Bengt> somewhere still? ;-)
I was just too lazy to incorporate (something like) your change. You will note that I was also lazy enough to simply steal your XXXXX file. <wink>