In our color types, we excessively use static functions to do conversion operations, such as FloatFromByte, ByteFromFloat or ByteFromFloatClamped. Inlining them would have nice potential for optimization. Calling the static functions directly and adding the AggressiveInlining attribute might already be sufficient, or maybe this can also be somehow implemented in the code generator.
Here is a quick benchmark I've created:
[SimpleJob(RuntimeMoniker.Net48)]
public class ColFunc
{
C3b[] _cols;
[GlobalSetup]
public void Init()
{
var rnd = new RandomSystem(0);
_cols = new C3b[1000].SetByIndex(i => rnd.UniformC3f().ToC3b());
}
[Benchmark]
public C3f ToC3fFunc()
{
C3f sum = C3f.Black;
foreach (var x in _cols)
sum += x.ToC3f();
return sum;
}
private const float c_byteToFloat = 1.0f / 255.0f;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public C3f ToC3fInline(C3b c)
{
return new C3f(
c_byteToFloat * (float)c.R,
c_byteToFloat * (float)c.G,
c_byteToFloat * (float)c.B);
}
[Benchmark]
public C3f ToC3fInline()
{
C3f sum = C3f.Black;
foreach (var x in _cols)
sum += ToC3fInline(x);
return sum;
}
}
In our color types, we excessively use static functions to do conversion operations, such as
FloatFromByte
,ByteFromFloat
orByteFromFloatClamped
. Inlining them would have nice potential for optimization. Calling the static functions directly and adding the AggressiveInlining attribute might already be sufficient, or maybe this can also be somehow implemented in the code generator.Here is a quick benchmark I've created: