James Cat's rough && ready blog

Object Pooling

Posted in Msc by zzjames on August 10, 2012

Memory pooling is necessary for high performance JavaScript, at least any high performance JavaScript which involves objects of any kind of size and number, of course you can get some little program to go really fast, you don’t need object pooling, but for a complex web app that requires high performance, chances are performance is going to be improved with object pooling.

Memory allocation in JavaScript

In C++ we can allocate a local object to the stack or the heap. Stack allocation is very fast as the only overhead is decreasing the stack pointer by the size of the object. Stack memory is fast as it is likely to be in the cache. If an object is allocated to the heap, the memory manager will have to find a free chunk of memory and mark it as in use. Heap allocation is therefore slower than stack allocation. For temporary objects whose lifetime is the length of the function the stack is a more efficient method for allocating memory.

JavaScript is a high-level scripting language. Objects in JavaScript are always allocated on the heap. Since JavaScript was not designed for professional programmers, memory allocation was not designed to be the programmer’s responsibility, but instead something that the JavaScript run-time finds the optimal solution for.

Unfortunately, this means there is no way of creating temporary objects in a cheap way and JavaScript run-times are not good at optimizing cases where temporary objects are being used.

On each occasion where we use a temporary object, instead of creating a new local object on the heap we could reuse an existing ‘temporary’ object. This strategy is known as ‘Object Pooling’ where temporary objects are fetched from an existing pool rather than created, and returned to this pool rather than being destroyed.

Thus we implement functions with a strict rule of whenever possible not allocating new local objects in the methods, ‘whenever possible’ is included in this rule because sometimes we might not have enough objects in our object pool, in this case a method of creating a new object in the pool is provided.

Implementing Object Pooling in JavaScript

There is at least one well documented publicly available implementation of object pooling in JavaScript, it belongs to a project called gamecore.js and the author(s) have very kindly spent some time making a tutorial on the subject.

Today I am currently looking at how their implementation will fit with my existing code. gamecore.js uses class.js – a script that builds class like behaviour using JavaScirpt prototype object model. Although I have been using the prototype chain as an equivelent of a class-definition, especially for sub-classing in the cases where it is (rarely thankfully!) needed, I am trying to stay away from just ‘fixing’ the prototype system by creating a layer code just to implement classes – this is purely an experiment to challenge my own thinking and to prove or disprove that C++/Java style OOP is the be-all and end all. The downside is that (I think, not read the whole thing yet!) the gamecore.js object pooling code is written for a ‘class’ based object system, I appreciate that it basically works like Singleton pattern, in that it uses a static ‘factory’ method on the class, but I don’t know what or where my static methods would live, in the constructor? in the prototype? I’m guessing the prototype would be faster as _proto_ links are optimised (they’re invariants) and .constructor links are not (although with inline cache I think .constructor property would get optimised  if you hit it a lot) .

This isssue will be solved by Saturday night, or I will, um, be sorry.

About My Project:

Posted in background, Msc by zzjames on August 10, 2012

The title of my project is: ‘The Development of a tool to facilitate the production of animated interactive learning materials, games and simulations’

The aim of my project is: To develop software to facilitate teachers, developers, educators and learning technologists in the production of animations, simulations and games as learning material.

The Objective of my project is: To develop a game engine for use in an educational context.

My intention is to ultimately situate a game engine and authoring tools within a online virtual learning environment (VLE) providing not just tools for creating and displaying this content but also assisting with feedback to the VLE to monitor students progress using the material, even potentially setting learners the task of authoring their own content – and getting feedback from this.