Closed
Description
Description
The JIT is not able to inline this method because of the throw new FormatException("Bad variable size int");
statement with the following error:
generic !!0 (value class System.ReadOnlySpan`1<unsigned int8>,int32,int32&)
Fail Reason: has ldstr VM restriction
A cleanup version of the offending code:
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static T ReadVariableSizeInt<T>(ReadOnlySpan<byte> buffer, int pos, out int offset) where T : unmanaged
{
if (typeof(T) == typeof(int) || typeof(T) == typeof(uint))
{
int ui = 0;
offset = 0;
byte b = buffer[pos + 0];
ui |= (b & 0x7F);
offset++;
if ((b & 0x80) == 0)
goto End;
b = buffer[pos + 1];
ui |= (b & 0x7F) << 7;
offset++;
if ((b & 0x80) == 0)
goto End;
b = buffer[pos + 2];
ui |= (b & 0x7F) << 14;
offset++;
if ((b & 0x80) == 0)
goto End;
b = buffer[pos + 3];
ui |= (b & 0x7F) << 21;
offset++;
if ((b & 0x80) == 0)
goto End;
b = buffer[pos + 4];
ui |= (b & 0x7F) << 28;
offset++;
if ((b & 0x80) != 0)
throw new FormatException("Bad variable size int");
End: return (T)(object)ui;
}
throw new NotSupportedException($"The type {nameof(T)} is not supported to be written.");
}
Configuration
.Net 5.0
Analysis
It sounds to me that because the offending line is a throw statement and is to be marked as cold, that restriction should not apply.