Log in

Previous Entry | Next Entry

More on Flash

Just now I was playing a round of Wordscraper with a friend of mine on Facebook. Wordscraper is a game like Scrabble, only Hasbro can't charge rent on it because it's not identical. Don't get me started.

Anyway, I noticed that Wordscraper was going really slowly. It turned out that of the three (!) Flash applications on that page, one of them was a really stupid ad with a busy wait in it. A busy wait is the computer programming equivalent of a track stand. Suppose you are on your bicycle and you come to a stop light. You can't go until it turns green (or at least until there's no cross traffic, depending on your proclivities). So you have to wait, there at the light, probably for a minute or so.

You could step out of your pedals and onto the pavement, and just stand there waiting. Or, you could try to balance on your bike, with great effort and concentration, for the entire time until the light changes. Busy waiting is like that. While your computer is busy waiting, it can't do anything else, because it's busy. On a multitasking system, like the Mac, the computer will periodically interrupt the busy wait and let other programs work, but the busy wait will take up a substantial amount of computer time, because the program looks busy. So the rest of the computer will be really sluggish.

On top of this, remember that computers use less power when they are idle. This didn't use to be the case, but it's the case with practically every modern computer. Why? Because computers actually draw a significant amount of power. If you don't want your computer to be a total power hog, you have to carefully conserve energy when it's not needed, by slowing down the CPU. But if the CPU is doing a busy wait, it's going to be running at 100% utilization all the time, drawing the maximum amount of power continuously.

What does this have to do with Flash in particular? Just this: Flash programs are written with authoring tools that make it so easy to write them that the person writing them doesn't have to have any training or experience at all, and indeed may not even realize that he or she is writing a program. These programs are then foisted on your web browser without any vetting by anyone who would be competent to detect a serious programming error like this. There's nothing wrong with amateurs writing programs. Indeed, it's really good for them to do so. But when their programs get run on millions of computers, that's not so good.

Steve Jobs mentioned in an interview a while ago that if you enable Flash, your battery life goes down by about 30%. This is why. It's because Flash programs are big, and inefficient, and often written by people who don't know what they are doing, and don't know not to make tyro mistakes like putting in a busy wait. It doesn't even matter if Flash is a good technology or not. What matters is that automatically running programs written by advertising executives on your computer is a bad idea.

Just think. This particular Flash App, when it's deployed, probably adds several megawatts of power to the developed world's power draw, because it's on Facebook, and everyone uses Facebook. It uses enough power for me to notice individually. Multiply that by a hundred million Facebook users. Ouch.

The iPad doesn't support Flash. And I routinely get ten hours out of the battery. Coincidence? I think not.



( 7 comments — Leave a comment )
Feb. 6th, 2011 05:47 pm (UTC)
Surely the fault of the poor authoring tools?
Feb. 6th, 2011 06:42 pm (UTC)
Sure, if you're willing to insist that authoring tools be sentient. But that's actually a classic non-geek assumption: that computers think. Computers don't think. They do.
Feb. 6th, 2011 06:58 pm (UTC)
You misunderstand my verbal shorthand. It's possible to write Flash apps which don't busy wait. It's possible to write (say) JavaScript/HTML5 apps which do. So busy-waiting isn't a consequence of the use of Flash per se (i.e. not a fault of Flash). It might be just sloppy programming, or the consequence of the use of some poor authoring tools (i.e. sloppy programming on the part of the creators of the tools). My experience is that most programs, especially web programs, are written by people trying stuff almost at random, or cutting and pasting something which worked and tweaking it for a new use.

I'm curious: how did you diagnose the busy-wait? AFAIK I don't have any tool in my browser (Firefox 3.6) which lets me (say) identify the flash widgets in active tabs, or profile the relative CPU and/or memory use of active tabs and/or widgets. I would like to have such a tool.
Feb. 6th, 2011 07:37 pm (UTC)
Regarding diagnosis, it was obvious. In Chrome, you can just run top to see if there's a Chrome hog process. One Chrome process was taking 83% of the CPU. Examining what the app was doing, it was obvious that it was continuously redrawing a picture element whose motion was meant to be tied to the position of the cursor, even when the cursor wasn't moving.

How you'd do it in the general case, I don't know, which is another reason not to like Flash: at least Safari and Chrome, and possibly other browsers, include a built-in tool for profiling Javascript code. This is a clear weakness of Flash: because it is proprietary, and because one of its goals is to prevent you looking under the hood, tools aren't available for clearly diagnosing these problems.

But as to your point about verbal shorthands, I'm as guilty as you. Of course any programming language is subject to busy wait mistakes. I had a co-worker who spent $100k accidentally on mainframe CPU time because he didn't understand how do do event-driven code. The shorthand I was using here has to do with usage.

Of course Javascript/HTML5 is as susceptible to bad coding as Flash. But generally speaking, you never run Javascript to display an ad. Javascript, generally speaking, belongs to the web site you chose to visit. If this web site gives you a bad experience, you won't come back. Whereas the same Flash ad can pop up on any web site, and probably won't be consistently present on any one web site. So there's no cost to the ad designer for wasting your CPU time--you can't do anything about it, and if you aren't a geek you might not even realize why your computer suddenly got sluggish, or how you burned 10% of your battery in a few minutes.

I don't expect to see Javascript/HTML5 ads proliferate. There's substantially more work involved in deploying them, and the vulnerabilities are pretty obvious. Flash's strength is its weakness: it's really easy for tyros to use, and hence really easy for them to cause problems. Turning it off entirely, or selectively, is an easy fix, with very little real downside (particularly if you can turn it off selectively).
Feb. 6th, 2011 08:05 pm (UTC)
It's an arms race. The people who want to sell your attention will come up with new ways to get it.
Feb. 6th, 2011 09:48 pm (UTC)
Well, but that's not the point at all. I would personally prefer to pay a nominal fee for the content I receive, but I am not opposed in principle to ads. I am opposed in principle to things that are broken.
Feb. 6th, 2011 08:34 pm (UTC)
It is harder to narrow down flash hogs on Firefox, because all browser windows and tabs run in the same process. For people who browse like me (several dozen tabs at once) "top" and similar tools are very uninformative (they simply tell me that Firefox is using 97% CPU and 600MB of memory). Apparently there are now Firefox plugins to help narrow it down.
( 7 comments — Leave a comment )