c# - JIT optimizations for dictionaries with value type keys -
are there optimizations done jitter avoid boxing , reject branches of code cannot taken depending on generic argument is? e.g. in default equality comparer used dictionary<int, tvalue>
comparer implementation genericequalitycomparer<int>
, understand because int
implements iequatable<t>
. equals method of comparer looks this:
public override bool equals(t x, t y) { if ((object) x != null) { if ((object) y != null) return x.equals(y); else return false; } else return (object) y == null; }
what jitted version of method like? in case t value type, can optimised comparing x y, , ignoring comparisons null?
using custom comparer trivial implementation of equals , gethashcode(), worse performance dictionary default comparer! random benchmark noise, difference appeared quite consistent.
public bool equals(int a, int b) { return == b; } public int gethashcode(int obj) { return obj; }
even stranger when created reverse engineered dictionary class recompiling without comparer field entirely, hard-coding integer keys inlining gethashcode-calls key , equals-calls simple equality, performance worse. interpretation of that:
- value types implementing
iequatable<t>
default equality comparer created inside dictionary, performance types suchint
@ least equivalent trivial comparer. - the gethashcode/equals calls done dictionary either irrelevant performance perspective, or inlined call
comparer.gethashcode(tkey key)
replaced integer value @ jit time.
are these assumptions correct?
Comments
Post a Comment