-
Notifications
You must be signed in to change notification settings - Fork 134
Performance optimizations #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,9 +121,6 @@ TOperatorInfo = record | |
|
|
||
| TOperators = class | ||
| strict private | ||
| class var FOps: TDictionary<TSyntaxNodeType, TOperatorInfo>; | ||
| class constructor Create; | ||
| class destructor Destroy; | ||
| class function GetItem(Typ: TSyntaxNodeType): TOperatorInfo; static; | ||
| public | ||
| class function IsOpName(Typ: TSyntaxNodeType): Boolean; | ||
|
|
@@ -163,29 +160,23 @@ TOperators = class | |
|
|
||
| { TOperators } | ||
|
|
||
| class constructor TOperators.Create; | ||
| var | ||
| I: Integer; | ||
| begin | ||
| FOps := TDictionary<TSyntaxNodeType, TOperatorInfo>.Create; | ||
|
|
||
| for I := Low(OperatorsInfo) to High(OperatorsInfo) do | ||
| FOps.Add(OperatorsInfo[I].Typ, OperatorsInfo[I]); | ||
| end; | ||
|
|
||
| class destructor TOperators.Destroy; | ||
| begin | ||
| FOps.Free; | ||
| end; | ||
|
|
||
| class function TOperators.GetItem(Typ: TSyntaxNodeType): TOperatorInfo; | ||
| var | ||
| i: Integer; | ||
| begin | ||
| Result := FOps[Typ]; | ||
| for i := 0 to High(OperatorsInfo) do | ||
| if OperatorsInfo[i].Typ = Typ then | ||
| Exit(OperatorsInfo[i]); | ||
| end; | ||
|
|
||
| class function TOperators.IsOpName(Typ: TSyntaxNodeType): Boolean; | ||
| var | ||
| i: Integer; | ||
| begin | ||
| Result := FOps.ContainsKey(Typ); | ||
| for i := 0 to High(OperatorsInfo) do | ||
| if OperatorsInfo[i].Typ = Typ then | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks very similar to the code above (which isn't the best, as if you modify one, you need to remember to modify the other).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly needs to be modified here? You only need to extend the const OperatorsInfo if a new operator would ever be introduced to the language. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking DRY, that's all. |
||
| Exit(True); | ||
| Result := False; | ||
| end; | ||
|
|
||
| function IsRoundClose(Typ: TSyntaxNodeType): Boolean; inline; | ||
|
|
@@ -362,31 +353,30 @@ class procedure TExpressionTools.RawNodeListToTree(RawParentNode: TSyntaxNode; R | |
| procedure TSyntaxNode.SetAttribute(const Key: TAttributeName; const Value: string); | ||
| var | ||
| AttributeEntry: PAttributeEntry; | ||
| NewAttributeEntry: TAttributeEntry; | ||
| len: Integer; | ||
| begin | ||
| if TryGetAttributeEntry(Key, AttributeEntry) then | ||
| AttributeEntry^.Value := Value | ||
| else | ||
| if not TryGetAttributeEntry(Key, AttributeEntry) then | ||
| begin | ||
| NewAttributeEntry.Key := Key; | ||
| NewAttributeEntry.Value := Value; | ||
| SetLength(FAttributes, Length(FAttributes) + 1); | ||
| FAttributes[Length(FAttributes) - 1] := NewAttributeEntry; | ||
| len := Length(FAttributes); | ||
| SetLength(FAttributes, len + 1); | ||
| AttributeEntry := @FAttributes[len]; | ||
| AttributeEntry^.Key := Key; | ||
| end; | ||
| AttributeEntry^.Value := Value; | ||
| end; | ||
|
|
||
| function TSyntaxNode.TryGetAttributeEntry(const Key: TAttributeName; var AttributeEntry: PAttributeEntry): boolean; | ||
| var | ||
| i: integer; | ||
| begin | ||
| for i := 0 to Length(FAttributes) - 1 do | ||
| for i := 0 to High(FAttributes) do | ||
| if FAttributes[i].Key = Key then | ||
| begin | ||
| AttributeEntry := @FAttributes[i]; | ||
| Exit(true); | ||
| Exit(True); | ||
| end; | ||
|
|
||
| Exit(false); | ||
| Result := False; | ||
| end; | ||
|
|
||
| function TSyntaxNode.AddChild(Node: TSyntaxNode): TSyntaxNode; | ||
|
|
@@ -408,16 +398,18 @@ function TSyntaxNode.AddChild(Typ: TSyntaxNodeType): TSyntaxNode; | |
|
|
||
| function TSyntaxNode.Clone: TSyntaxNode; | ||
| var | ||
| ChildNode: TSyntaxNode; | ||
| Attr: TPair<TAttributeName, string>; | ||
| i: Integer; | ||
| begin | ||
| Result := TSyntaxNodeClass(Self.ClassType).Create(FTyp); | ||
|
|
||
| for ChildNode in FChildNodes do | ||
| Result.AddChild(ChildNode.Clone); | ||
| SetLength(Result.FChildNodes, Length(FChildNodes)); | ||
| for i := 0 to High(FChildNodes) do | ||
| begin | ||
| Result.FChildNodes[i] := FChildNodes[i].Clone; | ||
| Result.FChildNodes[i].FParentNode := Result; | ||
| end; | ||
|
|
||
| for Attr in FAttributes do | ||
| Result.SetAttribute(Attr.Key, Attr.Value); | ||
| Result.FAttributes := Copy(FAttributes); | ||
|
|
||
| Result.Col := Self.Col; | ||
| Result.Line := Self.Line; | ||
|
|
@@ -428,29 +420,20 @@ constructor TSyntaxNode.Create(Typ: TSyntaxNodeType); | |
| begin | ||
| inherited Create; | ||
| FTyp := Typ; | ||
| SetLength(FAttributes, 0); | ||
| SetLength(FChildNodes, 0); | ||
| FParentNode := nil; | ||
| end; | ||
|
|
||
| procedure TSyntaxNode.ExtractChild(Node: TSyntaxNode); | ||
| var | ||
| NodeIndex, i: integer; | ||
| i: integer; | ||
| begin | ||
| NodeIndex := -1; | ||
| for i := 0 to Length(FChildNodes) - 1 do | ||
| for i := 0 to High(FChildNodes) do | ||
| if FChildNodes[i] = Node then | ||
| begin | ||
| NodeIndex := i; | ||
| break; | ||
| if i < High(FChildNodes) then | ||
| Move(FChildNodes[i + 1], FChildNodes[i], SizeOf(TSyntaxNode) * (Length(FChildNodes) - i - 1)); | ||
| SetLength(FChildNodes, High(FChildNodes)); | ||
| Break; | ||
| end; | ||
|
|
||
| if NodeIndex >= 0 then | ||
| begin | ||
| if NodeIndex < High(FChildNodes) then | ||
| Move(FChildNodes[NodeIndex + 1], FChildNodes[NodeIndex], SizeOf(FChildNodes[0]) * (Length(FChildNodes) - NodeIndex - 1)); | ||
| SetLength(FChildNodes, Length(FChildNodes) - 1); | ||
| end; | ||
| end; | ||
|
|
||
| procedure TSyntaxNode.DeleteChild(Node: TSyntaxNode); | ||
|
|
@@ -463,25 +446,19 @@ destructor TSyntaxNode.Destroy; | |
| var | ||
| i: integer; | ||
| begin | ||
| for i := 0 to Length(FChildNodes) - 1 do | ||
| FChildNodes[i].Free; | ||
| SetLength(FChildNodes, 0); | ||
|
|
||
| SetLength(FAttributes, 0); | ||
| for i := 0 to High(FChildNodes) do | ||
| FreeAndNil(FChildNodes[i]); | ||
| inherited; | ||
| end; | ||
|
|
||
| function TSyntaxNode.FindNode(Typ: TSyntaxNodeType): TSyntaxNode; | ||
| var | ||
| Node: TSyntaxNode; | ||
| i: Integer; | ||
| begin | ||
| for i := 0 to High(FChildNodes) do | ||
| if FChildNodes[i].Typ = Typ then | ||
| Exit(FChildNodes[i]); | ||
| Result := nil; | ||
| for Node in FChildNodes do | ||
| if Node.Typ = Typ then | ||
| begin | ||
| Result := Node; | ||
| Break; | ||
| end; | ||
| end; | ||
|
|
||
| function TSyntaxNode.GetAttribute(const Key: TAttributeName): string; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any way "Typ" is not in the OperationsInfo array? If so, what is being returned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might raise an exception in that case just like the dictionary did. But that would only happen if there were a new operator introduced to the language as all the existing operators are in the OperatorsInfo array.