Monday, March 28, 2005

Optimization

This recent post on JavaLobby has been in my thoughts all day. The post describes an unusual and unintuitive optimization based on the use of the Java String copy constructor. As much as I doubted the claim, the idea intrigued me enough that I wrote a program to test the assertion. Sure enough I was able to reproduce the results but only in the most narrow of cases. So narrow in fact that unless you can measure a benefit I wouldn’t suggest following that pattern.



However, this whole thing’s got me thinking. Programmers worry too much about minutia of execution when they should simply be following some key principles:



1. Be fanatical about your IO. Prefer smaller data sets to large ones but don’t skimp on boundary markers and type data that will allow you to version the software. Write big chunks.


2. Watch your cached and held data like a hawk. Just because you’re only storing a single object or pointer doesn’t mean you just haven’t referenced a giant object tree. Protect your caches from explosive growth. Don’t hold data you don’t need.


3. Worry about data conversions and data copying. Parse, copy, translate or whatever, as few times as possible. Uses references into the raw data blocks rather then duplicate the data if possible.


4. Don’t execute code that’s not needed.



Most performance issues can be addressed by just focusing on these simple principles. There’s seldom any need for magic solutions like the one described at the start of this post. I contend, as well, that following these basic principles isn’t premature optimization; It’s just smart coding.



A favorite mantra of mine is K.I.S.S. - Keep It Simple, Stupid. I believe however that keeping things simple doesn’t necessarily mean simplistic. Rather, I think it means focusing on the core code values such as I’ve stated above.

Post a Comment
 
The Out Campaign: Scarlet Letter of Atheism