Skip to content

Commit 40381b3

Browse files
committed
Misc. micro optimizations for compiler
1 parent 6d3a857 commit 40381b3

File tree

5 files changed

+72
-58
lines changed

5 files changed

+72
-58
lines changed

Source/dwsCompiler.pas

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,32 +1104,36 @@ function TStandardSymbolFactory.ReadInitExpr(expecting : TTypeSymbol = nil) : TT
11041104
end;
11051105

11061106
begin
1107-
if expecting<>nil then begin
1107+
if expecting <> nil then begin
11081108
case FCompiler.Tokenizer.TestAny([ttBLEFT, ttALEFT]) of
1109-
ttBLEFT :
1110-
if expecting.ClassType=TRecordSymbol then begin
1109+
ttBLEFT : begin
1110+
var expectingClassType := expecting.ClassType;
1111+
if expectingClassType = TRecordSymbol then begin
11111112
Result := ReadConstRecordInitExpr(TRecordSymbol(expecting));
11121113
Exit;
1113-
end else if expecting is TArraySymbol then begin
1114+
end else if expectingClassType.InheritsFrom(TArraySymbol) then begin
11141115
FCompiler.Tokenizer.KillToken;
11151116
Result := ReadArrayConstantExpr(ttBRIGHT, expecting);
11161117
Exit;
11171118
end;
1118-
ttALEFT :
1119-
if expecting is TArraySymbol then begin
1119+
end;
1120+
ttALEFT :begin
1121+
var expectingClassType := expecting.ClassType;
1122+
if expectingClassType.InheritsFrom(TArraySymbol) then begin
11201123
FCompiler.Tokenizer.KillToken;
11211124
Result := ReadArrayConstantExpr(ttARIGHT, expecting);
11221125
Exit;
1123-
end else if expecting is TSetOfSymbol then begin
1126+
end else if expectingClassType = TSetOfSymbol then begin
11241127
FCompiler.Tokenizer.KillToken;
11251128
Result := ReadArrayConstantExpr(ttARIGHT, expecting);
11261129
Result := TConvExpr.WrapWithConvCast(FCompiler.FCompilerContext, FCompiler.Tokenizer.HotPos,
11271130
expecting, Result, CPE_IncompatibleTypes);
11281131
Exit;
11291132
end;
1133+
end;
11301134
end;
11311135
end;
1132-
Result:=ReadExpr(expecting)
1136+
Result := ReadExpr(expecting)
11331137
end;
11341138

11351139
// ------------------

Source/dwsConvExprs.pas

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,14 @@ class function TConvExpr.WrapWithConvCast(context : TdwsCompilerContext; const s
277277

278278
if expr.ClassType = TArrayConstantExpr then begin
279279

280-
arrayConst:=TArrayConstantExpr(expr);
281-
if toTyp is TDynamicArraySymbol then begin
280+
arrayConst := TArrayConstantExpr(expr);
281+
var toTypClass := toTyp.ClassType;
282+
if toTypClass = TDynamicArraySymbol then begin
282283
if (toTyp.Typ.IsOfType(expr.Typ.Typ))
283284
or ((arrayConst.ElementCount=0) and (arrayConst.Typ.Typ.IsOfType(context.TypVariant))) then
284285
Result:=TConvArrayConstantToDynamicExpr.Create(context, scriptPos, arrayConst,
285286
TDynamicArraySymbol(toTyp))
286-
end else if toTyp is TSetOfSymbol then begin
287+
end else if toTypClass = TSetOfSymbol then begin
287288
if arrayConst.ElementCount=0 then begin
288289
Result:=TConstExpr.Create(cNullPos, toTyp);
289290
expr.Free;

Source/dwsSymbols.pas

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,22 +1185,22 @@ TBaseSymbol = class(TTypeSymbol)
11851185
function SpecializeType(const context : ISpecializationContext) : TTypeSymbol; override;
11861186
end;
11871187

1188-
TBaseIntegerSymbol = class (TBaseSymbol)
1188+
TBaseIntegerSymbol = class sealed (TBaseSymbol)
11891189
public
11901190
constructor Create;
11911191

11921192
procedure InitDataContext(const data : IDataContext; offset : NativeInt); override;
11931193
function IsCompatible(typSym : TTypeSymbol) : Boolean; override;
11941194
end;
11951195

1196-
TBaseFloatSymbol = class (TBaseSymbol)
1196+
TBaseFloatSymbol = class sealed (TBaseSymbol)
11971197
public
11981198
constructor Create;
11991199

12001200
procedure InitDataContext(const data : IDataContext; offset : NativeInt); override;
12011201
end;
12021202

1203-
TBaseStringSymbol = class (TBaseSymbol)
1203+
TBaseStringSymbol = class sealed (TBaseSymbol)
12041204
private
12051205
FLengthPseudoSymbol : TPseudoMethodSymbol;
12061206
FHighPseudoSymbol : TPseudoMethodSymbol;
@@ -1220,7 +1220,7 @@ TBaseStringSymbol = class (TBaseSymbol)
12201220
function LowPseudoSymbol(baseSymbols : TdwsBaseSymbolsContext) : TPseudoMethodSymbol; inline;
12211221
end;
12221222

1223-
TBaseBooleanSymbol = class (TBaseSymbol)
1223+
TBaseBooleanSymbol = class sealed (TBaseSymbol)
12241224
public
12251225
constructor Create;
12261226

@@ -6917,7 +6917,9 @@ function TSymbolTable.FindLocal(const aName : String) : TSymbol;
69176917
ptrList := FSymbols.List;
69186918
while lo <= hi do begin
69196919
mid := (lo + hi) shr 1;
6920+
{$IFOPT R+}{$DEFINE RANGEON}{$R-}{$ELSE}{$UNDEF RANGEON}{$ENDIF}
69206921
Result := TSymbol(ptrList[mid]);
6922+
{$IFDEF RANGEON}{$R+}{$UNDEF RANGEON}{$ENDIF}
69216923
cmpResult := UnicodeCompareText(Result.Name, aName);
69226924
if cmpResult < 0 then
69236925
lo := mid+1
@@ -7049,7 +7051,9 @@ function TSymbolTable.EnumerateLocalSymbolsOfName(
70497051
var list := FSymbols.List;
70507052
var nameLen := Length(aName);
70517053
for var i := 0 to Count-1 do begin
7054+
{$IFOPT R+}{$DEFINE RANGEON}{$R-}{$ELSE}{$UNDEF RANGEON}{$ENDIF}
70527055
var sym := TSymbol(list[i]);
7056+
{$IFDEF RANGEON}{$R+}{$UNDEF RANGEON}{$ENDIF}
70537057
if (Length(sym.Name) = nameLen)
70547058
and (
70557059
(nameLen = 0)
@@ -7095,15 +7099,15 @@ function TSymbolTable.EnumerateSymbolsOfNameInScope(const aName : String;
70957099
//
70967100
function TSymbolTable.EnumerateLocalHelpers(helpedType : TTypeSymbol; const callback : THelperSymbolEnumerationCallback) : Boolean;
70977101
var
7098-
i : Integer;
7099-
sym : TSymbol;
71007102
list : PObjectTightList;
71017103
begin
71027104
if stfHasHelpers in FFlags then begin
71037105
list := FSymbols.List;
7104-
for i:=0 to FSymbols.Count-1 do begin
7105-
sym:=TSymbol(list[i]);
7106-
if sym.ClassType=THelperSymbol then
7106+
for var i := 0 to FSymbols.Count-1 do begin
7107+
{$IFOPT R+}{$DEFINE RANGEON}{$R-}{$ELSE}{$UNDEF RANGEON}{$ENDIF}
7108+
var sym := TSymbol(list[i]);
7109+
{$IFDEF RANGEON}{$R+}{$UNDEF RANGEON}{$ENDIF}
7110+
if sym.ClassType = THelperSymbol then
71077111
if THelperSymbol(sym).HelpsType(helpedType) then begin
71087112
if callback(THelperSymbol(sym)) then Exit(True);
71097113
end;

Source/dwsTokenizer.pas

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
interface
2525

2626
uses
27-
SysUtils, Classes, TypInfo,
28-
dwsTokenTypes, dwsScriptSource, dwsErrors, dwsStrings, dwsXPlatform, dwsUtils, dwsXXHash
27+
System.SysUtils, System.Classes, System.TypInfo,
28+
dwsTokenTypes, dwsScriptSource, dwsErrors, dwsUtils
2929
{$ifdef FPC},lazutf8{$endif};
3030

3131
type
@@ -200,13 +200,13 @@ TTokenizerRules = class
200200
end;
201201

202202
TTokenizerSourceInfo = record
203+
FHotPos : TScriptPos;
204+
FCurPos : TScriptPos;
205+
FPosPtr : PChar;
203206
FPathName : TFileName;
204207
FLocation : TFileName;
205208
FText : String;
206209
FDefaultPos : TScriptPos;
207-
FHotPos : TScriptPos;
208-
FCurPos : TScriptPos;
209-
FPosPtr : PChar;
210210
end;
211211
PTokenizerSourceInfo = ^TTokenizerSourceInfo;
212212

@@ -222,23 +222,27 @@ TTokenizerConditionalInfo = record
222222
TTokenizer = class
223223
private
224224
FTokenBuf : TTokenBuffer;
225-
FNextToken : TToken;
226-
FRules : TTokenizerRules;
225+
FOnBeforeAction : TTokenizerActionEvent;
227226
FStartState : TState;
228-
FToken : TToken;
229227
FSource : TTokenizerSourceInfo;
228+
229+
FOnEndSourceFile : TTokenizerEndSourceFileEvent;
230+
231+
FToken : TToken;
232+
FNextToken : TToken;
233+
234+
FRules : TTokenizerRules;
235+
FTokenPool : TToken;
236+
230237
FSwitchHandler : TSwitchHandler;
231238
FSwitchProcessor : TSwitchHandler;
239+
240+
FSourceStack : array of TTokenizerSourceInfo;
241+
232242
FMsgs : TdwsCompileMessageList;
233243
FConditionalDefines : IAutoStrings;
234244
FConditionalDepth : TSimpleStack<TTokenizerConditionalInfo>;
235245

236-
FTokenPool : TToken;
237-
238-
FSourceStack : array of TTokenizerSourceInfo;
239-
FOnEndSourceFile : TTokenizerEndSourceFileEvent;
240-
FOnBeforeAction : TTokenizerActionEvent;
241-
242246
procedure AllocateToken;
243247
procedure ReleaseToken;
244248

@@ -315,6 +319,8 @@ implementation
315319
// ------------------------------------------------------------------
316320
// ------------------------------------------------------------------
317321

322+
uses dwsXPlatform, dwsStrings;
323+
318324
const
319325
cFormatSettings : TFormatSettings = ( DecimalSeparator : {%H-}'.' );
320326

@@ -327,12 +333,14 @@ function TTokenRecord.EmptyString : Boolean;
327333

328334
// AppendChar
329335
//
336+
{$IFOPT R+}{$DEFINE RANGEON}{$R-}{$ELSE}{$UNDEF RANGEON}{$ENDIF}
330337
procedure TTokenBuffer.AppendChar(c : Char);
331338
begin
332-
if Len>=Capacity then Grow;
333-
Buffer[Len]:=c;
339+
if Len >= Capacity then Grow;
340+
Buffer[Len] := c;
334341
Inc(Len);
335342
end;
343+
{$IFDEF RANGEON}{$R+}{$UNDEF RANGEON}{$ENDIF}
336344

337345
// Grow
338346
//
@@ -927,45 +935,42 @@ procedure PrepareAlphaToTokenType;
927935
// UpperMatchLen
928936
//
929937
function TTokenBuffer.UpperMatchLen(const str : String) : Boolean;
930-
var
931-
i : Integer;
932-
p : PChar;
933-
ch : Char;
934938
begin
935-
p:=PChar(Pointer(str));
936-
for i:=1 to Len-1 do begin
937-
ch:=Buffer[i];
939+
var p := PChar(Pointer(str));
940+
for var i := 1 to Len-1 do begin
941+
var ch := Buffer[i];
938942
case ch of
939-
'a'..'z' : if Char(Word(ch) xor $0020)<>p[i] then Exit(False);
943+
'a'..'z' : if Char(Word(ch) xor $0020) <> p[i] then Exit(False);
940944
else
941-
if ch<>p[i] then Exit(False);
945+
if ch <> p[i] then Exit(False);
942946
end;
943947
end;
944-
Result:=True;
948+
Result := True;
945949
end;
946950

947951
// ToAlphaType
948952
//
953+
{$IFOPT R+}{$DEFINE RANGEON}{$R-}{$ELSE}{$UNDEF RANGEON}{$ENDIF}
949954
function TTokenBuffer.ToAlphaType : TTokenType;
950955
var
951-
ch : Char;
952-
i : Integer;
953-
lookups : PTokenAlphaLookups;
956+
lookups : Pointer;
954957
begin
955958
if (Len<2) or (Len>14) then Exit(ttNAME);
956-
ch:=Buffer[0];
959+
var ch := Buffer[0];
957960
case ch of
958-
'a'..'x' : lookups:=@vAlphaToTokenType[Len][Char(Word(ch) xor $0020)];
959-
'A'..'X' : lookups:=@vAlphaToTokenType[Len][ch];
961+
'a'..'x' : lookups := @vAlphaToTokenType[Len][Char(Word(ch) xor $0020)];
962+
'A'..'X' : lookups := @vAlphaToTokenType[Len][ch];
960963
else
961964
Exit(ttNAME);
962965
end;
963-
for i:=0 to High(lookups^) do begin
964-
if UpperMatchLen(lookups^[i].Alpha) then
965-
Exit(lookups^[i].Token);
966+
lookups := PPointer(lookups)^;
967+
for var i := 0 to High(TTokenAlphaLookups(lookups)) do begin
968+
if UpperMatchLen(TTokenAlphaLookups(lookups)[i].Alpha) then
969+
Exit(TTokenAlphaLookups(lookups)[i].Token);
966970
end;
967-
Result:=ttNAME;
971+
Result := ttNAME;
968972
end;
973+
{$IFDEF RANGEON}{$R+}{$UNDEF RANGEON}{$ENDIF}
969974

970975
// MatchLen
971976
//

Source/dwsUtils.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3785,10 +3785,10 @@ function UnicodeCompareLen(p1, p2 : PWideChar; n : Integer) : Integer;
37853785
if c1 <> c2 then begin
37863786
if (c1 <= 127) and (c2 <= 127) then begin
37873787
//if c1 in [Ord('a')..Ord('z')] then
3788-
if Cardinal(c1 - Ord('a')) <= Cardinal(Ord('z') - Ord('a')) then
3788+
if Cardinal(c1 - Ord('a')) <= Cardinal(Ord('z') - Ord('a')) then
37893789
c1 := c1 + (Ord('A')-Ord('a'));
37903790
// if c2 in [Ord('a')..Ord('z')] then
3791-
if Cardinal(c2 - Ord('a')) <= Cardinal(Ord('z') - Ord('a')) then
3791+
if Cardinal(c2 - Ord('a')) <= Cardinal(Ord('z') - Ord('a')) then
37923792
c2 := c2 + (Ord('A')-Ord('a'));
37933793
if c1 <> c2 then begin
37943794
Result := c1 - c2;

0 commit comments

Comments
 (0)