Search code examples
delphidelphi-7

Optimal Buffer Stream write process


We have our own data streaming algorithm that include some metadata+records+fields values.

Currently we use a TStream and write to add values to the stream. Now I'm wondering if this time cosuming operation could be made faster by using some technic.

Edit: We are only appending data to the end, not moving or seeking.

Some things I was thinking of are:

  • Not use Streams buf some large memory allocated buffer to copy data to, the problem is if we go beyond the buffer size, then we have to relocate to some new memory space.
  • Use a Stream prefilled with #0s to some size and then start adding values. The rationale is that Tstream have to allocate it's own buffer every time I do a write (I don't know how it really works, just wondering...)

We are adding strings to the TStream and binary data in the form #0#0#0#1 for example.

The data is then transfered via TCP, so it's not about write to a File.

So what is the best method for this?


Solution

  • Overwrite TMemoryStream and remove the restriction of Size and Capacity. And do not call TMemoryStream.Clear but call TMemoryStream.SetSize(0)

    type
      TMemoryStreamEx = class(TMemoryStream)
      public
        procedure SetSize(NewSize: Longint); override;
        property Capacity;
      end;
    
    implementation
    
    { TMemoryStreamEx }
    
    procedure TMemoryStreamEx.SetSize(NewSize: Integer);
    var
      OldPosition: Longint;
    begin
      if NewSize > Capacity then
        inherited SetSize(NewSize)
      else
      begin
        OldPosition := Position;
        SetPointer(Memory, NewSize);
        if OldPosition > NewSize then
          Seek(0, soFromEnd);
      end;
    end;