THRIFT-5428 Prevent costly reallocations to improve performance
Client: Delphi
Patch: Jens Geyer
diff --git a/lib/delphi/src/Thrift.Transport.pas b/lib/delphi/src/Thrift.Transport.pas
index 79fc977..558b65e 100644
--- a/lib/delphi/src/Thrift.Transport.pas
+++ b/lib/delphi/src/Thrift.Transport.pas
@@ -334,8 +334,8 @@
   strict private
     FStream : IThriftStream;
     FBufSize : Integer;
-    FReadBuffer : TMemoryStream;
-    FWriteBuffer : TMemoryStream;
+    FReadBuffer : TThriftMemoryStream;
+    FWriteBuffer : TThriftMemoryStream;
   strict protected
     procedure Write( const pBuf : Pointer; offset: Integer; count: Integer); override;
     function Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer; override;
@@ -450,8 +450,8 @@
   strict protected type
     TFramedHeader = Int32;
   strict protected
-    FWriteBuffer : TMemoryStream;
-    FReadBuffer : TMemoryStream;
+    FWriteBuffer : TThriftMemoryStream;
+    FReadBuffer : TThriftMemoryStream;
 
     procedure InitWriteBuffer;
     procedure ReadFrame;
@@ -1053,8 +1053,8 @@
   inherited Create;
   FStream := aStream;
   FBufSize := aBufSize;
-  FReadBuffer := TMemoryStream.Create;
-  FWriteBuffer := TMemoryStream.Create;
+  FReadBuffer := TThriftMemoryStream.Create(FBufSize);
+  FWriteBuffer := TThriftMemoryStream.Create(FBufSize);
 end;
 
 destructor TBufferedStreamImpl.Destroy;
@@ -1379,16 +1379,11 @@
   Result := InnerTransport.IsOpen;
 end;
 
-type
-  TAccessMemoryStream = class(TMemoryStream)
-  end;
-
 procedure TFramedTransportImpl.InitWriteBuffer;
 const DUMMY_HEADER : TFramedHeader = 0;
 begin
   FreeAndNil( FWriteBuffer);
-  FWriteBuffer := TMemoryStream.Create;
-  TAccessMemoryStream(FWriteBuffer).Capacity := 1024;
+  FWriteBuffer := TThriftMemoryStream.Create(1024);
   FWriteBuffer.Write( DUMMY_HEADER, SizeOf(DUMMY_HEADER));
 end;
 
@@ -1448,7 +1443,7 @@
   InnerTransport.ReadAll( buff, 0, size );
 
   FreeAndNil( FReadBuffer);
-  FReadBuffer := TMemoryStream.Create;
+  FReadBuffer := TThriftMemoryStream.Create(1024);
   if Length(buff) > 0
   then FReadBuffer.Write( Pointer(@buff[0])^, size );
   FReadBuffer.Position := 0;