From 0480f1e9841d731937d5bf468ff24821a0126fdf Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sat, 5 Mar 2011 21:53:23 +1300 Subject: [PATCH] Avoid constructing a delegate on every TraitDict query -- saving 250M/min --- OpenRA.Game/TraitDictionary.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/OpenRA.Game/TraitDictionary.cs b/OpenRA.Game/TraitDictionary.cs index 7d995c9a90..0524cbb765 100755 --- a/OpenRA.Game/TraitDictionary.cs +++ b/OpenRA.Game/TraitDictionary.cs @@ -17,17 +17,19 @@ namespace OpenRA { class TraitDictionary { - Dictionary traits = new Dictionary(); - + Dictionary traits = new Dictionary(); + ITraitContainer InnerGet( Type t ) { - return traits.GetOrAdd( t, CreateTraitContainer ); - } - + return traits.GetOrAdd( t, doCreateTraitContainer ); + } + + // construct this delegate once. + static Func doCreateTraitContainer = CreateTraitContainer; static ITraitContainer CreateTraitContainer( Type t ) { return (ITraitContainer)typeof( TraitContainer<> ).MakeGenericType( t ) - .GetConstructor( new Type[ 0 ] ).Invoke( new object[ 0 ] ); + .GetConstructor( Type.EmptyTypes ).Invoke( null ); } public void AddTrait( Actor actor, object val )