Q1. delegate性能比Luafunction好,为什么?
void Start()
{
LuaManager.GetInstance().Init();
LuaManager.GetInstance().DoLuaFile("main");
Stopwatch sw = new Stopwatch();
sw.Start();
delegateRunTime();
long times1 = sw.ElapsedMilliseconds;
sw.Restart();
luaFunctionRunTime();
long times2 = sw.ElapsedMilliseconds;
sw.Stop();
UnityEngine.Debug.Log("T1: " + times1 + " T2: " + times2);
}
void delegateRunTime(){
for(int i=0;i<10000;i++){
CustomCall1 customCall1 = LuaManager.GetInstance().Global.Get<CustomCall1>("testFunc1");
customCall1();
}
}
void luaFunctionRunTime(){
for(int i=0;i<10000;i++){
LuaFunction luaFunction= LuaManager.GetInstance().Global.Get<LuaFunction>("testFunc1");
luaFunction.Call();
}
}
对于无参无返回的lua函数,输出了下运行时间,发现相差无几(其实也阅读了下源码,也没有发现性能不一样的地方)

是因为delegrate bridge是弱表吗?弱表代表可以被GC回收,但是luafunction不行。
if (delegateType == null)
{
delegate_bridges[reference] = new WeakReference(bridge);
return bridge;
}