码迷,mamicode.com
首页 > 编程语言 > 详细

Unity中使用扩展方法解决foreach导致的GC

时间:2017-01-15 22:37:44      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:ict   ext   protect   表达式   next   地方   https   get   需要   


对于List这种顺序表,我们解决的时候还是可以使用for代替foreach即可。但是对于非顺序表,比如Dictionary或者Set之类,我们可以扩展方法Foreach,ForeachKey和ForeachValue来代替原有的foreach。 
关于扩展方法,可参考:https://msdn.microsoft.com/zh-cn/library/bb383977.aspx

static class DictionaryEx
{
    /// <summary>
    /// 提供一个方法遍历所有项
    /// </summary>
    public static void Foreach<TKey, TValue>(this Dictionary<TKey, TValue> dic, Action<TKey, TValue> action, int maxCount = 1000)
    {
        if (action == null) return;
        var enumerator = dic.GetEnumerator();
        int i = 0;
        while (enumerator.MoveNext() && i++ < maxCount)
        {
            action(enumerator.Current.Key, enumerator.Current.Value);
        }
    }

    /// <summary>
    /// 提供一个方法遍历所有key值
    /// </summary>
    public static void ForeachKey<TKey, TValue>(this Dictionary<TKey, TValue> dic, Action<TKey> action, int maxCount = 1000)
    {
        if (action == null) return;
        var enumerator = dic.GetEnumerator();
        int i = 0;
        while (enumerator.MoveNext() && i++ < maxCount)
        {
            action(enumerator.Current.Key);
        }
    }

    /// <summary>
    /// 提供一个方法遍历所有value值
    /// </summary>
    public static void ForeachValue<TKey, TValue>(this Dictionary<TKey, TValue> dic, Action<TValue> action, int maxCount = 1000)
    {
        if (action == null) return;
        var enumerator = dic.GetEnumerator();
        int i = 0;
        while (enumerator.MoveNext() && i++ < maxCount)
        {
            action(enumerator.Current.Value);
        }
    }
}

在调用时,我们可以使用Lambda表达式简化代码,如:

protected Dictionary<int, ReleaseOnceSkillBunchListShell> m_SkillBunchList = new Dictionary<int, ReleaseOnceSkillBunchListShell>(); // 一个技能列表
    /////////////
    public void Update(float deltaTime)
    {
        // 更新技能列表
        m_SkillBunchList.ForeachValue(
            (value) =>
            {
                value.Update(deltaTime);
            });
    }

本以为这样就解决问题了,结果打开profiler一看,妈蛋,竟然还有GC,仔细对比后发现,原来是lambda表达式的问题。没办法,之后把lambda表达式拆开了,拆完如下。

    protected float m_deltaTime;
    public void Update(float deltaTime)
    {
        m_deltaTime = deltaTime;
        m_SkillBunchList.ForeachValue(OnUpdateSkillBunch);
    }
    public void OnUpdateSkillBunch(ReleaseOnceSkillBunchListShell shell)
    {
        shell.Update(m_deltaTime);
    }

拆完不爽的地方就是,原来作用域里面的deltaTime,需要放到更高层的作用域里,否则不好访问。 
所以,小心了,lambda表达式也会产生GC!

Unity中使用扩展方法解决foreach导致的GC

标签:ict   ext   protect   表达式   next   地方   https   get   需要   

原文地址:http://www.cnblogs.com/lancidie/p/6287952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!