Skip to content

Commit

Permalink
Shrink hello world by 3.2% (#84463)
Browse files Browse the repository at this point in the history
* Get rid of the MethodTable for double/float
* Get rid of any `Array<T>` methods
  • Loading branch information
MichalStrehovsky authored Apr 7, 2023
1 parent 38b81ba commit f18c88d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,28 +251,17 @@ public bool SupportsReflection(Type type)
return true;
}

public static bool IsPrimitiveType(Type type)
=> type == typeof(bool) || type == typeof(char)
|| type == typeof(sbyte) || type == typeof(byte)
|| type == typeof(short) || type == typeof(ushort)
|| type == typeof(int) || type == typeof(uint)
|| type == typeof(long) || type == typeof(ulong)
|| type == typeof(float) || type == typeof(double)
|| type == typeof(nint) || type == typeof(nuint);

internal ExecutionEnvironment ExecutionEnvironment { get; }

internal ReflectionDomainSetup ReflectionDomainSetup { get; }

internal static IEnumerable<Type> PrimitiveTypes => s_primitiveTypes;

private static readonly Type[] s_primitiveTypes =
{
typeof(bool),
typeof(char),
typeof(sbyte),
typeof(byte),
typeof(short),
typeof(ushort),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(float),
typeof(double),
typeof(IntPtr),
typeof(UIntPtr),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -833,14 +833,8 @@ static Type GetLimitedBaseType(RuntimeTypeInfo thisType)
if (baseType == valueType && this != enumType)
{
classification |= TypeClassification.IsValueType;
foreach (Type primitiveType in ExecutionDomain.PrimitiveTypes)
{
if (this.Equals(primitiveType))
{
classification |= TypeClassification.IsPrimitive;
break;
}
}
if (ExecutionDomain.IsPrimitiveType(this))
classification |= TypeClassification.IsPrimitive;
}
}
_lazyClassification = classification;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ public void CopyToAssemblyName(AssemblyName blank)
{
// We must not hand out our own copy of the PKT to AssemblyName as AssemblyName is amazingly trusting and gives untrusted callers
// full freedom to scribble on its PKT array. (As do we but we only have trusted callers!)
byte[] pkCopy = new byte[this.PublicKeyOrToken.Length];
((ICollection<byte>)(this.PublicKeyOrToken)).CopyTo(pkCopy, 0);
var pkCopy = (byte[])this.PublicKeyOrToken.Clone();

if (0 != (this.Flags & AssemblyNameFlags.PublicKey))
blank.SetPublicKey(pkCopy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public static void GetEnumValuesAndNames(MetadataReader reader, TypeDefinitionHa
var handle = field.DefaultValue;
unsortedBoxedValues[i] = handle.HandleType switch
{
HandleType.ConstantSByteValue => (byte)handle.ToConstantSByteValueHandle(reader).GetConstantSByteValue(reader).Value,
HandleType.ConstantSByteValue => (object)(byte)handle.ToConstantSByteValueHandle(reader).GetConstantSByteValue(reader).Value,
HandleType.ConstantByteValue => handle.ToConstantByteValueHandle(reader).GetConstantByteValue(reader).Value,
HandleType.ConstantInt16Value => (ushort)handle.ToConstantInt16ValueHandle(reader).GetConstantInt16Value(reader).Value,
HandleType.ConstantUInt16Value => handle.ToConstantUInt16ValueHandle(reader).GetConstantUInt16Value(reader).Value,
HandleType.ConstantInt32Value => (uint)handle.ToConstantInt32ValueHandle(reader).GetConstantInt32Value(reader).Value,
HandleType.ConstantUInt32Value => handle.ToConstantUInt32ValueHandle(reader).GetConstantUInt32Value(reader).Value,
HandleType.ConstantInt64Value => (ulong)handle.ToConstantInt64ValueHandle(reader).GetConstantInt64Value(reader).Value,
HandleType.ConstantUInt64Value => handle.ToConstantUInt64ValueHandle(reader).GetConstantUInt64Value(reader).Value,
_ => handle.ParseConstantNumericValue(reader), // fallback for unhandled cases
_ => throw new InvalidOperationException(), // unreachable - we would have thrown InvalidOperationException earlier
};
i++;
}
Expand Down

0 comments on commit f18c88d

Please sign in to comment.