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

来源:互联网 发布:数据库设计 书籍 编辑:程序博客网 时间:2024/05/19 09:12

具体原因不详述了,可以见这里。http://blog.csdn.net/zhou8jie/article/details/50324475
对于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!

0 1
原创粉丝点击