From 93f3c79de28606e55086936fd1ed2a0202b19713 Mon Sep 17 00:00:00 2001 From: ab Date: Wed, 21 Sep 2016 09:58:43 +0200 Subject: [PATCH] try to avoid memory allocation in IntegerDynArrayToCSV/Int64DynArrayToCSV --- SynCommons.pas | 16 +++++++++------- SynopseCommit.inc | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/SynCommons.pas b/SynCommons.pas index fa2dd3929..54e5b692e 100644 --- a/SynCommons.pas +++ b/SynCommons.pas @@ -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); @@ -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; @@ -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; @@ -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); @@ -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; diff --git a/SynopseCommit.inc b/SynopseCommit.inc index 35dc00fbe..d8ca16a52 100644 --- a/SynopseCommit.inc +++ b/SynopseCommit.inc @@ -1 +1 @@ -'1.18.2981' +'1.18.2982'