in reply to Re^2: Out of memory problems
in thread Out of memory problems
Hmm. I'm not surprised your processing is slow. Converting the whole 3 GB to ascii-ized binary will produce a 24 GB file. Then search/replace on that can convert back as separate stages?
Looking at your regex, it looks like your patterns are probably byte aligned? Very few processes produce data that is not...but I realise that it is possible. This how would tackle the task assuming tha the data is byte aligned.
The technique is called a 'sliding buffer'. You read a buffer of at least double the size of the thing you are looking for. In this case, 384 x 2 = 768 bytes. You then apply the regex to the whole of that buffer, write out the first half of it, move the second half to the front, top it up from the input file and re-apply the regex to the whole thing.
Note. That is a simplified description and the following code implements that simplified description by way of example only. It isn't tested and almost certainly won't work as is.
#!/usr/local/bin/perl -w use strict; open IN, '< :raw', $ARGV[ 0 ] or die "$ARGV[ 0 ] : $!"; open OUT, '> :raw', $ARGV[ 1 ] or die "$ARGV[ 1 ] : $!"; my $buffer; sysread IN, $buffer, 384, 384; do{ ## Move the second half of the buffer to the front. $buffer = substr( $buffer, 384 ); ## and overwrite it with a new chunk sysread IN, $buffer, 384, length( $buffer ); ## Apply the regex $buffer =~ s[ \xF4 . ## The marker byte plus friend ( .{190} ) ## 1520 bits to retain \xF4 . ## Second marker + friend ( .{58} ) ## 464 bits to retain .{132} ## 1056 bits to discard ][$1$2]xg; ## Write out the first half of the buffer syswrite OUT, $buffer, 384; } until eof IN; close IN; close OUT;
The are some further complications that need to be addressed.
When no substitution has taken place, the above method should be okay.
When a substitution has occured, you will need to ensure that you do not re-process that amount of the buffer upto the place where the end of the last substitution occured--other wise you could get a false match/substitution occuring. See perlfunc:pos and /or perlvar @- & @+ for more information.
Basically, you need to find out which part of the buffer has already been processed and write that out and only copy the remainder to the front of the buffer before refilling it to the x2 length.
It will take thought, and testing (and testdata) to get this right! It is non-trivial to do and harder to describe.
Once you have the process working, instead of using a x2 buffer, you could save considerable time by reading and writing much larger chunks-- 1, 2, 10, or even 100 MB would be possible.
The only thing to remember is that if no match occurs you must retain the last 384 bytes from the buffer and place these at the front of the next to ensure full coverage.
If one (or more) substitutions have occurred, then you must retain everything from the end of the last substitution to the end--if that less than 384 bytes--or the last 384 bytes.
Processing your 3 GB this way should take no more than 1/2 hour if you can avoid the packing/unpacking. And not much more than a couple of hours if you cannot. These are ballpark figures!
I hope that rather sketchy explaination makes some kind of sense. Good luck.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Out of memory problems
by tperdue (Sexton) on Oct 21, 2004 at 21:45 UTC | |
by BrowserUk (Patriarch) on Oct 21, 2004 at 22:13 UTC | |
|
Re^4: Out of memory problems
by tperdue (Sexton) on Oct 22, 2004 at 15:57 UTC | |
by BrowserUk (Patriarch) on Oct 22, 2004 at 19:36 UTC | |
by tperdue (Sexton) on Oct 23, 2004 at 14:33 UTC | |
by BrowserUk (Patriarch) on Oct 24, 2004 at 06:29 UTC | |
by tperdue (Sexton) on Oct 26, 2004 at 10:48 UTC | |
|