Friday, April 29, 2011

Sylvester & Speed

I've got some great news for The Render Engine!  This morning I did some optimizing for the engine that has resulted in a big speed improvement.  Since the beginning, The Render Engine has depended on Sylvester for it's 2d and 3d math.  While Sylvester is a fantastic library, it is generalized for solving a multitude of mathematical problems involving vectors, matrices, lines, and planes.  This generalization, while good for accurate calculations, is bad for performance.

Using Sylvester simplified the need to have The Render Engine know how to manipulate points, vectors, and matrices.  Most of the methods, however, are generalized to work against vectors of almost any length, matrices with numerous columns and rows, and more.  This generalization isn't optimal when it came to performance.  A lot of the methods rely upon loops over the elements of arrays.  While profiling the engine last night, I determined that a lot of time was being spent in "point2d.js".  By making the operations happen against simple objects (without property getters and setters and without looping) I was able to reduce the overall time, during one step of the engine, from 7% of the total time to about 2% of the total time.

Points and vectors are used heavily in the engine, so it made sense to optimize them for what they were intended to be.  Sylvester is still used for some of the more complex tasks because it works so well.  But for 99% of the cases the operations are against simple, purpose-built objects.  Overall, I am seeing huge speed gains, making The Render Engine v2.0 run almost as fast, per frame, as v1.x with more operations per frame!  I still need to make some more changes to Rectangle2D, Circle2D, and the 3d object types, but I'm really pleased with the performance I am seeing.

No comments:

Post a Comment