IL2CPPでsealedを付けても速くならない

最終更新日


Unityでclassを定義するときにsealedを付けると高速化すると言う話があります。この話の大本のソースはIL2CPP Optimizations: Devirtualizationだと思います。

では実際にどのくらい速くなるのか計測するために以下のコードで試してみました。記事通りならInvokeNonSealedよりもInvokeSealedの方が高速なはずです。

using NUnit.Framework;
using Unity.PerformanceTesting;

public abstract class Parent
{
    public abstract string Method();
}

public class NonSealedChild : Parent
{
    public override string Method() => nameof(NonSealedChild);
}

public sealed class SealedChild : Parent
{
    public override string Method() => nameof(SealedChild);
}


public class PerformanceTest
{
    [Test, Performance]
    public void InvokeNonSealed()
    {
        var c = new NonSealedChild();

        Measure.Method(() => { c.Method(); })
            .WarmupCount(10)
            .MeasurementCount(100)
            .IterationsPerMeasurement(100000)
            .Run();
    }

    [Test, Performance]
    public void InvokeSealed()
    {
        var c = new SealedChild();

        Measure.Method(() => { c.Method(); })
            .WarmupCount(10)
            .MeasurementCount(100)
            .IterationsPerMeasurement(100000)
            .Run();
    }
}

Unity2020.3.17での計測結果は以下になります。ほとんど変わりません。

IL2CPPでビルドされたC++のコードを直接確認しても、どちらもVirtFuncInvoker0を使って呼ばれているので速くなる理由がありません。

どうしてこうなったかというと、現在のUnityではDevirtualizationが行われていないためです。Unityの方が明言しています

sealedを付けるべきか?

ややこしいことに、UnityのIL2CPPでは高速化しませんが.NET6では高速化されます。Unityでは.NET5を飛ばして.NET6をサポートするつもりではあるようです。なので将来的にはUnityでも高速化される可能性があります。将来のためにも可能な箇所ではsealedを付けておくのが良いと思います。


4件のフィードバック

  1. Its such as you read my thoughts! You seem to understand
    so much about this, such as you wrote the ebook in it or
    something. I believe that you simply can do with a few % to drive the message home a bit, but other than that, this is wonderful blog.
    A great read. I’ll certainly be back.

  2. I do trust all of the ideas you have introduced in your post.
    They are really convincing and can certainly work.
    Still, the posts are too short for novices. Could you please
    prolong them a little from subsequent time? Thanks for the post.

  3. It’s a pity you don’t have a donate button! I’d without a
    doubt donate to this brilliant blog! I suppose for now i’ll settle
    for bookmarking and adding your RSS feed to my Google account.

    I look forward to new updates and will talk about this site with my Facebook group.
    Chat soon!

コメントを残す

メールアドレスが公開されることはありません。

コメントする