Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use a TMemoryStream as an efficient buffer before writing to a file?

Tags:

file-io

delphi

I am using D6 Professional and need to create a text file in a specific format from lots of small strings already in memory. For performance reasons, I am considering using a TMemoryStream to collate the file data, and then write it out to disk in one go via a TFileStream.

But I have a half forgotten memory (probably from pre-D6 days) of reading somewhere that TMemoryStream is inefficient, especially after it hits its Capacity size. My Delphi (and Windows API) skill is not good enough to check the Classes.pas code for myself.

(OFFTOPIC) especially code like this: (line 5152 of Classes.pas):
NewCapacity := (NewCapacity + (MemoryDelta - 1)) and not (MemoryDelta - 1);
(/OFFTOPIC)

Adding to my worry is that the conclusion of a related question Using MemoryStream to write out to XML
was not to use TMemoryStream, but didn't say why - whether due to TMemoryStream itself, or because there is sufficient buffering in the TFileStream or the I/O device driver, or just the specifics of the code in question.

Thanks for any advice
Regards,
PhilW.

like image 214
PhilW Avatar asked Nov 30 '22 12:11

PhilW


1 Answers

A normal TFileStram also does buffering, and that is sufficient to optimize I/O. Putting a MemoryStream in front only adds overhead.

like image 134
Henk Holterman Avatar answered Dec 04 '22 23:12

Henk Holterman