Need efficient way to read file in reverse

Thomas M. Breuel tmb at ai.mit.edu
Sat Sep 29 05:58:13 AEST 1990


In article <1802 at s5.Morgan.COM>, chuck at Morgan.COM (Chuck Ocheret) writes:
|> I recommend that you check to see if your system supports some form of
|> memory mapping of a file.  On some systems there is the mmap(2) call on
|> others a version of shmat(2) allows you to do mostly the same thing.
|> Not all systems provide such a mechanism.  When you map a file in using
|> either of these you get a pointer to memory which maps directly to a
|> specified file on the file system (can also be a device).  This gives
|> you full random access to all bytes in the file.  Read it backwards,
|> forwards, or however you wish; your file has just become a char *.

Believing in magic won't make your code run fast. Just because you can
access the components of your file in random order by dereferencing a
"char*" doesn't mean that that method will be particularly efficient.
Depending on how mmap(2) is implemented and what paging algorithms your
kernel is using, this may in fact be one of the least efficient methods
for reading a file backwards or in random order.

If you want high performance disk access for some particular access
pattern, the proven and reliable way is to work out a buffering
scheme, thinking carefully about how much real memory you have
available, what parts of the file you need to access how often, etc.

For reading a file backwards, this is fortunately straightforward,
since this case is essentially identical to sequential access of
in the "forward" direction.



More information about the Comp.unix.internals mailing list