C#里结构体是值类型,其局部变量的空间分配在栈上。不少同窗喜欢用它,是由于它的存储密度高、分配和回收成本很是低。html
可是前几天在查热点的时候,却碰到结构体的性能很是慢,甚至远低于把一样数据结构作成的引用类型。下文对这个问题作了些简化,方便你们理解。性能优化
优化前的源代码示例:数据结构
//结构体声明 public struct Point2D { public int X { get; set; } public int Y { get; set; } } var target = new Point2D() { X = 99, Y = 100 }; //热点语句,points 是一个有几百万元素的链表: foreach(var item in point2Ds) { if (item.Equals(target)) return target; }
优化方法很简单,就是在Point2D的结构体声明中,加一个手写的Equals方法:ide
//优化后: public struct Point2D { public int X { get; set; } public int Y { get; set; } public bool Equals(Point2D obj) { return obj.X == this.X && obj.Y == this.Y; } }
构造一个有1千万元素的points。微服务
优化前的执行时间(单位:ms)性能
优化后的执行时间(单位:ms)测试
先后提高差很少50%。优化
查看IL能够发现,优化后是调用的Point2D.Equals方法,也就是咱们写的方法:this
而优化前的IL以下,是调用Object.Equals方法:spa
那么,这二者有什么区别呢?
能够查看一下struct的Equals方法。因为struct是值类型,它从ValueType继承来,所以Equals方法实际是执行的ValueType.Equals。
源码地址:https://referencesource.microsoft.com/mscorlib/system/valuetype.cs.html
public abstract class ValueType { [System.Security.SecuritySafeCritical] public override bool Equals (Object obj) { BCLDebug.Perf(false, "ValueType::Equals is not fast. "+this.GetType().FullName+" should override Equals(Object)"); if (null==obj) { return false; } RuntimeType thisType = (RuntimeType)this.GetType(); RuntimeType thatType = (RuntimeType)obj.GetType(); if (thatType!=thisType) { return false; } Object thisObj = (Object)this; Object thisResult, thatResult; // if there are no GC references in this object we can avoid reflection // and do a fast memcmp if (CanCompareBits(this)) return FastEqualsCheck(thisObj, obj); FieldInfo[] thisFields = thisType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); for (int i=0; i<thisFields.Length; i++) { thisResult = ((RtFieldInfo)thisFields[i]).UnsafeGetValue(thisObj); thatResult = ((RtFieldInfo)thisFields[i]).UnsafeGetValue(obj); if (thisResult == null) { if (thatResult != null) return false; } else if (!thisResult.Equals(thatResult)) { return false; } } return true; } }
能够发现,ValueType.Equals方法并非直接比较的二者引用地址是否相等,而是递归遍历struct的每一个字段,判断它们是否相等。而在遍历struct字段时,使用了反射取值,这是很耗性能的。
另外,因为其参数是Object类型,会把传入的struct作一次装箱,这也是热点。
而咱们写的方法,是直接对比属性,并且传入参数是Point2D类型,也不用装箱,能够直接使用。
总结一下,在使用结构体的时候,避免装箱,重写Equals方法避免原Equals的反射。
性能优化相关文章: