Skip to content

Commit

Permalink
try to avoid memory allocation in IntegerDynArrayToCSV/Int64DynArrayT…
Browse files Browse the repository at this point in the history
…oCSV
  • Loading branch information
synopse committed Sep 21, 2016
1 parent b5021d7 commit 93f3c79
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions SynCommons.pas
Expand Up @@ -26979,12 +26979,14 @@ function IntegerDynArrayToCSV(const Values: array of integer; ValuesCount: integ
tmp: array[0..15] of AnsiChar;
ints: ^TInts16;
P: PAnsiChar;
tmpbuf: TSynTempBuffer;
begin
result := '';
if ValuesCount=0 then
exit;
GetMem(ints,ValuesCount*sizeof(ints[0])); // getmem is faster than a dynamic array
tmpbuf.Init(ValuesCount*sizeof(ints[0])); // faster than a dynamic array
try
ints := tmpbuf.buf;
// compute whole result length at once
dec(ValuesCount);
Len := length(Prefix)+length(Suffix);
Expand All @@ -27011,7 +27013,7 @@ function IntegerDynArrayToCSV(const Values: array of integer; ValuesCount: integ
if Suffix<>'' then
MoveFast(pointer(Suffix)^,P^,length(Suffix));
finally
FreeMem(ints);
tmpbuf.Done;
end;
end;

Expand All @@ -27023,19 +27025,19 @@ function Int64DynArrayToCSV(const Values: array of Int64; ValuesCount: integer;
Val: array[0..19] of AnsiChar; // Int64: 19 digits, then - sign
end;
var i, L, Len: PtrInt;
ints: pointer;
int: ^TInt;
P: PAnsiChar;
tmp: TSynTempBuffer;
begin
result := '';
if ValuesCount=0 then
exit;
GetMem(ints,ValuesCount*sizeof(TInt)); // getmem is faster than a dynamic array
tmp.Init(ValuesCount*sizeof(TInt)); // faster than a dynamic array
try
// compute whole result length at once
dec(ValuesCount);
Len := length(Prefix)+length(Suffix);
int := ints;
int := tmp.buf;
for i := 0 to ValuesCount do begin
P := StrInt64(PAnsiChar(int)+21,Values[i]);
L := PAnsiChar(int)+21-P;
Expand All @@ -27052,7 +27054,7 @@ function Int64DynArrayToCSV(const Values: array of Int64; ValuesCount: integer;
MoveFast(pointer(Prefix)^,P^,length(Prefix));
inc(P,length(Prefix));
end;
int := ints;
int := tmp.buf;
repeat
L := int^.Len;
MoveFast(PAnsiChar(int)[21-L],P^,L);
Expand All @@ -27067,7 +27069,7 @@ function Int64DynArrayToCSV(const Values: array of Int64; ValuesCount: integer;
if Suffix<>'' then
MoveFast(pointer(Suffix)^,P^,length(Suffix));
finally
FreeMem(ints);
tmp.Done;
end;
end;

Expand Down
2 changes: 1 addition & 1 deletion SynopseCommit.inc
@@ -1 +1 @@
'1.18.2981'
'1.18.2982'

0 comments on commit 93f3c79

Please sign in to comment.