Можно ли создать idenitity делегата, чтобы отличить его от другого делегата? Подумайте об этом коде:
Func<int, int, int> delegate1 = a, b => a + b;
Func<int, int, int> delegate2 = a, b => a + b;
Func<int, int, int> delegate3 = a, b => a - b;
let id1 = id(delegate1);
let id2 = id(delegate2);
let id3 = id(delegate3);
Assert(id1 == id2);
Assert(id1 != id3);
Проблема, которую я хочу решить, я хочу кэшировать некоторый JIT-компилированный графический код в .NET. Чтобы упростить его использование, я хочу, чтобы пользователь послал делегата, и если делегат тот же, мы пытаемся выяснить код графического процессора из кеша, а не компилировать его каждый раз:
Parallel(outputA, inputA1, inputA2, a, b => a + b); //should JIT compile GPU code, and cache it by its identity
Parallel(outputB, inputB1, inputB2, a, b => a + b); //should NOT JIT compile GPU code, and use the cached GPU code by its identity
Одним из возможных решений является сравнение строки выражения, но у нее все еще есть проблема, чтобы поймать кузнец, например:
int c = 0;
Expression<Func<int, int, int>> delegate1 = (a, b) => a + b + c;
c += 1;
Expression<Func<int, int, int>> delegate2 = (a, b) => a + b + c;
Expression<Func<int, int, int>> delegate3 = (a, b) => a - b - c;
Console.WriteLine(delegate1);
Console.WriteLine(delegate2);
Console.WriteLine(delegate1.ToString() == delegate2.ToString());
Console.ReadKey();
Как указано в @SWeko и @Luaan, в приведенном выше примере delegate1
и delegate2
на самом деле одинаковы. Но цель кэширования делегата заключается в следующем использовании:
int c = 1;
Parallel(outputA, inputA1, inputA2, (a,b) => a+b); //do JIT compile of GPU code
c += 1;
Parallel(outputB, inputB1, inputB2, (a,b) => a+b); //as the delegate is same then the previouse one, it will not do JIT GPU code compiling, but then, that is wrong!