Google+ Badge

Thursday, March 20, 2014

Is it fast enough - the lost art of building fast programs

When I was writing programs for the stock market speed and memory usage were huge issues. You dare not leak any memory because 100,000 headlines coming your way in a 24 hour period with a memory leak was a sure way to crash a program.

Speed was also very important. In the news business being first, and maybe only, source to get a headline out was critical. It needed to be on the screen right away. Turns out accuracy from the source was of less importance, they could always correct things in the story body later.

After the stock market company outsourced everything to India I work on tamer programs that did not use real-time data. Of course you still don't want to leak memory and you want things to be fast but the thrill and stress of that waned considerably.

Writing mobile apps brought a lot of that back. Not only do you want to avoid memory leaks you want to have as small a footprint as possible. The CPU / GPU combination on a mobile device is much slower than the desktop you are developing on. Speed is again a big factor. No one wants taps and swipes to feel like molasses. You have much less screen space to deal with and you need to use it wisely. Everything was about speed, space and time again. The thrill was in the air. I never stopped coding like size and speed mattered but the importance bubbled to the top again and I loved it.

Right now I am helping out with a web application using JavaScript and AngularJS. I have used JavaScript with JQuery in the past and I find AngularJS to be big step in the right direction. The app really just needs to be fast enough to be usable. Simple request / reply / show it on the screen operations. Speed is in no way critical and really is just an afterthought if considered at all. We are also using a number of JavaScript libraries including Underscore.js.

As I use various methods in these libraries I wonder about their speed and efficiency. You do seem some mention of that when you query about Underscore array methods on Stack Overflow. I don't feel enough attention is focused here and I try to find and use the most efficient method for my code. Sure, it is not bad to make a new copy of an array to add or remove a few elements but if that array grows tenfold what happens?

It is so easy to look at code and think each line of code takes the exact same amount of time to execute. Here is this wonderful AngularJS controller and it is only 20 lines of code! Dang it must be fast because when we did something similar in JQuery it was 100 lines. Sadly reality does not work that way. Each of those 20 lines can be running all sorts of framework code. That one call to an Underscore method might be 50 lines in itself. Dynamic injection is happening. DOM changes are occurring. Watch methods are being called. Other controllers are receiving messages and running lines and lines of code.

It is wonderful when the framework takes care of the busy work. I want to focus on the business logic at hand. I love it when a few lines of code updates four separate areas of the screen. I have written multithreaded framework code to perform those types of actions. Set a variable to false and have everything that cares disable - the menu item, the toolbar button the right button menu option, etc. If the user of my framework put the call to toggle the setting between true and false in a loop it looked innocent but man a lot of stuff was happening in the background.

I still want to know what is going on behind the scenes. When I called that line what was the ripple effect? If I add an item to that collection does it create a whole new collection and return it to me or just add it? Did it walk the entire collection to add it to the end or is the push operation fairly native? Does it scale well or do I need to use a different object or collection or framework to do this job? Will the client have 10 of those or 1,000 of them?

Speed is hard to add but easy to lose. Optimizing early can be a waste of time. Not thinking about optimizing at all can kill a project. Right now I am trying to learn so many new things in the JavaScript arena it is hard to know if I am doing it correctly. As I write the code I try to look for the optimal way to do it. Luckily code reviews help point out when I missed a better way. Taking time out to find web references is also needed. I try to scan over all the available methods in a library even if I don't need them now just in case I need them later. I may not remember the exact name but if I remember there was a method I can go back and find it and give in the in depth evaluation pass to decide if it is usable.