Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c12bee7
some funny initial results of the bm of by ref access of Switch vs As…
dadhi May 29, 2025
2bcfb4a
improv AsSpanCompiled for net472
dadhi May 29, 2025
ef6241b
testing Add because the bm throws the exception
dadhi May 30, 2025
1bc3da6
fine
dadhi May 30, 2025
9becea9
replaced SmallList4 with SmallList<T, Stack4<T>>; using ref Indexer i…
dadhi May 31, 2025
f341d8a
move the stuff to the SmallList struct and optimize along the way
dadhi May 31, 2025
bccd35a
remove SmallList2 and 300 loc
dadhi Jun 1, 2025
6cf2d99
generic SmallMap with configured count of items on stack
dadhi Jun 2, 2025
b9f2673
some funny benchmarks
dadhi Jun 2, 2025
5012f9e
small use of GetSurePresentItemRef
dadhi Jun 3, 2025
05665bd
fix the assert with SmallList.RemoveLastSurePresentItem; cleanup
dadhi Jun 3, 2025
680764e
simplify ILReader; make it eager tbd
dadhi Jun 4, 2025
7a47bf8
improv
dadhi Jun 4, 2025
5c31ad3
minimize laziness for ILReader
dadhi Jun 4, 2025
139d658
TEntry type for the SmallMap
dadhi Jun 5, 2025
f9eb067
adding the Set wrappers based on SmallMap
dadhi Jun 5, 2025
0aa32d6
trying Stack TryGetByRef
dadhi Jun 6, 2025
af3e491
trying harder
dadhi Jun 6, 2025
e4fd7fb
ok, type level stuff
dadhi Jun 6, 2025
cdaf72b
fix ci, and thigs
dadhi Jun 6, 2025
6adf2d6
bm out
dadhi Jun 6, 2025
42d990d
spell check galor
dadhi Jun 6, 2025
4ec5f18
@wip adding types to SmallMap
dadhi Jun 6, 2025
a723711
abstractong things @wip
dadhi Jun 8, 2025
32cbe18
using TryGetEntryRef everwhere
dadhi Jun 8, 2025
d994700
nice capacity boundaries
dadhi Jun 8, 2025
3f9a130
@wip tryng the padding
dadhi Jun 8, 2025
d1c6c19
cleanup
dadhi Jun 9, 2025
2ff603c
fixe for the count
dadhi Jun 9, 2025
610063c
adding Size0
dadhi Jun 10, 2025
0f231e9
tbd
dadhi Jun 10, 2025
de17e43
the fix for the small TryGetRef
dadhi Jun 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 49 additions & 20 deletions src/FastExpressionCompiler.LightExpression/Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public virtual bool TryEmit(CompilerFlags flags, ref ClosureInfo closure, IParam
public virtual bool IsCustomToCSharpString => false;

[RequiresUnreferencedCode(Trimming.Message)]
public virtual StringBuilder CustomToCSharpString(StringBuilder sb, EnclosedIn enclosedIn, ref SmallList4<NamedWithIndex> named,
public virtual StringBuilder CustomToCSharpString(StringBuilder sb, EnclosedIn enclosedIn,
ref SmallList<NamedWithIndex, Stack4<NamedWithIndex>> named,
int lineIndent = 0, bool stripNamespace = false, Func<Type, string, string> printType = null, int indentSpaces = 4,
ObjectToCode notRecognizedToCode = null) => sb;

Expand Down Expand Up @@ -1597,7 +1598,7 @@ public static BinaryExpression LessThanOrEqual(Expression left, Expression right

public static BlockExpression Block(IEnumerable<Expression> expressions) => new BlockExpression(expressions.AsReadOnlyList()); // todo: @perf optimize the double copying

public static BlockExpression Block(in SmallList2<Expression> expressions) => new BlockExpression(in expressions);
public static BlockExpression Block(in SmallList<Expression, Stack2<Expression>> expressions) => new BlockExpression(in expressions);

public static BlockExpression Block(IEnumerable<ParameterExpression> variables, Expression expr0) =>
new ManyVariablesBlockExpression(variables.AsReadOnlyList(), expr0); // todo: @perf @mem add the check for empty variables
Expand All @@ -1619,7 +1620,7 @@ public static BlockExpression Block(IEnumerable<ParameterExpression> variables,
public static BlockExpression Block(IEnumerable<ParameterExpression> variables, IEnumerable<Expression> expressions) =>
Block(variables, expressions.AsReadOnlyList());

public static BlockExpression Block(IEnumerable<ParameterExpression> variables, in SmallList2<Expression> expressions)
public static BlockExpression Block(IEnumerable<ParameterExpression> variables, in SmallList<Expression, Stack2<Expression>> expressions)
{
var vars = variables.AsReadOnlyList();
return vars.Count == 0
Expand All @@ -1642,7 +1643,7 @@ public static BlockExpression Block(Type type, IReadOnlyList<Expression> express
public static BlockExpression Block(Type type, IEnumerable<Expression> expressions) =>
new TypedBlockExpression(type, expressions.AsReadOnlyList()); // todo: @perf @mem

public static BlockExpression Block(Type type, in SmallList2<Expression> expressions) =>
public static BlockExpression Block(Type type, in SmallList<Expression, Stack2<Expression>> expressions) =>
new TypedBlockExpression(type, in expressions);

public static BlockExpression Block(Type type, IEnumerable<ParameterExpression> variables, Expression expr0)
Expand All @@ -1669,7 +1670,7 @@ public static BlockExpression Block(Type type, IEnumerable<ParameterExpression>
public static BlockExpression Block(Type type, IEnumerable<ParameterExpression> variables, IEnumerable<Expression> expressions) =>
new TypedManyVariablesBlockExpression(type, variables.AsReadOnlyList(), expressions.AsReadOnlyList()); // todo: @perf @mem

public static BlockExpression Block(Type type, IEnumerable<ParameterExpression> variables, in SmallList2<Expression> expressions) =>
public static BlockExpression Block(Type type, IEnumerable<ParameterExpression> variables, in SmallList<Expression, Stack2<Expression>> expressions) =>
new TypedManyVariablesBlockExpression(type, variables.AsReadOnlyList(), in expressions);

public static BlockExpression MakeBlock(Type type, IEnumerable<ParameterExpression> variables, IEnumerable<Expression> expressions)
Expand Down Expand Up @@ -3352,7 +3353,8 @@ public override bool TryEmit(CompilerFlags flags, ref ClosureInfo closure, IPara

[RequiresUnreferencedCode(Trimming.Message)]
public override StringBuilder CustomToCSharpString(StringBuilder sb,
EnclosedIn enclosedIn, ref SmallList4<NamedWithIndex> named,
EnclosedIn enclosedIn,
ref SmallList<NamedWithIndex, Stack4<NamedWithIndex>> named,
int lineIndent = 0, bool stripNamespace = false, Func<Type, string, string> printType = null, int indentSpaces = 4,
ObjectToCode notRecognizedToCode = null)
{
Expand Down Expand Up @@ -5060,20 +5062,47 @@ public class BlockExpression : Expression, IArgumentProvider
public override ExpressionType NodeType => ExpressionType.Block;
public override Type Type => Result.Type;
public virtual IReadOnlyList<ParameterExpression> Variables => Tools.Empty<ParameterExpression>();
public SmallList2<Expression> Expressions;
public SmallList<Expression, Stack2<Expression>> Expressions;
public Expression Result => Expressions.GetLastSurePresentItem(); // todo: @check what if no expressions?
public virtual int ArgumentCount => 0;
public virtual Expression GetArgument(int index) => throw new NotImplementedException();
internal BlockExpression(in SmallList2<Expression> expressions) =>
internal BlockExpression(in SmallList<Expression, Stack2<Expression>> expressions) =>
Expressions = expressions;
internal BlockExpression(Expression e0) =>
Expressions.Populate1(e0);
internal BlockExpression(Expression e0, Expression e1) =>
Expressions.Populate2(e0, e1);
internal BlockExpression(Expression e0, Expression e1, params Expression[] rest) =>
Expressions.Populate(e0, e1, rest);
internal BlockExpression(IReadOnlyList<Expression> expressions) =>
Expressions.Populate(expressions);
internal BlockExpression(Expression e0)
{
Expressions.Stack._it0 = e0;
Expressions.Count = 1;
}
internal BlockExpression(Expression e0, Expression e1)
{
Expressions.Stack._it0 = e0;
Expressions.Stack._it1 = e1;
Expressions.Count = 2;
}
internal BlockExpression(Expression e0, Expression e1, params Expression[] rest)
{
Expressions.Stack._it0 = e0;
Expressions.Stack._it1 = e1;
Expressions.Rest = rest;
Expressions.Count = rest.Length + 2;
}
internal BlockExpression(IReadOnlyList<Expression> expressions)
{
Debug.Assert(expressions != null, "Expressions cannot be null");
var count = expressions.Count;
if (count >= 1)
Expressions.Stack._it0 = expressions[0];
if (count >= 2)
Expressions.Stack._it1 = expressions[1];
if (count >= 3)
{
var rest = new Expression[count - 2];
for (var i = 2; i < count; ++i)
rest[i - 2] = expressions[i];
Expressions.Rest = rest;
}
Expressions.Count = count;
}
#if SUPPORTS_VISITOR
[RequiresUnreferencedCode(Trimming.Message)]
protected internal override Expression Accept(ExpressionVisitor visitor) => visitor.VisitBlock(this);
Expand All @@ -5088,7 +5117,7 @@ internal override SysExpr CreateSysExpression(ref SmallList<LightAndSysExpr> exp
public sealed class TypedBlockExpression : BlockExpression
{
public override Type Type { get; }
internal TypedBlockExpression(Type type, in SmallList2<Expression> expressions) : base(in expressions) => Type = type;
internal TypedBlockExpression(Type type, in SmallList<Expression, Stack2<Expression>> expressions) : base(in expressions) => Type = type;
internal TypedBlockExpression(Type type, Expression e0) : base(e0) => Type = type;
internal TypedBlockExpression(Type type, Expression e0, Expression e1) : base(e0, e1) => Type = type;
internal TypedBlockExpression(Type type, Expression e0, Expression e1, params Expression[] rest) : base(e0, e1, rest) => Type = type;
Expand All @@ -5100,7 +5129,7 @@ public class ManyVariablesBlockExpression : BlockExpression
public sealed override IReadOnlyList<ParameterExpression> Variables { get; }
public sealed override int ArgumentCount => Expressions.Count;
public sealed override Expression GetArgument(int index) => Expressions.GetSurePresentItemRef(index);
internal ManyVariablesBlockExpression(IReadOnlyList<ParameterExpression> variables, in SmallList2<Expression> expressions) : base(in expressions) =>
internal ManyVariablesBlockExpression(IReadOnlyList<ParameterExpression> variables, in SmallList<Expression, Stack2<Expression>> expressions) : base(in expressions) =>
Variables = variables;
internal ManyVariablesBlockExpression(IReadOnlyList<ParameterExpression> variables, Expression e0) : base(e0) =>
Variables = variables;
Expand All @@ -5115,7 +5144,7 @@ internal ManyVariablesBlockExpression(IReadOnlyList<ParameterExpression> variabl
public sealed class TypedManyVariablesBlockExpression : ManyVariablesBlockExpression
{
public override Type Type { get; }
internal TypedManyVariablesBlockExpression(Type type, IReadOnlyList<ParameterExpression> variables, in SmallList2<Expression> expressions)
internal TypedManyVariablesBlockExpression(Type type, IReadOnlyList<ParameterExpression> variables, in SmallList<Expression, Stack2<Expression>> expressions)
: base(variables, in expressions) => Type = type;
internal TypedManyVariablesBlockExpression(Type type, IReadOnlyList<ParameterExpression> variables, Expression e0)
: base(variables, e0) => Type = type;
Expand Down Expand Up @@ -5283,7 +5312,7 @@ internal System.Linq.Expressions.LabelTarget ToSystemLabelTarget(ref SmallList<L
Justification = "The method is used for debugging purposes only.")]
public override string ToString()
{
SmallList4<NamedWithIndex> named = default;
SmallList<NamedWithIndex, Stack4<NamedWithIndex>> named = default;
return new StringBuilder().AppendLabelName(this, ref named).ToString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public IReadOnlyList<Expression> Visit(IReadOnlyList<Expression> nodes)
return newNodes ?? nodes;
}

public IReadOnlyList<Expression> Visit(SmallList2<Expression> nodes)
public IReadOnlyList<Expression> Visit(SmallList<Expression, Stack2<Expression>> nodes)
{
var newNodes = new Expression[nodes.Count];
for (var i = 0; i < nodes.Count; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,5 @@
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0"/>
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0"/>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'net472' ">
<!-- For the Span -->
<PackageReference Include="System.Memory" Version="4.6.3"/>
</ItemGroup>

</Project>
Loading