I'm finally starting to try to make some more use of Lua, and ran into a minor hiccup today.
To make use of the different paste modes in the Lua version of g.putcells(), from the gplus module, my first attempt was
patternvar.put(x,y,{1,0,0,1},"xor")
but thanks to Lua's carefree way of throwing away input values that it doesn't need, the "xor" gets silently discarded. What actually works is
patternvar.put(x,y,{1,0,0,1,"xor"})
Seems a little weird to have to hide the paste mode inside the transformation matrix to get it noticed. I could get used to it, but I'm not sure I should...!
Also, might it make sense to adjust init.lua so that "emptypat = pattern()" works the same as "emptypat = pattern({})" ?
EDIT: Another interesting-but-not-surprising thing I ran into and worked around, while working on a 50K universal megafier script for Adam's old stable 2^N megacells.
There are various places where you need a row of 500 eaters or more, or a long row of reflectors or Fx77 conduits -- lots of repetition. I tried building cell lists (cell tables?) in the obvious inefficient way, by adding one eater at a time in a for loop. But very quickly Golly's memory usage explodes -- I couldn't even get all the way around the megacell, just a few thousand fishhook eaters, before the script started running very slowly.
Golly had used up the meager (by modern standards) three gigs of memory on this laptop and was paging to the hard drive. It seemed as if Lua's garbage collector just wasn't keeping up with the increasingly larger discarded temporary tables.
Here's a code snippet that reproduces the problem for me:
Code: Select all
local g = golly()
local gp = require "gplus"
local split = gp.split
local pattern = gp.pattern
local pat = pattern({})
local x = 0
local k=g.getevent()
while k=="" do
pat=pat+pattern("2o$2o!",x, 0)
x=x+3
k=g.getevent()
g.show(x//3)
end
pat.put()
I start seeing serious trouble around 5000 blocks or so. Y'all probably have more memory, so your mileage may vary.
Might it be worth adding a pattern.append method to the gplus module? That way this kind of incremental pattern building could be done by adding new cells to the same table at each step, instead of garbage-collecting two entire tables after copying them into a third. I
think that's what's going on here, anyway -- haven't tested the theory yet.
The immediate simple workaround was to build a series of medium-sized cell tables -- one edge of the megacell at a time, for example -- store them in separate variables, and combine them at the very end. Those medium-sized tables only have to get copied and discarded once, and it seems to be enormously faster.
For the new megafier script I'll probably write an "N copies of pattern P at offset X, Y" function that builds and appends increasingly longer subpatterns with 2^k copies each, assembling N from its binary bits. It looks like that will be significantly faster again than my current method.
Still, maybe it's a good idea to document the issue in the Lua Scripting Help document, under Potential Problems and/or "Using the gplus module" -- at least if anyone else can see this effect. It just seems as if Python's += operator for patterns is
much more memory-efficient than Lua's pat1 = pat1 + pat2 ... or at least it's garbage-collected a lot more smoothly.