TL DR . Can I somehow create an algorithm that can use different functionality in the inner loop and still use this βfunctionalityβ without resorting to copy / paste or if / else statements
I am trying to create an algorithm that basically looks like this:
for(var i=0; i<big; i++) {
for(var j=0; j<big2; j++) {
var x = SomeFunc(a, b, c);
}
}
I want the algorithm to run for a number of possible functions ( SomeFuncabove), each of which will call a large number of times for each run. Each function is SomeFuncvery simple (usually an arithmetic expression).
Now, in order to get acceptable performance from this algorithm, it needs to SomeFuncbe built-in. However, I cannot get the inlined function, but at the same time I allow several functions.
I understand this means that the function of the algorithm should be JITted several times, but I was hoping that such a construction would work:
interface ISomeFunc {
int SomeFunc(int a, int b, int c);
}
private sealed class SomeFunc1 : ISomeFunc {
public int SomeFunc(int a, int b, int c) {
return ....;
}
}
private static void RunMyGenericAlgo<T>(T func) where T : ISomeFunc
{
for ... for ..
x = func.SomeFunc(a, b, c);
}
But it seems that the function call is not built-in, since the funcabove is called through the interface, and not through a private class.
I also tried to make an obvious approach:
abstract class MyAlgo {
protected abstract int SomeFunc(int a, int b, int c);
public void Run() {
}
}
sealed class MyAlgoSomeFunc1 : MyAlgo {
protected override int SomeFunc(int a, int b, int c) {...}
}
and he was also not included.
This program, however, will be included as desired (and will work about 50% faster):
class MyAlgo {
int SomeFunc(int a, int b, int c) {...}
public void Run() {
}
}
EDIT. To clarify , I also explored using the MethodImplc attribute AggressiveInlining, and it didn't seem to help.
# / ?