Cache VariableExpressions on load.

Compiling these expressions is sadly expensive, and we needed new ones for every trait on every actor each time one was generated. The expressions thankfully can be shared as they are pure functions, which removes this overhead.
This commit is contained in:
RoosterDragon
2017-12-16 14:34:38 +00:00
committed by reaperrr
parent bf21fc5213
commit de38313579

View File

@@ -71,6 +71,11 @@ namespace OpenRA
static readonly ConcurrentCache<MemberInfo, bool> MemberHasTranslateAttribute = static readonly ConcurrentCache<MemberInfo, bool> MemberHasTranslateAttribute =
new ConcurrentCache<MemberInfo, bool>(member => member.HasAttribute<TranslateAttribute>()); new ConcurrentCache<MemberInfo, bool>(member => member.HasAttribute<TranslateAttribute>());
static readonly ConcurrentCache<string, BooleanExpression> BooleanExpressionCache =
new ConcurrentCache<string, BooleanExpression>(expression => new BooleanExpression(expression));
static readonly ConcurrentCache<string, IntegerExpression> IntegerExpressionCache =
new ConcurrentCache<string, IntegerExpression>(expression => new IntegerExpression(expression));
static readonly object TranslationsLock = new object(); static readonly object TranslationsLock = new object();
static Dictionary<string, string> translations; static Dictionary<string, string> translations;
@@ -408,7 +413,7 @@ namespace OpenRA
{ {
try try
{ {
return new BooleanExpression(value); return BooleanExpressionCache[value];
} }
catch (InvalidDataException e) catch (InvalidDataException e)
{ {
@@ -424,7 +429,7 @@ namespace OpenRA
{ {
try try
{ {
return new IntegerExpression(value); return IntegerExpressionCache[value];
} }
catch (InvalidDataException e) catch (InvalidDataException e)
{ {