Skip to content

Commit

Permalink
Merge pull request #24 from metaindu/perf
Browse files Browse the repository at this point in the history
Perf
  • Loading branch information
izrik committed Nov 11, 2021
2 parents c2dcb4d + d3c4792 commit 324df7d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
11 changes: 8 additions & 3 deletions Expressions/FunctionCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,16 @@ public void GatherMatchingFunctionCalls(ICollection<FunctionCall> matchingFuncti
}
}

private IMathObject[] _evaledArgsCache = new IMathObject[0];
// Warning: Not thread-safe
public virtual IMathObject Call(SolusEnvironment env)
{
var evaledArgs =
Arguments.Select(a => a.Eval(env)).ToArray();
return Function.Call(env, evaledArgs);
if (_evaledArgsCache.Length < Arguments.Count)
_evaledArgsCache = new IMathObject[Arguments.Count];
int i;
for (i = 0; i < Arguments.Count; i++)
_evaledArgsCache[i] = Arguments[i].Eval(env);
return Function.Call(env, _evaledArgsCache);
}

public virtual List<Expression> Arguments
Expand Down
2 changes: 1 addition & 1 deletion Expressions/Literal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public virtual IMathObject Value
if (_value != value)
{
_value = value;
this.OnValueChanged(new EventArgs());
this.OnValueChanged(EventArgs.Empty);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Expressions/VariableAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override IMathObject Eval(SolusEnvironment env)
if (env.ContainsVariable(var))
{
if (env.GetVariable(var) is Literal literal)
return literal.Value.ToNumber();
return literal.Value;

return env.GetVariable(var).Eval(env);
}
Expand Down
30 changes: 21 additions & 9 deletions Expressions/VectorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public override IMathObject Eval(SolusEnvironment env)
var values = new IMathObject[Length];
for (int i = 0; i < Length; i++)
values[i] = this[i].Eval(env);
return new Vector(values);
// Vector will take ownership of array
return new Vector(values); // TODO: don't box here
}

public override Expression Clone()
Expand Down Expand Up @@ -214,19 +215,30 @@ public override void AcceptVisitor(IExpressionVisitor visitor)
}
}

private Expression[] _valuesCache = null;
public override Expression PreliminaryEval(SolusEnvironment env)
{
var values = _array.Select(
e => e.PreliminaryEval(env)).ToArray();
if (values.All(e => e is Literal))
if (_valuesCache == null ||
_valuesCache.Length < _array.Length)
_valuesCache = new Expression[_array.Length];

bool allLiterals = true;
int i;
for (i = 0; i < _array.Length; i++)
{
var e = _valuesCache[i] = _array[i].PreliminaryEval(env);
allLiterals &= e is Literal;
}
if (allLiterals)
{
return new Literal(
new Vector(
values.Select(
e => ((Literal) e).Value).ToArray()));
var values = new IMathObject[_valuesCache.Length];
for (i = 0; i < _array.Length; i++)
values[i] = ((Literal)_valuesCache[i]).Value;
// Vector will take ownership of array
return new Literal(new Vector(values));
}

return new VectorExpression(values.Length, values);
return new VectorExpression(_valuesCache.Length, _valuesCache);
}

public override void ApplyToAll(Modulator mod)
Expand Down
1 change: 1 addition & 0 deletions Values/Vector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace MetaphysicsIndustries.Solus.Values
{
public Vector(IMathObject[] components)
{
// TODO: don't clone here
_components = (IMathObject[]) components.Clone();

var componentType = _components[0].GetMathType();
Expand Down

0 comments on commit 324df7d

Please sign in to comment.