优化Lua以进行循环执行 [英] Optimizing Lua for cyclic execution

查看:152
本文介绍了优化Lua以进行循环执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每个10毫秒的程序周期执行一次Lua脚本.使用相同的Lua_state(在我的应用中一次调用过luaL_newstate)

I'm exeucting my Lua script once per program cycle of 10 ms. using the same Lua_state (luaL_newstate called once in my app)

可以肯定的是,调用luaL_loadbuffer可以非常快速地编译脚本,但由于脚本不会更改,因此每次执行脚本时似乎都不需要这样做.

Calling luaL_loadbuffer complies the script very fast for sure, still it seems unneccessary to do this every time the script is executed since the script does not change.

试图使用lua_dump()保存二进制文件然后执行它,但是lua_pcall()由于某种原因不接受二进制文件.

Tried to save binary using lua_dump() and then execute it, but lua_pcall() didn't accept the binary for some reason.

关于如何优化的任何想法? (在这里,LuaJIT并不是一个永久的选择)

Any ideas on how to optimize? (LuaJIT is not an unfortenately an option here)

Jan

推荐答案

您是正确的,如果代码没有更改,则没有理由重新处理代码.也许您可以执行以下操作:

You're correct, if the code is not changing, there is no reason to reprocess the code. Perhaps you could do something like the following:

luaL_loadbuffer(state, buff, len, name); // TODO:  check return value
while (true) {
    // sleep 10ms
    lua_pushvalue(state, -1); // make another reference to the loaded chunk
    lua_call(state, 0, 0);
}

您会注意到,我们只是在堆栈顶部复制了函数引用,因为lua_call从堆栈中删除了它调用的函数.这样,您就不会丢失对已加载块的引用.

You'll note that we simply duplicate the function reference on the top of the stack, since lua_call removes the function that it calls from the stack. This way, you do not lose a reference to the loaded chunk.

这篇关于优化Lua以进行循环执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆