Former Sun Mobile JIT Engineers Take On Mobile JavaScript/HTML Performance
First time accepted submitter digiti writes "In response to Drew Crawford's article about JavaScript performance, Shai Almog wrote a piece providing a different interpretation for the performance costs (summary: it's the DOM not the script). He then gives several examples of where mobile Java performs really well on memory constrained devices. Where do you stand in the debate?"
An arbitrary tree of arbitrary objects with arbitrary methods of manipulating them thanks to years of incremental changes that are never properly documented (quick! point to the document showing that a select tag has a value attribute!) and are never deprecated.
I think Drew's article wasn't that performance had to suffer, it was that garbage collection isn't free. It has to take place, though, so it's not an O(GC)=0 component. If garbage collection takes place in a lot of memory, but not -enough- memory, it takes a very long time, in real time. Depending heavily on the application, it may be very visible at the UI level.
Programmers intent on using all of the resources available, and performing intensive tasks, should think about means other than garbage collection.
While this post is a valuable addition to Drew's analysis, I feel it's not really a rebutal at all.
Yes, JavaScript is slow for the reasons Drew mentioned and yes, the DOM is a nightmare to optimize for responsive UIs. They're both right.
While this blog also provides some nice insight into how you can have acceptable performance with a GC on mobile, it's not offering any workable alternative that would work for JavaScript. So Drew's article still comes out pretty strong, IMO.
I dislike the separation of 'Perceived' vs 'Actual' performance. If I perceive it to be slow, it's slow. This reminds of of the Firefox devs that spent years saying how if an add-on makes their browser a memory hog and a slowpoke, it's not their problem because their performance is fine.
Devs.. If it's slow, it's slow. Call it perceived, call it actual, call it the Pope for all I care. It's a Slow Pope.
Crawford brought in lots of data on real-world performance. (e.g. http://sealedabstract.com/wp-content/uploads/2013/05/Screen-Shot-2013-05-14-at-10.15.29-PM.png)
Almog's rebuttal has a lot of claims with no actual evidence. Nothing is measured; everything he says is based on how he thinks things should in theory work. But the "sufficiently smart GC" is as big a joke as the "sufficiently smart compiler", and he even says "while some of these allocation patterns were discussed by teams when I was at Sun I don't know if these were actually implemented".
Also:
I'm a professional game programmer, and I'm laughing at this. If you're making Space Invaders, and there's a fixed board and a fixed number of invaders, that statement is true. If you're making a game for this decade, with meaningful AI, an open world that's continuously streamed in and out of memory, and dynamic, emergent, or player-driven events, that's just silly. For Mr. Almog to even say that shows how much he doesn't know about the subject.
If they care about performance why not use assembly?
FTFY
Perhaps it's a difference between throughput and latency. Nine moms can make nine babies in nine months, offering nine times the throughput of one mom, but each baby still takes nine months from conception to completion. Users tend to notice latency more than throughput unless an operation already takes long enough to need a progress bar. Some algorithms have a tradeoff between throughput and latency, which need fine tuning to make things feel fast enough.
There are also a few ways to psychologically hide latency even if you can't eliminate it. The "door opening" transition screen in Resident Evil is one example, hiding a load from disc, as are some of the transitions used by online photo albums to slowly open a viewer window while the photo downloads.
The separation is useful to understand where the optimization is necessary. JavaScript could be made less painful if it didn't have DOM manipulation to contend with obviously that's not very practical.
If they care about performance why not use assembly?
Some people do (in a sense) use assembly when they use C. If a particular inner loop is running too slowly, an expert programmer might look at the code that the compiler generates to see what's going wrong. For example, an inefficient translation of C to assembly language might point to needing const or restrict qualifiers to allow the compiler to make certain optimizations.
I guarantee Javascript will perform much better once we get to 16 cores and 3.6Ghz on the standard mobile device.
Join the Slashcott! Feb 10 thru Feb 17!
1) You need 100 times more time to program a certain program if you do it in assembly
2) You need X times the time if you want it running on X platforms (can even be the same processor)
A long known fact in programming is: your programmers productivity in terms of lines of code per day is FIXED. REGARDLESS of language.
That simply means a programmer who is able to "correctly" write 25 - 50 lines of Java per day will also only write like 25 - 50 lines assembly per day.
This is the main reason why we migrated from bytecode/machine code to assembly and from there to MACRO ASSEMBLERS and from there to Fortran/Cobol and from there to Lisp/SmallTalk.
Then came the many mixed in solutions like C (a portable assembler) / PL1 / ALgol / Pascla /Java etc.
Higher abstraction levels make it possible that programmers are 100 even up to 1000 times faster than our grandfathers with assembly.
Cost free eBook I read (by iBook/Kobo/Amazon/ObookO/Gutenberg etc.): "The Green Odyssey" by Philip Jose Farmer.
I actually agree with almost everything Drew wrote with the exception of his GC statements, I worked for an EA contractor in the 90's doing large scale terrain streaming on what today would be a a computer less powerful than an iPhone so while my game programming experience might be outdated its still valid. Saying that I don't know if its actually implemented only referred to the last section. Like I said, I actually worked on the VM code as well as the elements on top of it. As I said in the comments to the article never might have been harsh but I pretty much stand by it. If you use a GC you need to program with that in mind and design the times where GC occurs (level loading). Most of your dynamic memory would be textures anyway which are outside the domain of the GC altogether and shouldn't trigger GC activity. To avoid overhead in a very large or infinite world you swap objects in place by using a pool, no its not ideal and you do need to program "around" the GC in that regard. OTOH when we programmed games in C++ we did exactly the same thing, no one would EVER allocate dynamic memory during gameplay to avoid framerate cost.
I find it very amusing that we are having this conversation on a website that just deployed the slowest suckiest mobile website I have ever seen.
This whole conversation should have been retitled "Why web apps are slow on mobile", and not about JavaScript at all.
The comparison between Objective C and Java is totally ridiculous and beside the point; it's the only thing which ties this article to actual mobile Apps instead of web apps, and fails to address the original articles comments on JavaScript.
In principle, Objective C the language can be used for dynamic binding; in practice, the Objective C runtime, as represented in crt1.o, and in the dyld and later dyle dynamic linkers, it can't be. This was an intentional decision by Apple to prevent a dynamic binding override from changing aspects of the UI, and to prevent malicious code being injected into your program - this is a position Apple strengthened by adding code signing.
Comparison to Java of Objective C as a proxy for comparing iOS applications to Android applications is also ridiculous: there are Android native apps, they are just more difficult to write than the Dalvik Apps. Angry Birds, and most media applications, are not run under the Dalvik VM, but are instead run on the native instruction set.
The whole argument made by the article author is not a rebuttal; it's an attempt to twist the conversation in an attempt to beat on his personal hobby horse.
It says that JavaScript is inherently slow because of DOM. It says that this should not be applied as a sweeping generalization to all managed languages e.g. Java. Then it gives examples including mobile Java performance where small heap devices work just fine.
The article mostly agrees with what Drew said with very few exceptions. The article points at Asha devices (and other devices) that have very small amounts of memory (2mb) and yet perform really well with a GC (and a slow CPU).
The GC study pointed in the Drew article was a desktop study taken out of context.
You're wrong. But not in the way you think. The ability to write abstractions, and an obsession with design patterns, often results in needless abstraction and a loss of focus.
Libraries like C++ STL and Java Collections only make this worse. Why? Because your application is basically one giant data structure. I forgot whether it was Dennis Ritchie, or Ken Thompson, or whomever, but it was once that said good programming is about implementing the correct data structures. Note that this doesn't mean reusing generic data structures; it means drawing upon the universe of conceptual data structures to craft a narrowly focus data structure and algorithm that fits your problem.
If you start out with generic data structures, then you end up working backwards. You have lots of boilerplate code just to use those generic data structures, which you then aggregate and glue together in the same way you would have done from scratch, but with far less code. Programming is not about splicing hash tables together with trees ad hoc. This is a ridiculously reductive perspective on what is programming and how good programs are written.
This is why, almost without fail, real-world applications developed in high-level languages tend to have ridiculously high source line counts. People get carried away with abstraction, including the language implementors with their plethora of generic data structure libraries.
IME, C is *the* sweet spot, if you had to pick a single language. However, you can do better by mixing and matching different languages, especially where they offer unique characteristics--functions as first-class objects + real closures (i.e. not C++ lambdas), coroutines, exceptional optimizing support (e.g. Fortran for iterative numerical algorithms), and functional language characteristics which make is easier to express some kinds of data structures and algorithms. This is why I stay away from C++; it's too much of a pain to mix C++ with various other languages, where almost every language in existence has strong C support.
If people spent as much time maintaing proficiency in different languages instead of trying to do everything in a single language (and grappling with the inevitably cognitive dissonance when defending their choices on web forums), then the world would be a far more efficienct and less bug-prone place.
+Bravo, nailed it.
"His name was James Damore."
Until you hit the performance / out of memory wall. When you have to write a serious application, and one day you will, if you do not mind how many resources your application is using will be "hasta la vista" to you.
Religion: The greatest weapon of mass destruction of all time
If they care about performance, why not design custom ASICs?
FTFY
SIGSEGV caught, terminating
wait... not that kind of sig.
Because DOM *also* re-composes constantly. I saw a Javascript app once that I sincerely *hope* was a joke. It literally tried to emulate a bitmap (pre-Canvas) by fetching a 1x1 cgi-generated PNG image for each unique RGB color used & assembling them mosaic-style into what must have been a 200 megabyte DOM just to draw a 640x480 bitmap pixel by pixel using absolute positioning. It actually *crashed* Firefox, and choked IE 8 for almost 20 seconds (especially the first row, and the last hundred or so rows).
Yeah, mosaics of 32x48 tiles were cute 10 years ago, but Jesus H Christ... SINGLE-PIXEL PNG images generated on the fly & fetched for each unique RGB value? Total *madness*. I wish I could find the site again, just because it was a case study in "how to utterly abuse HTML and browser-based apps."
And yet C would be so much nicer if it had a generic list library......
"First they came for the slanderers and i said nothing."
Thanks. The thing I was unsure about here is whether this is practical in a mobile VM with the added constraint of very limited CPU cache which is the really interesting bit here.
you misspelled frist!
David F Bacon designed some GCs for realtime applications that used reference counting with deferred cycle detection. The longer you defer cycle detection for, the higher the probability that the object will already have been proven to be non-garbage (by having its reference count incremented again) or garbage (by having its reference count reach 0 and it being collected). The trade for this is that it increases the maximum time for the cycle detector to run. You can adjust the delay based on the latency constraints of your application.
I am TheRaven on Soylent News
Apps seem to launch instantly since they have hardcoded splash images
What? iOS-Apps are made to lie regarding their startup-performance? That's golden. 10/10.
it was once that said good programming is about implementing the correct data structures. Note that this doesn't mean reusing generic data structures; it means drawing upon the universe of conceptual data structures to craft a narrowly focus data structure and algorithm that fits your problem.
And yet the domain of problems that most programs out there deal with is so ridiculously small that, in practice, you're far better off just implementing the common data structures once in the standard library and reusing them. Which is precisely why everyone does just that.
This is why, almost without fail, real-world applications developed in high-level languages tend to have ridiculously high source line counts.
No, that's usually because real-world applications developed in high-level languages are far more complex (in terms of use cases they cover and code paths they necessarily need) than real-world applications developed in low-level languages. And that's because most people rightly choose the higher-level tool to deal with the increased complexity. When you actually compare a, say, C app with a C++ app that does the same thing (including all the UI interactivity etc), the C++ app will be shorter by a lot.
How close is this model to what Python does (which is also refcounting + an occasionally running cycle breaker)?
is that you can still write a phonegap app that works on damn near anything you want it to in the time it takes the average Java team to tie their shoes and 95% of the time it's not going to perform adequately, it's going to perform better than what they tried to do in the native language. We can talk benchmarks until we're blue in the face, but in six years of web dev I've seen a lot of Java code bases and not one was competent. Modern JS JITs kick ass. Modern Java (and I'd argue C# as well) devs at the median level are pattern-swilling, enterprise-blinded, kool-aid snorting jackasses who can't write clean, minimalist, maintainable, good old fashioned DRY-lovin' code to save their lives.
Sun engineers can talk perf if it makes them feel better but they're missing the point. Decent JS devs do it faster, better, stronger, with smaller teams and robustly across a lot more platforms than equivalent-skilled Java devs could hope to in their wildest dreams. The reason? JS gives you all the rope you need to hang the shit out of yourself. When that happens you have two options. You quit. Or you learn more and you get better. And that's when you start noticing all the useful things you can do with all that rope that languages built with mediocrity-support in mind will never be able to do as efficiently.
And I'll be fair. When performance is really seriously hyper-critically important... you're better off with C (binds very nicely to JS via V8 might I add). Fuck Java.