.NET获取枚举DescriptionAttribute描述信息性能改进的多种方法

一. DescriptionAttribute的普通使用方式

1.1 使用示例

  DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显示,一个简单的枚举定义:

复制代码
public enum EnumGender
        {
            None,
            [System.ComponentModel.Description("")]
            Male,
            [System.ComponentModel.Description("")]
            Female,
            Other,
        }
复制代码

  本文不讨论DescriptionAttribute的其他应用场景,也不关注多语言的实现,只单纯的研究下获取枚举描述信息的方法。

  一般比较常见的获取枚举描述信息的方法如下,可以在园子里搜索类似的代码非常多。

复制代码
public static string GetDescriptionOriginal(this Enum @this)
        {
            var name = @this.ToString();
            var field = @this.GetType().GetField(name);
            if (field == null) return name;
            var att = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute), false);
            return att == null ? field.Name : ((DescriptionAttribute)att).Description;
        }
复制代码

  简单测试下:

复制代码
Console.WriteLine(EnumGender.Female.GetDescriptionOriginal());
Console.WriteLine(EnumGender.Male.GetDescriptionOriginal());
Console.WriteLine(EnumGender.Other.GetDescriptionOriginal());
//输出结果:

女 
男 
Other

复制代码

1.2 上面的实现代码的问题

  首先要理解特性是什么?

特性

    Attribute特性就是关联了一个目标对象的一段配置信息,存储在dll内的元数据。它本身没什么意义,可以通过反射来获取配置的特性信息。

  因此主要问题其实就是反射造成的严重性能问题:

  • 1.每次调用都会使用反射,效率慢!
  • 2.每次调用反射都会生成新的DescriptionAttribute对象,哪怕是同一个枚举值。造成内存、GC的极大浪费!
  • 3.好像不支持位域组合对象!
  • 4.这个地方的方法参数是Enum,Enum是枚举的基类,他是一个引用类型,而枚举是值类型,该方法会造成装箱,不过这个问题好像是不可避免的。

  性能到底有多差呢?代码来实测一下:

复制代码
        [Test]
        public void GetDescriptionOriginal_Test()
        {
            var enums = this.GetTestEnums();
            Console.WriteLine(enums.Count);
            TestHelper.InvokeAndWriteAll(() =>
            {
                System.Threading.Tasks.Parallel.For(0, 1000000, (i, obj) =>
                {
                    foreach (var item in enums)
                    {
                        var a = item.GetDescriptionOriginal();
                    }
                });
            });
        }

//输出结果:
80
TimeSpan:79,881.0000ms //共消耗了将近80秒
MemoryUsed:-1,652.7970KB
CollectionCount(0):7,990.00  //0代GC回收了7千多次,因为创建了大量的DescriptionAttribute对象
复制代码

  其中this.GetTestEnums();方法使用获取一个枚举值集合,用于测试的,集合大小80,执行100w次,相当于执行了8000w次GetDescriptionOriginal方法。

  TestHelper.InvokeAndWriteAll方法是用来计算执行前后的时间、内存消耗、0代GC回收次数的,文末附录中给出了代码,由于内存回收的原因,内存消耗计算其实不准确的,不过可以参考第三个指标0代GC回收次数。

二. 改进的DescriptionAttribute方法

  知道了问题原因,解决就好办了,基本思路就是把获取到的文本值缓存起来,一个枚举值只反射一次,这样性能问题就解决了。

2.1 使用字典缓存+锁

  因为使用静态变量字典来缓存值,就涉及到线程安全,需要使用锁(做了双重检测),具体方法:

复制代码
private static Dictionary<Enum, string> _LockDictionary = new Dictionary<Enum, string>();
        public static string GetDescriptionByDictionaryWithLocak(this Enum @this)
        {
            if (_LockDictionary.ContainsKey(@this)) return _LockDictionary[@this];
            Monitor.Enter(_obj);
            if (!_LockDictionary.ContainsKey(@this))
            {
                var value = @this.GetDescriptionOriginal();
                _LockDictionary.Add(@this, value);
            }
            Monitor.Exit(_obj);
            return _LockDictionary[@this];
        }
复制代码

  来测试一下,测试数据、次数和1.2的GetDescriptionOriginal_Test相同,效率有很大的提升,只有一次内存回收。

复制代码
[Test]
        public void GetDescriptionByDictionaryWithLocak_Test()
        {
            var enums = this.GetTestEnums();
            Console.WriteLine(enums.Count);
            TestHelper.InvokeAndWriteAll(() =>
            {
                System.Threading.Tasks.Parallel.For(0, 1000000, (i, obj) =>
                {
                    foreach (var item in enums)
                    {
                        var a = item.GetDescriptionByDictionaryWithLocak();
                    }
                });
            });
        }

//测试结果:
80
TimeSpan:1,860.0000ms
MemoryUsed:159.2422KB
CollectionCount(0):1.00
复制代码

2.2 使用字典缓存+异常(不走寻常路的方式)

  还是先看看实现方法吧!

复制代码
private static Dictionary<Enum, string> _ExceptionDictionary = new Dictionary<Enum, string>();
        public static string GetDescriptionByDictionaryWithException(this Enum @this)
        {
            try
            {
                return _ExceptionDictionary[@this];
            }
            catch (KeyNotFoundException)
            {
                Monitor.Enter(_obj);
                if (!_ExceptionDictionary.ContainsKey(@this))
                {
                    var value = @this.GetDescriptionOriginal();
                    _ExceptionDictionary.Add(@this, value);
                }
                Monitor.Exit(_obj);
                return _ExceptionDictionary[@this];
            }
        }
复制代码

  假设我们的使用场景是这样的:项目定义的枚举并不多,但是用其描述值很频繁,比如定义了一个用户性别枚举,用的地方很多,使用频率很高。

  上面GetDescriptionByDictionaryWithLocak的方法中,第一句代码“if (_LockDictionary.ContainsKey(@this)) ”就是验证是否包含枚举值。在2.1的测试中执行了8000w次,其中只有80次(总共只有80个枚举值用于测试)需要这句代码“if (_LockDictionary.ContainsKey(@this)) ”,其余的直接取值就可了。基于这样的考虑,就有了上面的方法GetDescriptionByDictionaryWithException。

  来测试一下,看看效果吧!

复制代码
[Test]
        public void GetDescriptionByDictionaryWithException_Test()
        {
            var enums = this.GetTestEnums();
            Console.WriteLine(enums.Count);
            TestHelper.InvokeAndWriteAll(() =>
            {
                System.Threading.Tasks.Parallel.For(0, 1000000, (i, obj) =>
                {
                    foreach (var item in enums)
                    {
                        var a = item.GetDescriptionByDictionaryWithException();
                    }
                });
            });
        }

//测试结果:
80
TimeSpan:1,208.0000ms
MemoryUsed:230.9453KB
CollectionCount(0):1.00
复制代码

  测试结果来看,基本上差不多,在时间上略微快乐一点点,1,208.0000ms:1,860.0000ms,执行8000w次快600毫秒,好像差别也不大啊,这是为什么呢?

  这个其实就是Dictionary的问题了,Dictionary内部使用散列算法计算存储地址,其查找的时间复杂度为o(1),他的查找效果是非常快的,而本方法中利用了异常处理,异常捕获本身是有一定性能影响的。

2.3 推荐简单方案:ConcurrentDictionary

  ConcurrentDictionary是一个线程安全的字典类,代码:

复制代码
private static ConcurrentDictionary<Enum, string> _ConcurrentDictionary = new ConcurrentDictionary<Enum, string>();
        public static string GetDescriptionByConcurrentDictionary(this Enum @this)
        {
            return _ConcurrentDictionary.GetOrAdd(@this, (key) =>
            {
                var type = key.GetType();
                var field = type.GetField(key.ToString());
                return field == null ? key.ToString() : GetDescription(field);
            });
        }
复制代码

  测试代码及测试结果:

复制代码
[Test]
        public void GetDescriptionByConcurrentDictionary_Test()
        {
            var enums = this.GetTestEnums();
            Console.WriteLine(enums.Count);
            TestHelper.InvokeAndWriteAll(() =>
            {
                System.Threading.Tasks.Parallel.For(0, 1000000, (i, obj) =>
                {
                    foreach (var item in enums)
                    {
                        var a = item.GetDescriptionByConcurrentDictionary();
                    }
                });
            });
        }

//测试结果:
80
TimeSpan:1,303.0000ms
MemoryUsed:198.0859KB
CollectionCount(0):1.00
复制代码

2.4 正式的代码

  综上所述,解决了性能问题、位域枚举问题的正式的代码:

复制代码
/// <summary>
        /// 获取枚举的描述信息(Descripion)。
        /// 支持位域,如果是位域组合值,多个按分隔符组合。
        /// </summary>
        public static string GetDescription(this Enum @this)
        {
            return _ConcurrentDictionary.GetOrAdd(@this, (key) =>
            {
                var type = key.GetType();
                var field = type.GetField(key.ToString());
                //如果field为null则应该是组合位域值,
                return field == null ? key.GetDescriptions() : GetDescription(field);
            });
        }

        /// <summary>
        /// 获取位域枚举的描述,多个按分隔符组合
        /// </summary>
        public static string GetDescriptions(this Enum @this, string separator = ",")
        {
            var names = @this.ToString().Split(',');
            string[] res = new string[names.Length];
            var type = @this.GetType();
            for (int i = 0; i < names.Length; i++)
            {
                var field = type.GetField(names[i].Trim());
                if (field == null) continue;
                res[i] = GetDescription(field);
            }
            return string.Join(separator, res);
        }

        private static string GetDescription(FieldInfo field)
        {
            var att = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute), false);
            return att == null ? field.Name : ((DescriptionAttribute)att).Description;
        }
复制代码

 版权所有,文章来源:http://www.cnblogs.com/anding

个人能力有限,本文内容仅供学习、探讨,欢迎指正、交流。

附录:

1.EnumExtension.cs代码: 

  View Code

2.测试类EnumTest.cs代码: 

  View Code

3.辅助测试类TestHelper.cs

  View Code

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值