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



    对于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!

  • 相关阅读:
    ECharts
    JavaScript实现数组去重方法
    面试题1
    Vue中的8种组件通信方式
    渐进式web应用 (PWA)
    单元测试(Jest 和 Mocha)
    axios
    Vue Router
    Vuex
    hash 模式与 history 模式小记
  • 原文地址:https://www.cnblogs.com/lancidie/p/6287952.html
Copyright © 2020-2023  润新知