Some improvements to tmemorystream-Delphi tutorial

Source: Internet
Author: User

Why is it about stream? Well, it should be said that it is only recently concerned about program efficiency. However, I have not made any special research on stream, but I have discovered some new usage, I hope it will be useful to everyone.

The cause of the incident was the tattered electronic album software. Today I found another improvement. I wrote a program like this:
Procedure createfile (const afilename: string; const astream: tmemorystream );
VaR
Filestream: tmemorystream;
Begin
Showprogressform (NiL );
Filestream: = tmemorystream. Create ();
Try
Filestream. loadfromfile (afilename );
Filestream. Position: = filestream. size;
Astream. Position: = 0;
Filestream. copyfrom (astream, astream. size );
Filestream. savetofile (afilename );
Finally
Filestream. Free;
End;
End;
To complete the task of appending a tmemorystream to a file, I used another tmemorystream to enable it to open the file first and then use the copyfrom () function to add data from the original stream, and then save it to the file.
The worst of them is the copyfrom () function, which will open up a new memory, first calls the readbuffer () function, obtains data from the source stream, and then calls its own writebuffer () function, write it to its own buffer, and finally release this temporary memory. These processes can see this section. Code :
function tstream. copyfrom (Source: tstream; count: int64): int64;
const
maxbufsize = $ f000;
var
bufsize, N: integer;
Buffer: pchar;
begin
If Count = 0 then
begin
source. position: = 0;
count: = source. size;
end;
result: = count;
If count> maxbufsize then bufsize: = maxbufsize else bufsize: = count;
getmem (buffer, bufsize);
try
while count <> 0 DO
begin
If count> bufsize then N: = bufsize else N: = count;
source. readbuffer (buffer ^, n);
writebuffer (buffer ^, n);
Dec (count, n);
end;
finally
freemem (buffer, bufsize);
end;
Furthermore, I don't know why the move () function provided by Delphi is particularly slow in memory copying. The final result is that it takes half a minute to write about 30 mb of data to a file.

Knowing the problem, it is very easy to speed up the process. First of all, we must avoid Memory copying, so I am determined to remove the cumbersome filestream, let the original stream write the memory data to the file by itself. Isn't that enough?
However, both tmemorystream and tfilestream only provide the ability to completely write data into one file, while what I need is the append function. This is simple and I can open the file myself, then writefile () is ready, so the final solution is:
A new class is inherited from tmemorystream, which is also called tmemorystreamex. A new method is added, called appendtofile (). It can completely append memory data to an existing file. The function content is as follows:
Procedure tmemorystreamex. appendtofile (const afilename: string );
VaR
Filehandle: longword;
Curpos: longword;
Byteswritten: longword;
Begin
Filehandle: = createfile (pchar (afilename), generic_write, 0, nil, open_always, file_attribute_normal, 0 );
If filehandle = invalid_handle_value then begin
Raise memorystreamexexception. Create (error when Create File );
End;
Try
Curpos: = setfilepointer (filehandle, 0, nil, file_end );
Lockfile (filehandle, curpos, 0, size, 0 );
Try
Byteswritten: = 0;
If not writefile (filehandle, memory ^, size, byteswritten, nil) then begin
Raise memorystreamexexception. Create (error when Write File );
End;
If (size <> byteswritten) then begin
Raise memorystreamexexception. Create (wrong written size );
End;
Finally
Unlockfile (filehandle, curpos, 0, size, 0 );
End;
Finally
Closehandle (filehandle );
End;
End;

Now, replace the original program and the new program becomes:
Procedure texeexporter. createexecutablefile (const afilename: string; const astream: tmemorystreamex );
Begin
Astream. appendtofile (afilename );
End;
This is simple, and the speed is reduced to only 2-3 seconds.

Recently, a series of software developed by the Organization are also being optimized. Many methods are used to manage the memory (reduce the number of malloc calls ), use hashtable to store data that is frequently searched .... And so on. It is a great sense of accomplishment to see that the software we have developed has made a qualitative leap in terms of speed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.