Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: regex over files (http://www.velocityreviews.com/forums/t344213-re-regex-over-files.html)

Robin Becker 04-25-2005 03:55 PM

Re: regex over files
 
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?

>
>



--
Robin Becker


Richard Brodie 04-26-2005 08:39 AM

Re: regex over files
 

"Robin Becker" <robin@reportlab.com> wrote in message
news:mailman.2469.1114444689.1799.python-list@python.org...
> 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.



Robin Becker 04-26-2005 08:55 AM

Re: regex over files
 
Richard Brodie wrote:
> "Robin Becker" <robin@reportlab.com> wrote in message
> news:mailman.2469.1114444689.1799.python-list@python.org...
>
>>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.
>
>


:)

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

Steve Holden 04-26-2005 09:36 AM

Re: regex over files
 
Robin Becker wrote:
> Richard Brodie wrote:
>
>> "Robin Becker" <robin@reportlab.com> wrote in message
>> news:mailman.2469.1114444689.1799.python-list@python.org...
>>
>>> 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.
>>
>>

>
> :)
>
> 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.

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/


Robin Becker 04-26-2005 11:12 AM

Re: regex over files
 
Steve Holden wrote:
.......
>
> 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.

......
thanks I'll give it a whirl

--
Robin Becker


Steve Holden 04-26-2005 12:34 PM

Re: regex over files
 
Robin Becker wrote:
> Steve Holden wrote:
> ......
>
>>
>> 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.

>
> .....
> thanks I'll give it a whirl
>

Whoops, I don't think it's a regex search :-(

You should be able to adapt the logic fairly easily, I hope.

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/


Robin Becker 04-26-2005 04:19 PM

Re: regex over files
 
Steve Holden wrote:
.....
>> .....
>> thanks I'll give it a whirl
>>

> Whoops, I don't think it's a regex search :-(
>
> 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


Skip Montanaro 04-26-2005 05:40 PM

Re: regex over files
 

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

Robin Becker 04-26-2005 06:32 PM

Re: regex over files
 
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


Jeremy Bowers 04-26-2005 07:25 PM

Re: regex over files
 
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.


All times are GMT. The time now is 02:37 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.