コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】GC Alloc の発生が少ない Linq を使用できる「smooth.foundations」紹介

はじめに

「smooth.foundations」を Unity プロジェクトに導入することで
GC Alloc の発生が少ない Linq を使用できるようになります

使用例

using Smooth.Slinq;
using Smooth.Slinq.Context;
using System.Linq;
using UnityEngine;
using UnityEngine.Profiling;

public class Example : MonoBehaviour
{
    private int m_count;
    private string[] m_list1;
    private Slinq<string, IListContext<string>> m_list2;

    private void Awake()
    {
        m_count = 10000;
        m_list1 = Enumerable
                  .Range( 0, 100 )
                  .Select( c => c.ToString() )
                  .ToArray();
        m_list2 = m_list1.Slinq();
    }

    private void Start()
    {
        var samplerA = CustomSampler.Create( "AAAAA" );
        samplerA.Begin();

        for ( int i = 0; i < m_count; i++ )
        {
            m_list1.Select( c => c );
            m_list1.Any( c => c != null );
        }

        samplerA.End();

        var samplerB = CustomSampler.Create( "BBBBB" );
        samplerB.Begin();

        for ( int i = 0; i < m_count; i++ )
        {
            m_list2.Select( c => c );
            m_list2.Any( c => c != null );
        }

        samplerB.End();
    }
}

f:id:baba_s:20190602203722p:plain

関連記事