Blog

PHP-Closure

26 February 2010

At the beginning of 2006 Erik and I started a 20% project to write a JS library for Google. Today it is used by many projects within Google and was recently open sourced.

On its own, the Closure Library isn’t very compelling for the average web developer. The dynamic loading is intended for unit tests and development, and using it in production would cause users to download masses of unused code.

Luckily for us, the Closure Compiler was open sourced at the same time. The compiler knows about a lot of the idioms used in the Closure Library and does a really good job of removing dead code and optimizing what remains. The team also launched a web service with a RESTful API so you don’t even have to run the compiler yourself.

Even with the web service, the development-debug cycle isn’t ideal. So when I found myself wanting to use some of the Closure Library for my new photo site I hacked together a PHP class that provides a convenient interface to the compiler web service, caches the optimized code, and recompiles when the sources have changed. Pretty basic, but quite useful I think.

I know PHP is looked down upon by many programmers, but it is easy, convenient, and widely available. If you have a PHP based site and want to use the Closure Library and/or the Compiler, it might be worth heading over to the project page and seeing if it might be useful.

[php-closure]

DocType

15 May 2008

Mark Pilgrim’s project to document the web launched today as DocType. This is a really great resource, with topics ranging from security to cross browser DOM wrangling, and it’s only going to get better as it is completely open.

A lot of the example JS code is based on the project Erik and I started, and some is available for download from the SVN depot.

MS Patches IE6 GC

17 December 2007

Microsoft released their GC patch to the JScript engine on automatic update, if you didn’t get it and ever use IE, even for testing, I recommend downloading it. See my post from March on the problems with IE6 GC.

Day Job

30 October 2007

You can read about what I’ve been working on here.

My favourite feature: visible history means shareable searches.

GeoTrace

7 May 2007

Post-Cinco-de-Mayo-tequilla-hangover == no skydiving. Instead I knocked together a little experiment for a geographical traceroute. Not polished or well coded.

Garbage Collection in IE6

7 March 2007

I’m not an avid blogger, and when I do post it’s rarely tech related. But recently I have had cause to do some investigation into the effects of Internet Explorer’s garbage collection routines on performance, and I thought it would be useful to summarize some findings.

Eric Lippert posted about the internals of IE’s garbage collector back in September 2003, though he skimmed over the important bits, which were later noted in the comments. The crux of the problem is that IE’s script engine uses allocations to determine when to run the GC; that is after 256 variable allocations, 4096 array slot allocations, or 64kb of strings have been allocated. Not only are allocations a bad indicator of garbage, but these limits are such that any decent sized application is going to make the GC run pretty regularly.

To compound this problem, the running time of the garbage collection routine is dependent on the size of the working set (O(N^2) as described in Lippert?s article, though the results below show a linear relationship). So as your application gets bigger the garbage collection runs slower.

Back in the day this didn?t really matter, but as web applications are getting more complex there is the potential to hit a performance wall. More code being executed means the garbage collector will be run more frequently; and because the applications contain more state on the client; and have larger code bases, the object graph that the garbage collector has to traverse gets bigger.

To demonstrate the effects of this on performance I?ve used a simple benchmarking function which creates 5000 object literals with random properties, and then sorts them. The function is then run on a simple HTML page, pre-populated with a further O-objects, each with P-properties, which will always remain in scope.

The following results show the mean execution time of the create-and-sort test as O increases for constant P=50 on Firefox 2.0 (red) and IE6 (blue) on the same computer.

http://www.endoflow.com/gcbench/gc_graph.png

Try the test for yourself.

Now, the test environment is quite contrived in that it creates the literals as homogeneous global variables in a simple scope, but the effects are the same if you create objects dynamically, with scope chains exposed via event handlers and closures.

I doubt there are many web applications that are big enough to be seriously impacted by these problems, though it is worth bearing in mind, since performance is proven to be strongly linked to adoption of web apps.

Microsoft issued a hot fix that allows you to increase the allocations, this gives a significant performance boost, but you don?t want to force all your users to patch IE so this isn’t a viable solution. IE7 seems to have solved this problem by having dynamic allocation thresholds that scale to the size of your application, but rollout of IE7, particularly to corporate users, is likely to be slow for the rest of 2007. The other options can be painful and basically involve optimizing your application by reducing code size and finding the balancing point between improved performance from keeping state local and keeping your working set to a manageable size. Always explicitly dispose objects when they are no longer needed by removing event handlers and dereferencing properties.

References:

Eric Lippert’s 2003 post: “How Do The Script Garbage Collectors Work”
http://blogs.msdn.com/ericlippert/archive/2003/09/17/53038.aspx

Micrsoft Support Article: “You may experience slow performance when you view a web page that uses Jscript in Internet Explorer 6″
http://support.microsoft.com/kb/919237