XNA/C#: Entity Factories and typeof(T) performance -


in our game (targeted @ mobile) have few different entity types , i'm writing factory/repository handle instantiation of new entities. each concrete entity type has own factory implementation , these factories managed entityrepository.

i'd implement repository such:

repository {  private dictionary <system.type, ientityfactory<ientity>> factorydict;   public t createentity<t> (params) t : ientity  {       return factorydict[typeof(t)].createentity() t;  } } 

usage example

var enemy = repo.createentity<enemy>(); 

but concerned performance, related typeof(t) operation in above. understanding compiler not able determine t's type , have determined @ runtime via reflection, correct? 1 alternative is:

repository {  private dictionary <system.type, ientityfactory> factorydict;   public ientity createentity (system.type type, params)  {       return factorydict[type].createentity();  } } 

which used as

var enemy = (enemy)repo.createentity(typeof(enemy), params); 

in case whenever typeof() called, type on hand , can determined compiler (right?) , performance should better. there noteable difference? other considerations? know can have method such createenemy in repository (we have few entity types) faster prefer keep repository entity-unaware possible.

edit:

i know may not bottleneck, concern such waste use time on reflecting when there less sugared alternative available. , think it's interesting question :)

i did benchmarking proved quite interesting (and seem confirm initial suspicions).

using performance measurement tool found @ http://blogs.msdn.com/b/vancem/archive/2006/09/21/765648.aspx (which runs test method several times , displays metrics such average time etc) conducted basic test, testing:

private static t genfunc<t>() t : class      {          return dict[typeof(t)] t;     } 

against

    private static object paramfunc(system.type type)     {         var d = dict[type];         return d;     } 

called

str = genfunc<string>(); 

vs

str = (string)paramfunc(typeof(string)); 

respectively. paramfunc shows remarkable improvement in performance (executes on average in 60-70% time takes genfunc) test quite rudimentary , might missing few things. how casting performed in generic function.

an interesting aside there little (neglible) performance gained 'caching' type in variable , passing paramfunc vs using typeof() every time.

generics in c# don't use or need reflection.

internally types passed around runtimetypehandle values. , typeof operator maps type.gettypefromhandle (msdn). without looking @ rotor or mono check, expect gettypefromhandle o(1) , fast (eg: array lookup).

so in generic (<t>) case you're passing runtimetypehandle method , calling gettypefromhandle in method. in non-generic case you're calling gettypefromhandle first , passing resultant type method. performance should near identical - , massively outweighed other factors, places you're allocating memory (eg: if you're using params keyword).

but it's factory method anyway. surely won't called more couple of times per second? worth optimising?


Comments

Popular posts from this blog

jQuery Ajax Render Fragments OR Whole Page -

javascript - Iterate over array and calculate average values of array-parts -

java - Simple Command Line calculator -