Some improvements to TMemoryStream

xiaoxiao2021-03-06  53

How is it about stream, huh, huh, it should be said that it is only the efficiency of the procedure recently, and I have no special research on Stream, but I have discovered some new usage, I hope it can be useful for everyone.

The cause of the matter is still the slotted electronic photo album software. I found a improvement today. I have a program I originally written:

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; I use a TMEMORYSTREAM to append a TMEMORYSTREAM to a file, I use Another TMEMORYSTREAM, let it open the file first, then use the CopyFrom () function, join the data from the original stream, and then save it to the file.

The worst of these is the CopyFrom () function, which opens up a new memory, first call the readbuffer () function, getting data from the source stream, then call the writebuffer () function, write itself in your own buffer, finally Release this temporary memory, these processes can be seen from this code:

Function TSTREAM.COPYFROM (Source: TSTREAM; Count: INT64): INT64; const Maxbufsize = $ f000; var buffsize, n: integer; buffer: pchar; begin if count = 0 dam 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; and don't know why , Delphi's own Move () function is particularly slow in memory. Finally, the result is that when I write about 30MB of data to the file, I will spend half a minute.

I know the problem, then to accelerate this process is very simple. First of all, I must avoid the memory copy, so I am determined to remove the cumbersome file, let the original stream write memory data to the file, then don't you? But whether it is TMEMORYSTREAM, or TFileStream, only provides full write data to a file, and what I need is to add functions, huh, huh, this simple, you open the file, then writefile () can, so the end The solution is:

Inheriting a new class from TMEMoryStream, temporarily called TMEMoryStreamEx, add a new method, called: appendtofile (), can completely add memory data to existing files, 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;

Ok, replace the original program, the new program becomes:

Procedure texeexporter.createexecutablefile (const); begin asam.appendtream; begin astream.appendtream (AfileName); END; be as simple, speed is shortened to just 2-3 seconds.

A series of software made in the nearest unit is also conducting speed optimization, using a lot of ways, managing memory yourself (reducing Malloc calls), using HashTable to store data that is often looking for. . . . Wait, I saw the software I developed in the speed of the leap, it is really a sense of accomplishment.

转载请注明原文地址:https://www.9cbs.com/read-82072.html

New Post(0)