Some improvements to tmemorystream
Why is it about stream? It should be said that it is only recently concerned about the efficiency of the program. 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 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 call the readbuffer () function, get data from the source stream, and then call its own writebuffer () function, write it to its own buffer, and finally release this temporary memory. You can refer to this code during these processes:
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;
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 easy to speed up the process. First of all, we need to 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. open the file by yourself, then writefile () is enough, so the final solution is:
A new class is inherited from tmemorystream, which is also called tmemorystreamex. A new method called appendtofile () can be added to 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;
Okay, 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's a sense of accomplishment to see that the software you have developed has made a qualitative leap in terms of speed.