Skip to content

Commit

Permalink
Cache metamethods in each LuaRuntime.
Browse files Browse the repository at this point in the history
This reflection is expensive, so by allowing it to be cached against each runtime we can remove most of the cost. Since each runtime is likely to only deal with a handful of custom objects, it shouldn't be an issue for memory usage.
  • Loading branch information
RoosterDragon committed Dec 29, 2015
1 parent 2630107 commit 2c0ef03
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Eluant/LuaClrObjectValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override string ToString()

internal abstract object BackingCustomObject { get; }

internal abstract MetamethodAttribute[] BackingCustomObjectMetamethods { get; }
internal abstract MetamethodAttribute[] BackingCustomObjectMetamethods(LuaRuntime runtime);

static internal MetamethodAttribute[] Metamethods(Type backingCustomObjectType)
{
Expand Down
4 changes: 2 additions & 2 deletions Eluant/LuaCustomClrObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ internal override object BackingCustomObject

private MetamethodAttribute[] metamethods;

internal override MetamethodAttribute[] BackingCustomObjectMetamethods
internal override MetamethodAttribute[] BackingCustomObjectMetamethods(LuaRuntime runtime)
{
get { return metamethods ?? (metamethods = Metamethods(BackingCustomObject.GetType())); }
return metamethods ?? (metamethods = runtime.CachedMetamethods(BackingCustomObject.GetType()));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Eluant/LuaOpaqueClrObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ internal override object BackingCustomObject
get { return null; }
}

internal override MetamethodAttribute[] BackingCustomObjectMetamethods
internal override MetamethodAttribute[] BackingCustomObjectMetamethods(LuaRuntime runtime)
{
get { return null; }
return null;
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion Eluant/LuaRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ protected GCHandle SelfHandle
private const string OPAQUECLROBJECT_METATABLE = "eluant_opaqueclrobject";

private Dictionary<string, LuaFunction> metamethodCallbacks = new Dictionary<string, LuaFunction>();
private Dictionary<Type, MetamethodAttribute[]> metamethodAttributes = new Dictionary<Type, MetamethodAttribute[]>();

private LuaFunction createManagedCallWrapper;

Expand Down Expand Up @@ -778,7 +779,7 @@ internal void PushCustomClrObject(LuaClrObjectValue obj)
LuaApi.lua_settable(LuaState, -3);

// For all others, we use MetamethodAttribute on the interface to make this code less repetitive.
foreach (var metamethod in obj.BackingCustomObjectMetamethods) {
foreach (var metamethod in obj.BackingCustomObjectMetamethods(this)) {
LuaApi.lua_pushstring(LuaState, metamethod.MethodName);
Push(metamethodCallbacks[metamethod.MethodName]);
LuaApi.lua_settable(LuaState, -3);
Expand All @@ -791,6 +792,19 @@ internal void PushCustomClrObject(LuaClrObjectValue obj)
}
}

internal MetamethodAttribute[] CachedMetamethods(Type backingCustomObjectType)
{
MetamethodAttribute[] metamethods;
if (metamethodAttributes.TryGetValue(backingCustomObjectType, out metamethods)) {
return metamethods;
}

metamethods = LuaClrObjectValue.Metamethods(backingCustomObjectType);
metamethodAttributes.Add(backingCustomObjectType, metamethods);

return metamethods;
}

private int NewindexCallback(IntPtr state)
{
return LuaToClrBoundary(state, toDispose => {
Expand Down
4 changes: 2 additions & 2 deletions Eluant/LuaTransparentClrObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ internal override object BackingCustomObject
get { return proxy; }
}

internal override MetamethodAttribute[] BackingCustomObjectMetamethods
internal override MetamethodAttribute[] BackingCustomObjectMetamethods(LuaRuntime runtime)
{
get { return TransparentClrObjectProxy.Metamethods; }
return TransparentClrObjectProxy.Metamethods;
}

private class TransparentClrObjectProxy : ILuaTableBinding, ILuaEqualityBinding
Expand Down

0 comments on commit 2c0ef03

Please sign in to comment.