Skip to content

Commit 9b2c972

Browse files
committed
reduce allocations
1 parent 751de7f commit 9b2c972

File tree

18 files changed

+47
-44
lines changed

18 files changed

+47
-44
lines changed

benchmarks/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ Apple M1 Pro, 1 CPU, 10 logical and 10 physical cores
1111
```
1212
| Method | Job | Runtime | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
1313
|--------|---------------|---------------|---------:|----------:|----------:|----------:|---------:|----------:|
14-
| Invoke | .NET 9.0 | .NET 9.0 | 9.496 ms | 0.1434 ms | 0.1341 ms | 1625.0000 | 546.8750 | 9.79 MB |
15-
| Invoke | NativeAOT 9.0 | NativeAOT 9.0 | 7.418 ms | 0.0217 ms | 0.0203 ms | 1632.8125 | 453.1250 | 9.78 MB |
14+
| Invoke | .NET 9.0 | .NET 9.0 | 7.427 ms | 0.1477 ms | 0.1701 ms | 1593.7500 | 406.2500 | 9.57 MB |
15+
| Invoke | NativeAOT 9.0 | NativeAOT 9.0 | 7.215 ms | 0.0836 ms | 0.0699 ms | 1593.7500 | 453.1250 | 9.56 MB |

src/Application/HydraScript.Application.CodeGeneration/Visitors/InstructionProvider.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryExpressions;
1212
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Statements;
1313
using Microsoft.Extensions.DependencyInjection;
14-
using ZLinq;
1514

1615
namespace HydraScript.Application.CodeGeneration.Visitors;
1716

@@ -169,9 +168,9 @@ public AddressedInstructions Visit(WhileStatement visitable)
169168
}
170169

171170
result.AddRange(visitable.Statement.Accept(This));
172-
result.AsValueEnumerable()
173-
.OfType<Goto>().Where(g => g.JumpType is not null)
174-
.ToList().ForEach(g =>
171+
for (var address = result.Start; address != null; address = address?.Next)
172+
{
173+
if (result[address] is Goto { JumpType: not null } g)
175174
{
176175
// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
177176
switch (g.JumpType)
@@ -183,7 +182,8 @@ public AddressedInstructions Visit(WhileStatement visitable)
183182
g.SetJump(startBlockLabel);
184183
break;
185184
}
186-
});
185+
}
186+
}
187187
result.Add(new Goto(startBlockLabel));
188188

189189
result.Add(new EndBlock(BlockType.Loop, blockId), endBlockLabel.Name);
@@ -222,9 +222,11 @@ public AddressedInstructions Visit(IfStatement visitable)
222222
if (visitable.HasElseBlock())
223223
result.AddRange(visitable.Else?.Accept(This) ?? []);
224224

225-
result.AsValueEnumerable()
226-
.OfType<Goto>().Where(g => g.JumpType is InsideStatementJumpType.Break)
227-
.ToList().ForEach(g => g.SetJump(endBlockLabel));
225+
for (var address = result.Start; address != null; address = address?.Next)
226+
{
227+
if (result[address] is Goto { JumpType: InsideStatementJumpType.Break } g)
228+
g.SetJump(endBlockLabel);
229+
}
228230

229231
result.Add(new EndBlock(BlockType.Condition, blockId), endBlockLabel.Name);
230232

src/Application/HydraScript.Application.StaticAnalysis/Impl/FunctionWithUndefinedReturnStorage.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ public FunctionDeclaration Get(FunctionSymbol symbol)
2121

2222
public void RemoveIfPresent(FunctionSymbol symbol) => _declarations.Remove(symbol.Id);
2323

24-
public IEnumerable<FunctionDeclaration> Flush() => _declarations.Keys.ToList()
25-
.Select(x =>
24+
public IEnumerable<FunctionDeclaration> Flush()
25+
{
26+
IReadOnlyList<FunctionSymbolId> keys = _declarations.Keys;
27+
while (keys.Count > 0)
2628
{
27-
var decl = _declarations[x];
28-
_declarations.Remove(x);
29-
return decl;
30-
});
29+
yield return _declarations[keys[0]];
30+
_declarations.Remove(keys[0]);
31+
}
32+
}
3133
}

src/Application/HydraScript.Application.StaticAnalysis/Visitors/SemanticChecker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public Type Visit(BinaryExpression visitable)
256256
"++" when lType is ArrayType { Type: Any } && rType is ArrayType { Type: Any } =>
257257
throw new CannotDefineType(visitable.Segment),
258258
"++" => lType is ArrayType lArrType && rType is ArrayType rArrType
259-
? new List<ArrayType> { lArrType, rArrType }.First(x => x.Type is not Any)
259+
? lArrType.Type is Any ? rArrType.Type : lArrType.Type
260260
: throw new UnsupportedOperation(visitable.Segment, lType, visitable.Operator),
261261
"::" when lType is not ArrayType =>
262262
throw new UnsupportedOperation(visitable.Segment, lType, visitable.Operator),
@@ -464,7 +464,7 @@ public Type Visit(CallExpression visitable)
464464

465465
public Type Visit(FunctionDeclaration visitable)
466466
{
467-
var parameters = visitable.Arguments.Select(x => x.TypeValue.Accept(_typeBuilder)).ToList();
467+
var parameters = visitable.Arguments.Select(x => x.TypeValue.Accept(_typeBuilder));
468468
var symbol = _symbolTables[visitable.Scope].FindSymbol(new FunctionSymbolId(visitable.Name, parameters))!;
469469
_functionStorage.RemoveIfPresent(symbol);
470470
visitable.Statements.Accept(This);

src/Domain/HydraScript.Domain.BackEnd/AddressedInstructions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ private void AddWithAddress(IExecutableInstruction instruction, IAddress newAddr
5454

5555
public void AddRange(AddressedInstructions instructions)
5656
{
57-
// ReSharper disable once ConstantConditionalAccessQualifier
5857
for (var address = instructions.Start; address != null; address = address?.Next)
5958
{
6059
AddWithAddress(instructions[address], address);

src/Domain/HydraScript.Domain.BackEnd/Impl/Instructions/WithAssignment/ComplexData/Create/CreateArray.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ public class CreateArray(string id, int size) : Simple(id)
77
public override IAddress Execute(IExecuteParams executeParams)
88
{
99
var frame = executeParams.Frames.Peek();
10-
frame[_id] = new object[size].ToList();
10+
var list = new List<object>(size * 2);
11+
for (var i = 0; i < size; i++)
12+
list.Add(null!);
13+
frame[_id] = list;
1114
return Address.Next;
1215
}
1316

src/Domain/HydraScript.Domain.FrontEnd/Lexer/Impl/TextCoordinateSystemComputer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public IReadOnlyList<int> GetLines(string text)
2727
indices.Add(start + index);
2828
}
2929

30-
return indices.ToList();
30+
return indices;
3131
}
3232

3333
/// <inheritdoc/>

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Declarations/TypeValue.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public override string ToString() =>
3636
}
3737

3838
[AutoVisitable<TypeValue>]
39-
public partial record ObjectTypeValue(
40-
IEnumerable<PropertyTypeValue> Properties) : TypeValue
39+
public partial record ObjectTypeValue(List<PropertyTypeValue> Properties) : TypeValue
4140
{
4241
public override string ToString() =>
4342
$"{{{ZString.Join(';', Properties)}}}";

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/CallExpression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public partial class CallExpression : LeftHandSideExpression
1818

1919
public string ComputedFunctionAddress { get; set; } = default!;
2020

21-
public CallExpression(MemberExpression member, IEnumerable<Expression> expressions)
21+
public CallExpression(MemberExpression member, List<Expression> expressions)
2222
{
2323
Member = member;
2424
Member.Parent = this;
2525

26-
_parameters = new List<Expression>(expressions);
26+
_parameters = expressions;
2727
_parameters.ForEach(expr => expr.Parent = this);
2828
}
2929

src/Domain/HydraScript.Domain.FrontEnd/Parser/Impl/Ast/Nodes/Expressions/ComplexLiterals/ArrayLiteral.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public partial class ArrayLiteral : ComplexLiteral
1010

1111
public IReadOnlyList<Expression> Expressions => _expressions;
1212

13-
public ArrayLiteral(IEnumerable<Expression> expressions)
13+
public ArrayLiteral(List<Expression> expressions)
1414
{
15-
_expressions = new List<Expression>(expressions);
15+
_expressions = expressions;
1616
_expressions.ForEach(expr => expr.Parent = this);
1717
}
1818

0 commit comments

Comments
 (0)