* ppudump: Use buffered write when outputting to json or xml. It improves the output speed a lot.

git-svn-id: trunk@41393 -
This commit is contained in:
yury 2019-02-20 10:13:18 +00:00
parent 25cb06f021
commit 9a3ddf650c
4 changed files with 49 additions and 17 deletions

View File

@ -4131,12 +4131,12 @@ begin
'J':
begin
nostdout:=True;
pout:=TPpuJsonOutput.Create(Output);
pout:=TPpuJsonOutput.Create(StdOutputHandle);
end;
'X':
begin
nostdout:=True;
pout:=TPpuXmlOutput.Create(Output);
pout:=TPpuXmlOutput.Create(StdOutputHandle);
end;
else
begin

View File

@ -47,7 +47,7 @@ type
procedure WriteBool(const AName: string; AValue: boolean); override;
procedure WriteNull(const AName: string); override;
public
constructor Create(var OutFile: Text); override;
constructor Create(OutFileHandle: THandle); override;
procedure IncI; override;
procedure DecI; override;
end;
@ -214,9 +214,9 @@ begin
Write('}');
end;
constructor TPpuJsonOutput.Create(var OutFile: Text);
constructor TPpuJsonOutput.Create(OutFileHandle: THandle);
begin
inherited Create(OutFile);
inherited Create(OutFileHandle);
SetLength(FNeedDelim, 10);
FNeedDelim[0]:=False;
end;

View File

@ -39,11 +39,14 @@ type
{ TPpuOutput }
TPpuOutput = class
private
FOutFile: ^Text;
FOutFileHandle: THandle;
FOutBuf: array[0..10000] of char;
FOutBufPos: integer;
FIndent: integer;
FIndentSize: integer;
FIndStr: string;
FNoIndent: boolean;
procedure Flush;
procedure SetIndent(AValue: integer);
procedure SetIndentSize(AValue: integer);
protected
@ -57,7 +60,7 @@ type
procedure WriteBool(const AName: string; AValue: boolean); virtual;
procedure WriteNull(const AName: string); virtual;
public
constructor Create(var OutFile: Text); virtual;
constructor Create(OutFileHandle: THandle); virtual;
destructor Destroy; override;
procedure Write(const s: string);
procedure WriteLn(const s: string = '');
@ -1187,22 +1190,56 @@ begin
DecI;
end;
constructor TPpuOutput.Create(var OutFile: Text);
constructor TPpuOutput.Create(OutFileHandle: THandle);
begin
FOutFile:=@OutFile;
FOutFileHandle:=OutFileHandle;
FIndentSize:=2;
end;
destructor TPpuOutput.Destroy;
begin
Flush;
inherited Destroy;
end;
procedure TPpuOutput.Flush;
var
i, len: integer;
begin
i:=0;
while FOutBufPos > 0 do begin
len:=FileWrite(FOutFileHandle, FOutBuf[i], FOutBufPos);
if len < 0 then
raise Exception.CreateFmt('Error writing to file: ', [SysErrorMessage(GetLastOSError)]);
Inc(i, len);
Dec(FOutBufPos, len);
end;
end;
procedure TPpuOutput.Write(const s: string);
var
ss: string;
i, len, len2: integer;
begin
if not FNoIndent then
System.Write(FOutFile^, FIndStr);
System.Write(FOutFile^, s);
ss:=FIndStr + s
else
ss:=s;
i:=1;
len:=Length(ss);
while len > 0 do begin
len2:=Length(FOutBuf) - FOutBufPos;
if len2 > 0 then begin
if len < len2 then
len2:=len;
Move(ss[i], FOutBuf[FOutBufPos], len2);
Inc(FOutBufPos, len2);
end;
if FOutBufPos = Length(FOutBuf) then
Flush;
Inc(i, len2);
Dec(len, len2);
end;
FNoIndent:=True;
end;
@ -1228,6 +1265,7 @@ end;
procedure TPpuOutput.Done;
begin
Flush;
end;
{ TPpuUnitDef }

View File

@ -41,7 +41,6 @@ type
procedure WriteArrayEnd(const AName: string); override;
procedure WriteStr(const AName, AValue: string); override;
public
constructor Create(var OutFile: Text); override;
procedure Init; override;
end;
@ -162,11 +161,6 @@ begin
WriteLn(Format('</%s>', [GetTagName(Def.DefTypeName, 'object')]));
end;
constructor TPpuXmlOutput.Create(var OutFile: Text);
begin
inherited Create(OutFile);
end;
procedure TPpuXmlOutput.Init;
begin
inherited Init;