Closures and garbage

June 10th, 2010 Victor Posted in beginner, intermediate | 1 Comment »

Here is a transcript of a message that just landed in my Inbox via Tamarin-devel@mozilla.org

Tamarin has a hybrid storage manager; reference counting takes care of a lot of freeing, but there’s a regular garbage collector that will deal with cyclically referenced objects. Objects that can be reclaimed by reference counting are reclaimed relatively promptly; the rest may take a while. The back-up collector is driven by allocation volume, so as the program runs and allocates storage (and doesn’t free it via reference counting) then the collector moves forward to a point where it will have scanned the entire heap and will reclaim garbage.

The reference count increment you describe is legitimate; when you create a closure it captures (increments references on) all objects in its lexical scope, and in a regular class method ‘this’ is one of those objects. There are optimizations that Tamarin could be doing that it’s not currently doing, by not capturing objects that contains only names that are not referenced from the body of the function expression. In your artificial example that would have helped you out, but it’s less clear whether it would help you out in a real situation where the body of that function would not be empty but would probably be referencing something in the surrounding class instance (ie ‘this’).

(All this information provided to you with the proviso that I’m relatively ignorant about unloading in Flash :-)

–lars

I’ll try to (quickly and incompletely) point out one of the implications of what Lars explains here. This affects any ActionScript3 program that runs in Tamarin (including swf’s in Flash Player).

Let’s take a look at this code:

class MyClass
{
	public function myFunction():Function
	{
		var usedObject:Object = {};
		var unusedObject:Object = {};
 
		var myClosure = function():Object
		{
			usedObject.field = 100;
			return usedObject;
		}
 
		//...
		return myClosure;
	}
}
 
//Later on, we use the closure
var inst:MyClass = new MyClass;
var fn:Function = inst.myFunction();
var o:Object = fn();

Now, as we know, the function myClosure has access to all variables in it’s scope chain, including locals of myFunction and instance members of MyClass.
The question is, what happens with each of the variables when they are not needed anymore?
For example, we expect unusedObject to be garbage collected right away. Well, it turns out that this is not the case. As Lars Hansen explains here, all variables accessible by the closure are captured by the closure. So they don’t get deleted until the closure gets deleted. More than that, the instance of MyClass (the “this” object) is also captured (linked), although you can’t directly use the keword this inside myClosure (it would point to the global object).

Bottom line, careful with closures. While they provide great convenience in coding, they might come with serious performance penalty.

Let’s just hope this will come to reality in the near future: “There are optimizations that Tamarin could be doing that it’s not currently doing, by not capturing objects that contains only names that are not referenced from the body of the function [...]

Thank you Lars for these details!

AddThis Social Bookmark Button

Eye for an Eye, MiniBuilder on Mac

May 24th, 2010 Victor Posted in MiniBuilder | 1 Comment »

The other day I finally borrowed a Mac for a couple of days with the plan to fix MiniBuilder on this platform.

As I recently received a patch that fixes copy/paste on Mac from a guy named Romain (thank you very much), it looked like it was time to make the bold move. So I installed MB on the MacBook and checked out the AirMiniBuilder project. And here’s where I was faced with the frustration of not having any option for copy/paste in the editor. Payday for me, I had to manually type the patch.

Seems OK now. I added a switch for the platform and I did the deploy. Mac users can install version 1.1.2 alpha and enjoy MiniBuilder.

There is still a problem on Mac, at least one that I’m aware of: the project window menu is not refreshed when you work with more than one project at once. If you switch form one project window to another, you end up with the menu of the old window. The workaround is to restart MiniBuilder when you change the project. MiniBuilder exits when all windows are closed.

http://code.google.com/p/minibuilder/

AddThis Social Bookmark Button

MiniBuilder 1.1.1-alpha GotoDefinition and Back

April 11th, 2010 Victor Posted in MiniBuilder | 5 Comments »

Development on MiniBuilder, the Flash/AIR IDE for ActionScript projects continues.

There were a few releases since my last post on this blog. If you want to keep a closer look, follow MiniBuilder on tweeter http://twitter.com/minibuilder

Latest important couple of features are Go To Definition and Back. Move the cursor inside any identifier defined in your project and press F4 to go to the place where your symbol is defined. It might be a class, a field, parameter or a local var of any enclosing closure. Then press Shift+F4 to return to the place you were before. I know, that’s very very useful ;)

Also, now you can customize the keyboard shortcuts, finally.

See the changelog

Download page

For installation instructions and other info, check project page http://minibuilder.googlecode.com/

AddThis Social Bookmark Button

MiniBuilder used by Wonderfl

December 11th, 2009 Victor Posted in MiniBuilder | 4 Comments »

I am happy to see that MiniBuilder is now used by a well-known online platform for developing ActionScript: Wonderfl

MiniBuilder is GPL licensed, so feel free to use it in your own open-source project. Don’t forget to add a link back, so MiniBuilder can also evolve.

http://code.google.com/p/minibuilder/

The days where you had to install heavy IDE to write just a few lines of ActionScript are over. MiniBuilder works on Linux and Windows and its main features are:

  • Code completion for your classes, default libraries and your swc libraries
  • Project tree, multi-tabs editor
  • Fast(!) text editor (written from scratch – it only renders the visible part of the text), it can handle up to 1000 lines in a class without becoming unresponsive.
  • Integration with compiler errors. One click goto-error file and line.
  • Many other features.

MiniBuilder on wonderfl.net (a customized stripped-down version of MiniBuilder)

minibuilder-on-wonderfl

AddThis Social Bookmark Button