Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AggressiveInlining doesn't exist

Tags:

c#

unity3d

I was trying to implement OpenSimplex Noise https://gist.github.com/digitalshadow/134a3a02b67cecd72181 in my game, but I'm getting the error "'MethodImplOptions' does not contain a definition for 'AggressiveInlining'".

[ MethodImpl(MethodImplOptions.AggressiveInlining)]
    private static int FastFloor(double x)
    {
        var xi = (int)x;
        return x < xi ? xi - 1 : xi;
    }

Yes, I included using System.Runtime.CompilerServices;. Now, I'll be perfectly honest, I don't know how inlining works, so I don't even know if it's necessary for a program in Unity. If I don't need it, I would like to know what to use instead. Thanks in advance.

like image 708
itsjustmejerk Avatar asked Dec 14 '22 23:12

itsjustmejerk


1 Answers

The MethodImplOptions.AggressiveInlining enum does not exist because this feature was added in .NET 4.5 while Unity is still using .NET 2.0/3.5.

I don't even know if it's necessary for a program in Unity

It's not necessary but it seems to speed up function calling. You can see a speed test here.

I would like to know what to use instead

You can use [MethodImpl(256)] instead of MethodImplOptions.AggressiveInlining. That should work too.

Why do you think it should work? I'm curious about that. Any sources?

Look at the source code for the MethodImplOptions enum below:

public enum MethodImplOptions {
        Unmanaged = 4,
        ForwardRef = 16,
        InternalCall = 4096,
        Synchronized = 32,
        NoInlining = 8,
        PreserveSig = 128,
        NoOptimization = 64,
#if NET_4_5
        AggressiveInlining  = 256,
#endif
    } // MethodImplOptions

As you can see, AggressiveInlining has a value of 256. When you use the MethodImplAttribute construction overload with the int parameter, you can essentially use any enum value that does not exist. Passing it 256 should give you AggressiveInlining on any .NET version. The new enum value is simply there to make it easier to remember/use.

Another source is from Unity Engineer:

Little .NET tip: use [MethodImpl(256)] instead of MethodImplOptions.AggressiveInlining to compile on any .NET versions and avoid ifdefs

like image 63
Programmer Avatar answered Dec 25 '22 07:12

Programmer