Quick Improvements on your TFileStream Access in Delphi


Recently I am involved in optimising the Delphi code for I/O file access. I found it a performance bottleneck in dealing with big files. For example, computing XOR check sum of a TFileStream requires looping each 4-byte using ReadBuffer() which yields unncessary calls to Win32 API ReadFile().

There are quite a lot of ways to improve that. Some require restructuring the I/O classes while some others are quick to do. For example, if your I/O does most of the cases sequential accesses via ReadBuffer(). You can optimised the following:

while (fs.Position <= fs.Size) do
begin
  // lots of ReadBuffer()
end;

to

var
  sz: Int64;
..
sz := fs.Size;
while (fs.Position <= sz) do
begin
  // lots of ReadBuffer()
end;

The reason that the second one is faster because of the Size property is a virtual function that it needs three calls to find the actual size. Caching this variable in local stack can improve the performance especially if the file is big.

The other quick improvement is to use Read() instead of ReadBuffer(), and the Write() instead of WriteBuffer(). The ReadBuffer() and WriteBuffer() are usually calling Read() and Write(); and they will raise Exceptions when count read or written does not match the one to read or write.

procedure TStream.ReadBuffer(var Buffer; Count: Longint);
begin
  if (Count <> 0) and (Read(Buffer, Count) <> Count) then
    raise EReadError.CreateRes(@SReadError);
end;

procedure TStream.WriteBuffer(const Buffer; Count: Longint);
begin
  if (Count <> 0) and (Write(Buffer, Count) <> Count) then
    raise EWriteError.CreateRes(@SWriteError);
end;

If you don’t care whether the I/O succeeded or not, you can safely use the Read() and Write(). But it is still recommended to include any I/O code in the try … finally … that will clean up the stream objects.

More to come on using Windows File Mapping and Cached TFileStream (that reduces the number of Read() and Write() by using a buffer).

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
413 words
Last Post: File Opening Mode for TFileStream in Delphi
Next Post: Passing Arguments to BASH function

The Permanent URL is: Quick Improvements on your TFileStream Access in Delphi

One Response

  1. Blaise

Leave a Reply