sábado, 1 de marzo de 2008

At least 5 times faster

Lately XP (eXtreme Programming) has been getting a bad reputation because it is being considered an excuse for not receiving nor writing documentation, even though that was the real intent of the XP inventors.
It is very interesting to note that even though XP as a whole is being dismissed, the individual practices are being incorporated into other competing processes like CMMI and RUP, as for example TDD (Test Driven Development), Continuous Integration and Spikes (Evolutionary Prototyping).
One of the XP practices that I'd like to write today about is the "Make it work, Make it right, Make it fast" (MiwMirMif) best practice, because I think most developers are doing the "make it fast" part with too much effort and very lame results.
The whole idea of the MiwMirMif is that:
1. First you get a working prototype you can manually test and verify it is working as expected. 2. Then you read the code and make sure the code is production quality, which means the code is clear enough, there is no code you don't understand or don't need, it is easily modifyable, etc. Whatever your criteria is for production-ready code. 3. Finally you make sure the code performs as expected.
The only problem I see with this is that most developers, once the second step has been achieved, ruin the second step in order to achieve the third step, and only to get a x1.2 speed increase, which in my opinion, is too lame.
There is a better way, and as always, the best way is far easier than the wrong way.
First, speed increases ideally should be an order of magnitude better. If not, they should be removed, as they are probably not the right speed increases. Second, if you positevely can't get your code to run 10 times faster, it at least should be 5 times faster. Else remove those optimizations and find better ones. Third, the optimizations must not clutter the code.
The functional code must be functional, and all mechanisms must be hidden in libraries that hide all the technical details. If the functional code also has optimizations, then most likely your optimizations are the wrong ones, since you can't test them independently, and anyway the functional code is always changing because business requirements change, therefore if you add optimizations to your functional code you are getting yourself into a problem most likely you will not be able to get out of..
All optimizations should get into libraries to be reused and to make sure your optmizations are correctly coded. Then your functional code will be clear and easy to modify and verify.
Which optimizations are the best?
First of all, make sure you have measured and you are positively sure you need some optmization, let us say, some operation takes 30 seconds and you need it to be under a second.
The first idea to consider is "do we really need to do this?", "can we avoid doing it entirely?". Sometimes it is not necessary at all.
Second, "do we have an optimization already in place?" If that is true, take it out, because it is not working as expected.
Third, "why is it taking so long?" Take a profiler and see for yourself which part of the code is taking long. Do not guess, just look at the profiler.
Fourth, "Is it O(1), O(log n), O(n)"? "Is it that we are going to the database too much?", "Are we performing the same operations over and over?" Make sure you understand why it is taking so long.
Fifth, make a guess about what could improve the speed. A cache? Then use a cache proxy. A buffer? Then use a buffer. Maybe you need a new data structure (highly unlikely), maybe you need shards, maybe you need to handle all in RAM in a distributed cluster using map/reduce. What ever you think can improve the speed, implement it at the library level and test it. Is it 10 times faster? Good! If not just test the next idea.

No hay comentarios: