How is a referencing environment generally implemented for closures?

Posted by Alexandr Kurilin on Stack Overflow See other posts from Stack Overflow or by Alexandr Kurilin
Published on 2012-09-02T03:18:09Z Indexed on 2012/09/02 3:38 UTC
Read the original article Hit count: 238

Let's say I have a statically/lexically scoped language with deep binding and I create a closure. The closure will consist of the statements I want executed plus the so called referencing environment, or, to quote this post, the collection of variables which can be used.

What does this referencing environment actually look like implementation-wise? I was recently reading about ObjectiveC's implementation of blocks, and the author suggests that behind the scenes you get a copy of all of the variables on the stack and also of all the references to heap objects. The explanation claims that you get a "snapshot" of the referencing environment at the point in time of the closure's creation.

  1. Is that more or less what happens, or did I misread that?
  2. Is anything done to "freeze" a separate copy of the heap objects, or is it safe to assume that if they get modified between closure creation and the closure executing, the closure will no longer be operating on the original version of the object?
  3. If indeed there's copying being made, are there memory usage considerations in situations where one might want to create plenty of closures and store them somewhere?

I think that misunderstanding of some of these concepts might lead to tricky issues like the ones Eric Lippert mentions in this blog post. It's interesting because you'd think that it wouldn't make sense to keep a reference to a value type that might be gone by the time the closure is called, but I'm guessing that in C# the compiler will figure out that the variable is needed later and put it into the heap instead.

It seems that in most memory-managed languages everything is a reference and thus ObjectiveC is a somewhat unique situation with having to deal with copying what's on the stack.

© Stack Overflow or respective owner

Related posts about compiler

Related posts about programming-languages