Skip to content

Commit b1874d8

Browse files
authored
refactor: inline small flat constants
1 parent 14f0c05 commit b1874d8

1 file changed

Lines changed: 23 additions & 70 deletions

File tree

src/FastExpressionCompiler.LightExpression/FlatExpression.cs

Lines changed: 23 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public int Constant(object value, Type type)
288288

289289
/// <summary>Adds an <see cref="int"/> constant node.</summary>
290290
[MethodImpl(MethodImplOptions.AggressiveInlining)]
291-
public int ConstantInt(int value) => AddRawExpressionNode(typeof(int), value, ExpressionType.Constant);
291+
public int ConstantInt(int value) => AddInlineConstantNode(typeof(int), unchecked((uint)value));
292292

293293
/// <summary>Adds a typed constant node.</summary>
294294
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -2012,37 +2012,27 @@ private static bool AreConstantsEqual(ref ExprTree xTree, ref ExprNode x, ref Ex
20122012
{
20132013
var xObj = GetStoredConstantValue(ref xTree, ref x);
20142014
var yObj = GetStoredConstantValue(ref yTree, ref y);
2015-
if (!ReferenceEquals(x.Obj, ExprNode.InlineValueMarker) && !ReferenceEquals(y.Obj, ExprNode.InlineValueMarker))
2015+
var xInline = ReferenceEquals(x.Obj, ExprNode.InlineValueMarker);
2016+
var yInline = ReferenceEquals(y.Obj, ExprNode.InlineValueMarker);
2017+
if (!(xInline && yInline))
20162018
return ReferenceEquals(xObj, yObj) || Equals(xObj, yObj);
20172019

20182020
if (x.Type.IsEnum)
20192021
{
2020-
if (ReferenceEquals(x.Obj, ExprNode.InlineValueMarker) && ReferenceEquals(y.Obj, ExprNode.InlineValueMarker))
2021-
return x.InlineValue == y.InlineValue;
2022-
return Type.GetTypeCode(Enum.GetUnderlyingType(x.Type)) switch
2023-
{
2024-
TypeCode.Byte => GetInlineOrConvertedByte(ref xTree, ref x) == GetInlineOrConvertedByte(ref yTree, ref y),
2025-
TypeCode.SByte => GetInlineOrConvertedSByte(ref xTree, ref x) == GetInlineOrConvertedSByte(ref yTree, ref y),
2026-
TypeCode.Char => GetInlineOrConvertedChar(ref xTree, ref x) == GetInlineOrConvertedChar(ref yTree, ref y),
2027-
TypeCode.Int16 => GetInlineOrConvertedInt16(ref xTree, ref x) == GetInlineOrConvertedInt16(ref yTree, ref y),
2028-
TypeCode.UInt16 => GetInlineOrConvertedUInt16(ref xTree, ref x) == GetInlineOrConvertedUInt16(ref yTree, ref y),
2029-
TypeCode.Int32 => GetInlineOrConvertedInt32(ref xTree, ref x) == GetInlineOrConvertedInt32(ref yTree, ref y),
2030-
TypeCode.UInt32 => GetInlineOrConvertedUInt32(ref xTree, ref x) == GetInlineOrConvertedUInt32(ref yTree, ref y),
2031-
var tc => FlatExpressionThrow.UnsupportedInlineConstantType<bool>(x.Type, tc)
2032-
};
2022+
return x.InlineValue == y.InlineValue;
20332023
}
20342024

20352025
return Type.GetTypeCode(x.Type) switch
20362026
{
2037-
TypeCode.Boolean => GetInlineOrStoredBoolean(ref xTree, ref x) == GetInlineOrStoredBoolean(ref yTree, ref y),
2038-
TypeCode.Byte => GetInlineOrStoredByte(ref xTree, ref x) == GetInlineOrStoredByte(ref yTree, ref y),
2039-
TypeCode.SByte => GetInlineOrStoredSByte(ref xTree, ref x) == GetInlineOrStoredSByte(ref yTree, ref y),
2040-
TypeCode.Char => GetInlineOrStoredChar(ref xTree, ref x) == GetInlineOrStoredChar(ref yTree, ref y),
2041-
TypeCode.Int16 => GetInlineOrStoredInt16(ref xTree, ref x) == GetInlineOrStoredInt16(ref yTree, ref y),
2042-
TypeCode.UInt16 => GetInlineOrStoredUInt16(ref xTree, ref x) == GetInlineOrStoredUInt16(ref yTree, ref y),
2043-
TypeCode.Int32 => GetInlineOrStoredInt32(ref xTree, ref x) == GetInlineOrStoredInt32(ref yTree, ref y),
2044-
TypeCode.UInt32 => GetInlineOrStoredUInt32(ref xTree, ref x) == GetInlineOrStoredUInt32(ref yTree, ref y),
2045-
TypeCode.Single => GetInlineOrStoredSingle(ref xTree, ref x).Equals(GetInlineOrStoredSingle(ref yTree, ref y)),
2027+
TypeCode.Boolean => GetInlineBoolean(ref x) == GetInlineBoolean(ref y),
2028+
TypeCode.Byte => GetInlineByte(ref x) == GetInlineByte(ref y),
2029+
TypeCode.SByte => GetInlineSByte(ref x) == GetInlineSByte(ref y),
2030+
TypeCode.Char => GetInlineChar(ref x) == GetInlineChar(ref y),
2031+
TypeCode.Int16 => GetInlineInt16(ref x) == GetInlineInt16(ref y),
2032+
TypeCode.UInt16 => GetInlineUInt16(ref x) == GetInlineUInt16(ref y),
2033+
TypeCode.Int32 => GetInlineInt32(ref x) == GetInlineInt32(ref y),
2034+
TypeCode.UInt32 => GetInlineUInt32(ref x) == GetInlineUInt32(ref y),
2035+
TypeCode.Single => GetInlineSingle(ref x).Equals(GetInlineSingle(ref y)),
20462036
_ => ReferenceEquals(xObj, yObj) || Equals(xObj, yObj)
20472037
};
20482038
}
@@ -2081,68 +2071,31 @@ private static int GetInlineConstantHashCode(Type type, uint data)
20812071
}
20822072

20832073
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2084-
private static bool GetInlineOrStoredBoolean(ref ExprTree tree, ref ExprNode node) =>
2085-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? node.InlineValue != 0 : (bool)GetStoredConstantValue(ref tree, ref node);
2086-
2087-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2088-
private static byte GetInlineOrStoredByte(ref ExprTree tree, ref ExprNode node) =>
2089-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (byte)node.InlineValue : (byte)GetStoredConstantValue(ref tree, ref node);
2090-
2091-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2092-
private static sbyte GetInlineOrStoredSByte(ref ExprTree tree, ref ExprNode node) =>
2093-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (sbyte)(byte)node.InlineValue : (sbyte)GetStoredConstantValue(ref tree, ref node);
2094-
2095-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2096-
private static char GetInlineOrStoredChar(ref ExprTree tree, ref ExprNode node) =>
2097-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (char)(ushort)node.InlineValue : (char)GetStoredConstantValue(ref tree, ref node);
2098-
2099-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2100-
private static short GetInlineOrStoredInt16(ref ExprTree tree, ref ExprNode node) =>
2101-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (short)(ushort)node.InlineValue : (short)GetStoredConstantValue(ref tree, ref node);
2102-
2103-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2104-
private static ushort GetInlineOrStoredUInt16(ref ExprTree tree, ref ExprNode node) =>
2105-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (ushort)node.InlineValue : (ushort)GetStoredConstantValue(ref tree, ref node);
2106-
2107-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2108-
private static int GetInlineOrStoredInt32(ref ExprTree tree, ref ExprNode node) =>
2109-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (int)node.InlineValue : (int)GetStoredConstantValue(ref tree, ref node);
2110-
2111-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2112-
private static uint GetInlineOrStoredUInt32(ref ExprTree tree, ref ExprNode node) =>
2113-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? node.InlineValue : (uint)GetStoredConstantValue(ref tree, ref node);
2074+
private static bool GetInlineBoolean(ref ExprNode node) => node.InlineValue != 0;
21142075

21152076
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2116-
private static float GetInlineOrStoredSingle(ref ExprTree tree, ref ExprNode node) =>
2117-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? FloatBits.ToFloat(node.InlineValue) : (float)GetStoredConstantValue(ref tree, ref node);
2077+
private static byte GetInlineByte(ref ExprNode node) => (byte)node.InlineValue;
21182078

21192079
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2120-
private static byte GetInlineOrConvertedByte(ref ExprTree tree, ref ExprNode node) =>
2121-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (byte)node.InlineValue : System.Convert.ToByte(GetStoredConstantValue(ref tree, ref node));
2080+
private static sbyte GetInlineSByte(ref ExprNode node) => (sbyte)(byte)node.InlineValue;
21222081

21232082
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2124-
private static sbyte GetInlineOrConvertedSByte(ref ExprTree tree, ref ExprNode node) =>
2125-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (sbyte)(byte)node.InlineValue : System.Convert.ToSByte(GetStoredConstantValue(ref tree, ref node));
2083+
private static char GetInlineChar(ref ExprNode node) => (char)(ushort)node.InlineValue;
21262084

21272085
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2128-
private static char GetInlineOrConvertedChar(ref ExprTree tree, ref ExprNode node) =>
2129-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (char)(ushort)node.InlineValue : System.Convert.ToChar(GetStoredConstantValue(ref tree, ref node));
2086+
private static short GetInlineInt16(ref ExprNode node) => (short)(ushort)node.InlineValue;
21302087

21312088
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2132-
private static short GetInlineOrConvertedInt16(ref ExprTree tree, ref ExprNode node) =>
2133-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (short)(ushort)node.InlineValue : System.Convert.ToInt16(GetStoredConstantValue(ref tree, ref node));
2089+
private static ushort GetInlineUInt16(ref ExprNode node) => (ushort)node.InlineValue;
21342090

21352091
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2136-
private static ushort GetInlineOrConvertedUInt16(ref ExprTree tree, ref ExprNode node) =>
2137-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (ushort)node.InlineValue : System.Convert.ToUInt16(GetStoredConstantValue(ref tree, ref node));
2092+
private static int GetInlineInt32(ref ExprNode node) => (int)node.InlineValue;
21382093

21392094
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2140-
private static int GetInlineOrConvertedInt32(ref ExprTree tree, ref ExprNode node) =>
2141-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? (int)node.InlineValue : System.Convert.ToInt32(GetStoredConstantValue(ref tree, ref node));
2095+
private static uint GetInlineUInt32(ref ExprNode node) => node.InlineValue;
21422096

21432097
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2144-
private static uint GetInlineOrConvertedUInt32(ref ExprTree tree, ref ExprNode node) =>
2145-
ReferenceEquals(node.Obj, ExprNode.InlineValueMarker) ? node.InlineValue : System.Convert.ToUInt32(GetStoredConstantValue(ref tree, ref node));
2098+
private static float GetInlineSingle(ref ExprNode node) => FloatBits.ToFloat(node.InlineValue);
21462099

21472100
private struct TraversalFrame
21482101
{

0 commit comments

Comments
 (0)