前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >正确lua简单的扩展,可以加速相关C++数据。

正确lua简单的扩展,可以加速相关C++数据。

作者头像
全栈程序员站长
发布2022-07-06 09:26:20
5090
发布2022-07-06 09:26:20
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

很早的时候,我有一件事纠结。如果,我在这里C++打开界面脚本。使用C++其中一个目标,和。我的程序有很多不同的lua虚拟机。每个虚拟机与一个关联C++对象,它是多线程,那么如何快速应利用这个好时机lua_State针来定位到对象指针呢?

曾经我没有能力读懂lua的源代码,也能够说不知道关键部分怎样操作,我当时的做法。是利用临界区和std::map来解决这个问题的。非常明显这个方式的效率非常低非常低。

如今有能力读lua源代码了。当然有更有效的解决的方法了。由于在我们利用lua的过程中。lua_State这个结构指针是要贯穿全部用到lua的地方的,那么我就行对这个结构进行扩展,让它可以保存我的数据,仅仅须要保存一个指针就可以。

lua_State这个结构,定义在 lstate.h中 (lua.h中仅仅是作者为了不让用户可以主动訪问结构成员而定义的空结构指针。各种开源脚本引擎都是这样,为了安全性。大家懂的)

以lua5.2.3为例,该结构原始定义例如以下:

struct lua_State { CommonHeader; lu_byte status; StkId top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ const Instruction *oldpc; /* last pc traced */ StkId stack_last; /* last free slot in the stack */ StkId stack; /* stack base */ int stacksize; unsigned short nny; /* number of non-yieldable calls in stack */ unsigned short nCcalls; /* number of nested C calls */ lu_byte hookmask; lu_byte allowhook; int basehookcount; int hookcount; lua_Hook hook; GCObject *openupval; /* list of open upvalues in this stack */ GCObject *gclist; struct lua_longjmp *errorJmp; /* current error recover point */ ptrdiff_t errfunc; /* current error handling function (stack index) */ CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ };

那么对这个结构扩展之后例如以下:

struct lua_State { CommonHeader; lu_byte status; StkId top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ const Instruction *oldpc; /* last pc traced */ StkId stack_last; /* last free slot in the stack */ StkId stack; /* stack base */ int stacksize; unsigned short nny; /* number of non-yieldable calls in stack */ unsigned short nCcalls; /* number of nested C calls */ lu_byte hookmask; lu_byte allowhook; int basehookcount; int hookcount; lua_Hook hook; GCObject *openupval; /* list of open upvalues in this stack */ GCObject *gclist; struct lua_longjmp *errorJmp; /* current error recover point */ ptrdiff_t errfunc; /* current error handling function (stack index) */ CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ int __mydata;//这里 };

//同一时候添加两个lua接口,能够将函数接口放到lapi.c中,声明放到lua.h中就可以,或者你是发烧追求极限效率不在乎很多其它的扩展和更新的朋友,那么你能够用硬编码定位,__mydata的偏移是0x70。

LUA_API void lua_setmydata(lua_State *L, int data){ L->__mydata = data; }

LUA_API int lua_getmydata(lua_State *L){ return L->__mydata; }

这样就万事具备了,又一次编译lua,试试结果怎样:

正确lua简单的扩展,可以加速相关C++数据。
正确lua简单的扩展,可以加速相关C++数据。

更抽象一点的做法:

正确lua简单的扩展,可以加速相关C++数据。
正确lua简单的扩展,可以加速相关C++数据。

使用硬编码进行定位:

正确lua简单的扩展,可以加速相关C++数据。
正确lua简单的扩展,可以加速相关C++数据。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117401.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月6,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档