A simple extension of the correct LUA can speed up the relevant C + + data.

Source: Internet
Author: User
Tags lua

Very early on, I had one thing tangled up. If, I am here C + + Open Interface script. Use one of the C + + goals, and. My program has a lot of different LUA virtual machines. Each virtual machine with an associated C + + object, which is multi-threaded, how quickly should you use this good time lua_state pin to locate the object pointer?


I did not have the ability to read the source code of LUA, and I was able to say that I did not know how to operate the key parts. is to use critical areas and std::map to solve this problem. It is very obvious that the efficiency of this approach is very low and very low.


Now has the ability to read LUA source code. Of course there is a more effective way to solve it. Because in the course of our use of LUA. Lua_state This structure pointer is going to go through all the places where Lua is used, then I can extend this structure so that it can save my data and just need to save a pointer.


Lua_state This structure, defined in lstate.h (Lua.h is simply an empty structure pointer defined by the author in order not to allow the user to proactively access the struct members. This is true for various open source scripting engines, for security. Everyone knows)


In the case of lua5.2.3, the original definition of the structure is as follows:


struct Lua_state {
Commonheader;
Lu_byte status;
Stkid top; /* First free slots 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 slots 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) */
};


So after this structure extends for example the following:

struct Lua_state {
Commonheader;
Lu_byte status;
Stkid top; /* First free slots 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 slots 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;//Here
};


Add two LUA interfaces at the same time, be able to put the function interface into the LAPI.C, the declaration put into lua.h can, or you are a fever to pursue limit efficiency don't care about a lot of other extensions and update friends, then you can use hard coded positioning, __mydata offset is 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;
}


That's all there is to it, and once again, to compile Lua, try the results:


A more abstract approach:






To locate using hard-coded:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvr25vcnro/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/center ">

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

A simple extension of the correct LUA can speed up the relevant C + + data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.