mrdoob/three.js — pull request #5789 by bhouston (2014-12-19 17:49:01+00:00)
Title: improve recycling of programs

When a material changes, Three.JS current deallocates the associated material program, and then creates a new program. Often the new program is the same as the previous program. The deallocator is smart in that it uses reference counting, but if only a single material of the specific type is in use, the reference count will go to zero every time in this scenario, and the program will be deallocated and then it is requested again where it is re-created: thus the reference count codes 1 to 0 to 1.

What I have done is change the order slightly, so now when a material changes, it first requests the new program before it deallocates the old program. This ensures that if the program can be reused it's reference count goes from 1 to 2 to 1, thus avoiding an unnecessary deallocation.

This change greatly speeds up https://Clara.io in the face of dynamic materials and lights.