Skip to content
Permalink
Browse files

deep pass over the whole framework to fix and tune memory allocation …

…of strings or TBytes returned as functions result
  • Loading branch information
Arnaud Bouchez
Arnaud Bouchez committed Nov 28, 2019
1 parent e8c46de commit 63672f7bcf0ac922031b0dc57ad0bd6d96c5e17c
Showing with 36 additions and 25 deletions.
  1. +5 −0 SQLite3/mORMot.pas
  2. +2 −2 SQLite3/mORMotMVC.pas
  3. +6 −6 SynCrtSock.pas
  4. +3 −0 SynCrypto.pas
  5. +1 −0 SynLog.pas
  6. +1 −1 SynSelfTests.pas
  7. +17 −15 SynTable.pas
  8. +1 −1 SynopseCommit.inc
var props: PPropInfoDynArray;
n,i: integer;
begin
result := nil;
props := ClassFieldAllProps(ClassType,Types);
n := length(props);
SetLength(result,n);
var max,i: integer;
V: PShortString;
begin
Finalize(result);
max := MaxValue-MinValue;
SetLength(result,max+1);
V := @NameList;
var t: TIntegerDynArray;
n,i: integer;
begin
result := nil;
t := GetTableIndexesFromSQLSelect(SQL);
n := length(t);
if n=0 then
const aGUIDs: array of TGUID): PTypeInfoDynArray;
var i: integer;
begin
result := nil;
SetLength(result,length(aGUIDs));
for i := 0 to high(aGUIDs) do
result[i] := GUID2TypeInfo(aGUIDs[i]);
function TServiceMethod.ArgsNames(Input: Boolean): TRawUTF8DynArray;
var a,n: integer;
begin
result := nil;
if Input then begin
SetLength(result,ArgsInputValuesCount);
n := 0;
@@ -1218,7 +1218,7 @@ constructor TMVCSessionWithCookies.Create;
inherited Create;
fContext.CookieName := 'mORMot';
// temporary secret for encryption
fContext.CryptNonce := Random32;
fContext.CryptNonce := Random32gsl;
TAESPRNG.Main.FillRandom(@fContext.Crypt,sizeof(fContext.Crypt));
// temporary secret for HMAC-CRC32C
TAESPRNG.Main.FillRandom(@rnd,sizeof(rnd));
@@ -1308,7 +1308,7 @@ function TMVCSessionWithCookies.Initialize(
if len>sizeof(tmp.data) then // all cookies storage should be < 4K
raise EMVCApplication.CreateGotoError('Big Fat Cookie');
result := InterlockedIncrement(fContext.SessionCount);
tmp.head.cryptnonce := Random32;
tmp.head.cryptnonce := Random32gsl;
tmp.head.session := result;
tmp.head.issued := UnixTimeUTC;
if SessionTimeOutMinutes=0 then
@@ -3857,13 +3857,13 @@ procedure RaiseLastModuleError(ModuleName: PChar; ModuleException: ExceptClass);
function Ansi7ToUnicode(const Ansi: SockString): SockString;
var n, i: integer;
begin // fast ANSI 7 bit conversion
result := '';
if Ansi='' then
result := '' else begin
n := length(Ansi);
SetLength(result,n*2+1);
for i := 0 to n do // to n = including last #0
PWordArray(pointer(result))^[i] := PByteArray(pointer(Ansi))^[i];
end;
exit;
n := length(Ansi);
SetLength(result,n*2+1);
for i := 0 to n do // to n = including last #0
PWordArray(pointer(result))^[i] := PByteArray(pointer(Ansi))^[i];
end;

function DefaultUserAgent(Instance: TObject): SockString;
@@ -12133,6 +12133,7 @@ function TAESAbstract.EncryptPKCS7(const Input: RawByteString;
function TAESAbstract.EncryptPKCS7(const Input: TBytes;
IVAtBeginning: boolean): TBytes;
begin
result := nil;
SetLength(result,EncryptPKCS7Length(length(Input),IVAtBeginning));
EncryptPKCS7Buffer(Pointer(Input),pointer(result),
length(Input),length(result),IVAtBeginning);
@@ -13677,6 +13678,8 @@ function TAESPRNG.FillRandom(Len: integer): RawByteString;

function TAESPRNG.FillRandomBytes(Len: integer): TBytes;
begin
if Len<>length(result) then
result := nil;
SetLength(result,Len);
FillRandom(pointer(result),Len);
end;
@@ -5451,6 +5451,7 @@ function TSynLogFile.ThreadName(ThreadID, CurrentLogIndex: integer): RawUTF8;
function TSynLogFile.ThreadNames(CurrentLogIndex: integer): TRawUTF8DynArray;
var i: integer;
begin
result := nil;
SetLength(result,fThreadMax);
if fThreadInfo=nil then
exit;
@@ -11329,7 +11329,7 @@ procedure TTestCryptographicRoutines._TAESPNRG;
check(SynCommons.HexToBin(pointer(s1),nil,i));
c := a1.Random32;
check(c<>a2.Random32,'Random32 collision');
if c<maxint then
if c<cardinal(maxint) then
inc(clo) else
inc(chi);
check(a1.Random64<>a2.Random64);
@@ -4360,7 +4360,7 @@ function TSynTable.AddField(const aName: RawUTF8;
end;

procedure TSynTable.UpdateFieldData(RecordBuffer: PUTF8Char; RecordBufferLen,
FieldIndex: integer; var result: TSBFString; const NewFieldData: TSBFString='');
FieldIndex: integer; var result: TSBFString; const NewFieldData: TSBFString);
var NewSize, DestOffset, OldSize: integer;
F: TSynTableFieldProperties;
NewData, Dest: PAnsiChar;
@@ -4390,7 +4390,7 @@ procedure TSynTable.UpdateFieldData(RecordBuffer: PUTF8Char; RecordBufferLen,
// update content
OldSize := F.GetLength(Dest);
dec(RecordBufferLen,OldSize);
SetLength(Result,RecordBufferLen+NewSize);
SetString(Result,nil,RecordBufferLen+NewSize);
MoveFast(RecordBuffer^,PByteArray(result)[0],DestOffset);
MoveFast(NewData^,PByteArray(result)[DestOffset],NewSize);
MoveFast(Dest[OldSize],PByteArray(result)[DestOffset+NewSize],RecordBufferLen-DestOffset);
@@ -7268,7 +7268,7 @@ function SearchSBNDMQ2ComputeMask(const Pattern: RawUTF8; u: PNormTable): RawByt
m: PSBNDMQ2Mask absolute result;
c: PCardinal;
begin
SetLength(result, SizeOf(m^));
SetString(result, nil, SizeOf(m^));
{$ifdef FPC}FillChar{$else}FillCharFast{$endif}(m^, SizeOf(m^), 0);
for i := 0 to length(Pattern) - 1 do begin
c := @m^[u[p[i]]]; // for FPC code generation
@@ -10410,9 +10410,9 @@ function TSynConnectionDefinition.SaveToJSON: RawUTF8;
constructor TSynAuthenticationAbstract.Create;
begin
fSafe.Init;
fTokenSeed := Random32;
fTokenSeed := Random32gsl;
fSessionGenerator := abs(fTokenSeed*PPtrInt(self)^);
fTokenSeed := abs(fTokenSeed*Random32);
fTokenSeed := abs(fTokenSeed*Random32gsl);
end;

destructor TSynAuthenticationAbstract.Destroy;
@@ -10691,22 +10691,22 @@ function FieldIndexToBits(const Index: TSQLFieldIndexDynArray): TSQLFieldBits;

function DateToSQL(Date: TDateTime): RawUTF8;
begin
result := '';
if Date<=0 then
result := '' else begin
SetLength(result,13);
PCardinal(pointer(result))^ := JSON_SQLDATE_MAGIC;
DateToIso8601PChar(Date,PUTF8Char(pointer(result))+3,True);
end;
exit;
FastSetString(result,nil,13);
PCardinal(pointer(result))^ := JSON_SQLDATE_MAGIC;
DateToIso8601PChar(Date,PUTF8Char(pointer(result))+3,True);
end;

function DateToSQL(Year,Month,Day: Cardinal): RawUTF8;
begin
result := '';
if (Year=0) or (Month-1>11) or (Day-1>30) then
result := '' else begin
SetLength(result,13);
PCardinal(pointer(result))^ := JSON_SQLDATE_MAGIC;
DateToIso8601PChar(PUTF8Char(pointer(result))+3,True,Year,Month,Day);
end;
exit;
FastSetString(result,nil,13);
PCardinal(pointer(result))^ := JSON_SQLDATE_MAGIC;
DateToIso8601PChar(PUTF8Char(pointer(result))+3,True,Year,Month,Day);
end;

var
@@ -12634,6 +12634,7 @@ procedure InitWindowsAPI;
function EnumAllProcesses(out Count: Cardinal): TCardinalDynArray;
var n: cardinal;
begin
result := nil;
n := 2048;
repeat
SetLength(result, n);
@@ -13000,6 +13001,7 @@ function TSystemUse.History(aProcessID,aDepth: integer): TSingleDynArray;
var i,n: integer;
data: TSystemUseDataDynArray;
begin
result := nil;
data := HistoryData(aProcessID,aDepth);
n := length(data);
SetLength(result,n);
@@ -1 +1 @@
'1.18.5481'
'1.18.5482'

0 comments on commit 63672f7

Please sign in to comment.
You can’t perform that action at this time.