.NET 4.0 Dictionary<TKey,TValue>: Value type key boxed for a futile 'null' check. Does this hurt performance? -


for example:

.method private hidebysig instance void insert(!tkey key, !tvalue 'value', bool add) cil managed {     .maxstack 3     .locals init (         [0] int32 num,         [1] int32 num2,         [2] int32 num3,         [3] int32 num4)     l_0000: ldarg.1      l_0001: box !tkey     l_0006: brtrue.s l_000e     l_0008: ldc.i4.5      l_0009: call void system.throwhelper::throwargumentnullexception(valuetype system.exceptionargument) 

this internal add method of dictionary<int,object> in .net 4.0. although generics touted helping avoid boxing of value types, why system component doing inefficient check on every operation value-type key? if understand correctly, not hurts performance, return true (a boxed value type never null reference)

edit: summary of marc's answer specific question: reason matters dictionary<k,v> implementation has opted disallow use of "null" nullable<t> instances keys. because msil box instruction gives special treatment nullable<t> value type, check not futile value types.

a nullable<t> struct / value-type, , can null (depending on definition of null; can box null). , not tkey value-type (string being perhaps common tkey).

there requirement here key isn't null; need verify that.

in reality, boxing isn't bad people think; boxed, gen-0 collected. could special-case via generics (like equalitycomparer<t> - via few different sub-classes), seems overkill.

the jit may able remove null-check. may here, although cited, have seen cases null-check beyond jit's ability remove.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -