DominicHamon.com
Life is like a grapefruit
Typed data for performance boost
After reading John McCutchan’s recent post on numeric computation in Dart, and a conversation with Srdjan Mitrovic from the Dart VM team, I made some changes to the Dart port of Box2D in the hope of improving performance. The specific commits that make up this change are here and here, and this change has been released to pub as version 0.1.6.
Originally, I had attempted to port the internal math library to John’s vector_math library. For one, this would mean that I no longer need to maintain a chunk of code, and for another it means any work he does to introduce SIMD into the library will automatically be available to me. However, this integration introduced some major instabilities into one of the collision solvers so I abandoned it.
Instead, I made two key changes to the code: Converted all num
types to double
, and converted the vector and matrix types to use typed data instead of double stored in fields. First, the results. All benchmark code can be found here and were run under SDK version 0.5.11.1_r23200. In all the below charts, the y-axis is the number of steps simulated per second, hence bigger is better.
Results
This simple benchmark shows little difference in performance across the board.
This simple benchmark shows a performance regression for higher step counts. This may be a sign that some parts of the code haven’t been updated to use doubles so the VM is having to do extra work.
The first of the complex benchmarks with many collisions per frame shows a huge improvement across the board. The performance was always fairly consistent, but now it is consistently better.
The most complex benchmark (the one that exhibits the collision instabilities under vector_math) also shows huge improvement in performance.
Rationale
It is clear from the above results that these changes have made a significant positive difference to performance, but why? We’ll tackle each part of the change in turn.
doubles
The VM can work with doubles in an unboxed form which makes computations very efficient. This does, however, break down when passing doubles to functions or returning them from methods as this causes a box/unbox operation pair. Similarly, storing a non-smi number in a field, as the old math library was doing, causes a box operation as fields can only hold smi values or object pointers. And that’s where typed lists come in.
Typed lists
Typed lists in Dart can only hold numbers, not regular objects. Further, unlike when working with integers, the double value does not need to be tagged. This means that the storing and retrieving doubles from a typed list is very fast, and also very memory efficient.
JavaScript
This is all well and good for the VM, but what happens when this code is converted to JavaScript, which is going to be the majority use-case at this point? JavaScript stores all numbers as double precision floating point numbers anyway, so there’s no conversion necessary there. Also, the typed lists map trivially to the native typed arrays in JavaScript which also means no further overhead.
Conclusion
If you’re doing any kind of numeric computation in Dart, read John’s article again and follow his advice. The tips he gives are not theoretical; they have a significant practical performance impact.
Posted on 2013/06/05 in performance, web | | Leave a comment
Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Recent Posts
- London Calling
- The Rise and Fall of Ziggy Stardust
- The only winning move is not to play
- s/GOOG/TWTR/
- URL shortener in go
- All the small things
- gomud
- Hole hearted
- Typed data for performance boost
Archives
- April 2018
- May 2015
- August 2014
- February 2014
- January 2014
- November 2013
- September 2013
- June 2013
- May 2013
- March 2013
- February 2013
- December 2012
- November 2012
- September 2012
- May 2012
- March 2012
- January 2012
- September 2011
- May 2011
- April 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- July 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
Search
Search
Copyright © Dominic Hamon 2021. WordPress theme by Ryan Hellyer.