[aquaria] Patch to run Lua scripts as threads

Alec Holowka zaphire at gmail.com
Tue Jun 29 03:07:13 EDT 2010


I don't know that "local" works that way, see:
http://www.lua.org/pil/4.2.html

I find that page of documentation kind of confusing. :P

On Tue, Jun 29, 2010 at 10:41 AM, Andrew Church
<achurch+aquaria at achurch.org<achurch%2Baquaria at achurch.org>
> wrote:

> That sounds pretty similar to my approach, only using an explicit table
> instead of creating a new environment table from the C++ side.  But if
> we're going to update the scripts anyway, wouldn't it be simpler to just
> use Lua's built-in local variable functionality?  E.g., instead of
>
> n = 0
> function init(me)
>        n = getNaija()
> end
>
> you make the first line "local n = 0", and if my understanding of Lua's
> scoping rules is correct, "n" is then local to that specific script.  I
> think I read that local variables are also faster than globals, though
> I can't recall the reference at the moment.
>
> The one potential issue I've seen so far with this is that Lua doesn't
> seem to have any facility for loading include files in the current scope,
> like "require" in Perl.  I haven't looked at all the Lua include files to
> see whether it's actually a problem, but if any of them reference global
> variables, those would have to be adjusted to take function parameters
> instead.
>
>  --Andrew Church
>    achurch at achurch.org
>    http://achurch.org/
>
> >So, one way to change the scripts to use local variables... Based on what
> a
> >friend told me, and the engine he's working on...
> >
> >Instead of creating a lua_State for each script, we create a table. Then
> any
> >script can access it's table... something like
> >
> >v = getVars();
> >
> >then any local variable just has to have "v." added before it.
> >
> >This doesn't sound like a super hard change to make, although it would
> >involve changing all the scripts.
> >
> >On Thu, Jun 17, 2010 at 11:05 AM, Andrew Church
> ><achurch+aquaria at achurch.org <achurch%2Baquaria at achurch.org><
> achurch%2Baquaria at achurch.org <achurch%252Baquaria at achurch.org>>
> >> wrote:
> >
> >> I've put together a patch that allows the game's Lua scripts to run as
> >> threads within a single Lua state.  The patch is admittedly a bit
> >> hackish, in that it creates a nested environment for each thread so the
> >> global variables defined in each script don't interfere with those used
> >> by other scripts, but IMHO the memory savings -- typically around 80% of
> >> Lua's current memory usage, for example ~16MB in Home Waters -- make it
> >> worth the change.  The nested environment hack could of course be
> >> removed in the future if the scripts are updated to use local variables.
> >> I haven't tested the patch extensively, but in theory it should be safe,
> >> as the only effective change is merging the common function set into a
> >> single read-only parent environment.
> >>
> >> The patch is attached below, as a compressed file due to its length.
> >> (Ryan, I hope that's not a problem for either you or the mailing list
> >> software!)  The actual changes aren't too major; the bulk of the patch
> >> is due to s/*L/baseState/ in ScriptInterface::createBaseLuaVM()
> >> (formerly initLuaVM(), which is now used only for threads).
> >>
> >> The patch is also in my repository (
> http://achurch.org/cgi-bin/hg/aquaria/
> >> )
> >> on branch "single-lua-state".  It's actually committed as two patches
> >> there, since part of the change is identical to one of my PSP patches,
> >> but I've merged them here for (what I hope is) convenience.
> >>
> >>  --Andrew Church
> >>    achurch at achurch.org
> >>    http://achurch.org/
> >>
> >> _______________________________________________
> >> aquaria mailing list
> >> aquaria at icculus.org
> >> http://icculus.org/mailman/listinfo/aquaria
> >>
> >
> >
> >
> >--
> >Alec Holowka
> >www.infiniteammo.ca
> >www.bit-blot.com
> >
> >--00c09f99e54989e40c048a24bf46
> >Content-Type: text/html; charset=ISO-8859-1
> >Content-Transfer-Encoding: quoted-printable
> >
> >So, one way to change the scripts to use local variables... Based on what
> a=
> > friend told me, and the engine he&#39;s working
> on...<div><br></div><div>I=
> >nstead of creating a lua_State for each script, we create a table. Then
> any=
> > script can access it&#39;s table... something like</div>
> ><div><br></div><div>v =3D getVars();</div><div><br></div><div>then any
> loca=
> >l variable just has to have &quot;v.&quot; added before
> it.</div><div><br><=
> >/div><div>This doesn&#39;t sound like a super hard change to make,
> although=
> > it would involve changing all the scripts.<br>
> ><br><div class=3D"gmail_quote">On Thu, Jun 17, 2010 at 11:05 AM, Andrew
> Chu=
> >rch <span dir=3D"ltr">&lt;<a href=3D"mailto:achurch%2Baquaria at achurch.org<achurch%252Baquaria at achurch.org>
> ">=
> >achurch+aquaria at achurch.org <achurch%2Baquaria at achurch.org></a>&gt;</span>
> wrote:<br><blockquote class=3D"g=
> >mail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc
> solid;padding-l=
> >eft:1ex;">
> >I&#39;ve put together a patch that allows the game&#39;s Lua scripts to
> run=
> > as<br>
> >threads within a single Lua state. =A0The patch is admittedly a bit<br>
> >hackish, in that it creates a nested environment for each thread so
> the<br>
> >global variables defined in each script don&#39;t interfere with those
> used=
> ><br>
> >by other scripts, but IMHO the memory savings -- typically around 80%
> of<br=
> >>
> >Lua&#39;s current memory usage, for example ~16MB in Home Waters -- make
> it=
> ><br>
> >worth the change. =A0The nested environment hack could of course be<br>
> >removed in the future if the scripts are updated to use local
> variables.<br=
> >>
> >I haven&#39;t tested the patch extensively, but in theory it should be
> safe=
> >,<br>
> >as the only effective change is merging the common function set into a<br>
> >single read-only parent environment.<br>
> ><br>
> >The patch is attached below, as a compressed file due to its length.<br>
> >(Ryan, I hope that&#39;s not a problem for either you or the mailing
> list<b=
> >r>
> >software!) =A0The actual changes aren&#39;t too major; the bulk of the
> patc=
> >h<br>
> >is due to s/*L/baseState/ in ScriptInterface::createBaseLuaVM()<br>
> >(formerly initLuaVM(), which is now used only for threads).<br>
> ><br>
> >The patch is also in my repository (<a href=3D"
> http://achurch.org/cgi-bin/h=
> >g/aquaria/" target=3D"_blank">http://achurch.org/cgi-bin/hg/aquaria/
> </a>)<b=
> >r>
> >on branch &quot;single-lua-state&quot;. =A0It&#39;s actually committed as
> t=
> >wo patches<br>
> >there, since part of the change is identical to one of my PSP patches,<br>
> >but I&#39;ve merged them here for (what I hope is) convenience.<br>
> ><br>
> > =A0--Andrew Church<br>
> > =A0 =A0<a href=3D"mailto:achurch at achurch.org">achurch at achurch.org
> </a><br>
> > =A0 =A0<a href=3D"http://achurch.org/" target=3D"_blank">
> http://achurch.or=
> >g/</a><br>
> ><br>
> >_______________________________________________<br>
> >aquaria mailing list<br>
> ><a href=3D"mailto:aquaria at icculus.org">aquaria at icculus.org</a><br>
> ><a href=3D"http://icculus.org/mailman/listinfo/aquaria"
> target=3D"_blank">h=
> >ttp://icculus.org/mailman/listinfo/aquaria</a><br>
> ></blockquote></div><br><br clear=3D"all"><br>-- <br>Alec Holowka<br><a
> href=
> >=3D"http://www.infiniteammo.ca">www.infiniteammo.ca</a><br><a
> href=3D"http:=
> >//www.bit-blot.com">www.bit-blot.com</a><br>
> ></div>
> >
> >--00c09f99e54989e40c048a24bf46--
> >
> >--===============4162574011203577186==
> >Content-Type: text/plain; charset="us-ascii"
> >MIME-Version: 1.0
> >Content-Transfer-Encoding: 7bit
> >Content-Disposition: inline
> >
> >_______________________________________________
> >aquaria mailing list
> >aquaria at icculus.org
> >http://icculus.org/mailman/listinfo/aquaria
> >
> >--===============4162574011203577186==--
> _______________________________________________
> aquaria mailing list
> aquaria at icculus.org
> http://icculus.org/mailman/listinfo/aquaria
>



-- 
Alec Holowka
www.infiniteammo.ca
www.bit-blot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://icculus.org/pipermail/aquaria/attachments/20100629/36077c92/attachment-0001.htm>


More information about the aquaria mailing list