On Tue, Jun 12, 2012 at 10:53 AM, Mike Pall <mike-1206@xxxxxxxxxx> wrote: > Robert G. Jakabosky wrote: >> On Monday 11, Mike Pall wrote: >> > Adam Strzelecki wrote: >> > > for i = 0, 1000 -- some tight loop >> > > local value = model[i] * view >> > > gl.UniformMatrix4fv(location, 1, gl.TRUE, value.gl) >> > > end >> > >> > Well, no, because 'value' *does* escape. There's no way for the >> > compiler to know that gl.UniformMatrix4fv() doesn't store the >> > 'value' pointer somewhere else. >> >> If gl.UniformMatrix4fv() did store the value and the value wasn't also stored >> somewhere on the Lua side, then there is already a bug since the GC would >> free >> the value which might still be in use by the C code. > > No, there's a guarantee that arguments to a C function call are > not collected before the C call returns. But - correct if I'm wrong - if the C side stored a 'value' pointer anywhere, there wouldn't be any guarantee the GC wouldn't free the value *after* the FFI call returns. So, the only way one could prevent the value from being freed by the GC after one iteration of the loop would be to store a reference to it on the Lua side. But then 'value' goes out of scope and no reference was stored to it anywhere on the Lua side. It seems to me that, in this case, it could be possible that some sort of liveness analysis would detect 'value' is not used after the iteration on which it was created, and thus would mark it as movable to preallocated memory. However, I don't know if such an analysis would be easy to implement in the actual codebase.