hazzik / DelegateDecompiler

A library which is able to decompile a delegate or a method body to its lambda representation
MIT License
522 stars 62 forks source link

Do not consider final method overridable #223

Closed billybraga closed 1 month ago

billybraga commented 8 months ago

As noted here, "To determine if a method is overridable, it is not sufficient to check that IsVirtual is true. For a method to be overridable, IsVirtual must be true and IsFinal must be false."

I may be wrong, but I think this PR may increase performance and keep functionnality intact.

(By the way, I submitted another PR by mistake earlier, trying to update my fork, and it seems to have linked a bunch of stuff to it, sorry)

I did a small benchmark:

BenchmarkRunner.Run<Benchmark>();
return;

public interface I
{
    string? P { get; set; }
}

public class C : I
{
    public string? P { get; set; }
}

public class Benchmark
{
    static readonly Type Type = typeof(C);

    static readonly MethodInfo MethodInfo = Type.GetProperty(nameof(C.P))?.GetMethod
        ?? throw new ArgumentException("Did not find method");

    [Benchmark]
    public void DecompileNew()
    {
        MethodBodyDecompiler.Decompile(MethodInfo, Type);
    }

    [Benchmark]
    public void DecompileOld()
    {
        MethodBodyDecompiler.DecompileOld(MethodInfo, Type);
    }
}
| Method       | Mean         | Error      | StdDev      |
|------------- |-------------:|-----------:|------------:|
| DecompileNew |     3.030 us |  0.0871 us |   0.2554 us |
| DecompileOld | 2,292.221 us | 60.4400 us | 175.3475 us |
billybraga commented 2 months ago

@hazzik Could this be merged?