lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Stefan <ste@evelance.de> 于2020年4月17日周五 上午2:18写道:
> This sounds interesting!
> I've added your modified Lua 5.3 to the test repository:
> https://github.com/evelance/lxlib/tree/09bf1392a74713fb7bd393b99c8aaa7ba3482f4f

Thanks for your interresting.

I patched lua 5.4 yesterday with a little different way. (Because lua
5.4 use the whole 8bits of ->marked)

You may have a look here : https://github.com/ejoy/lua/tree/shared

There are 5 parts:

1. Add a shared status to the GC object, it can prevent gc cycle to
mark the object shared from outside.
https://github.com/ejoy/lua/commit/24ae2666afcfa145478b144cd59695c5971e8bd8
2. Add a small thread lib for multithread environment, becuase we may
run multi lua vm in different os threads.
https://github.com/ejoy/lua/commit/f64ce23202b52d832809c438e1757c877d778b2e
3. Add an id in TString
(http://lua-users.org/lists/lua-l/2019-06/msg00413.html )
https://github.com/ejoy/lua/commit/fa95776afc38a02c37c2849498ad16dfbbf550f1
4. Add lua_clonefunction  to share function prototype from outside.
https://github.com/ejoy/lua/commit/e37479374e3245716bea7cd8fbc774aba2a9a80f
5. Add a small test case.
https://github.com/ejoy/lua/commit/22422277775562ce6885c33bbf6a3cd5ef2c4c1a

You can use it as the regular lua, but I provied some new C APIs :

LUA_API void (lua_clonefunction) (lua_State *L, const void * fp);
LUA_API void (lua_sharefunction) (lua_State *L, int index);
LUA_API void (lua_sharestring) (lua_State *L, int index);
LUA_API void (lua_clonetable) (lua_State *L, const void * t);

See test/* for the detail.

https://github.com/ejoy/lua/blob/shared/test/test.lua

```lua
local sharetable = require "sharetable"
local multivm = require "multivm"
local vm = multivm.new()
local testp, tablep = vm:dostring [[
  local sharetable = require "sharetable"
  _G.test = { "Hello", "World" }
  local testp = sharetable.mark_shared(_G.test)
  local tablep = sharetable.mark_shared(_G.table)
  return testp, tablep
]]

local shared_test = sharetable.clone(testp)
local shared_table = sharetable.clone(tablep)

-- Use table.unpack in vm to unpack _G.test in vm.
print(shared_table.unpack(shared_test))
```

If you use multiple lua VMs in the same process, you can access the
table/function without upvalues from outside (in the other VM)
directly.

For example, you can share the base library table
table/string/utf8/... by `lua_clonetable`, it's much faster than
luaL_openlibs . And if you loaded a .lua file from a vm before,
loading the same file from other VM would be much faster , too.

-- 
http://blog.codingnow.com