News for hackers. Stuff that matters.
Re:V8: A Tale of TurboFan by mike_hearn on Thursday March 02, 2017 @04:14AM (#13772744)
It's interesting to watch the swings and roundabouts in the VM engineering space. I remember many years ago being in a Google engineering all hands where Android was first announced (to the firm) and the technical architecture was explained. I and quite a few others were very surprised to hear that they planned to take slow, limited mobile devices and run a Java bytecode interpreter on them. The stated rationale was also quite surprising: it was done to save memory. I remember being very dubious about the idea of running a GCd interpreted language on a mobile phone (I had J2ME experience!).

In the years since I've seen Android go from interpreter, to interpreter+JIT, to AOT, to JIT+AOT, to interpreted + JIT then AOT at night. V8 has gone from only JIT (compile on first use) to multiple JITs to now, interpreter+single JIT. MS CLR still doesn't have any interpreter and is fully AOT or JIT depending on mode. HotSpot started interpreted, then gained a parallel fast JIT, then gained a parallel optimising JIT, then went to a tiered mechanism where code can be interpreted, compiled and recompiled multiple times before the system stabilises at peak performance.

Looking back, it's apparent that Android's and indeed Java's initial design was really quite insightful. A tight bytecode interpreter isn't quite as awful as it sounds, especially given how far CPU core execution speed has raced ahead of memory and cache availability. If you can fit an interpreter almost entirely in icache, and pack a ton of logic into dense bytecode, and if you can use spare cores that would otherwise be idle (in desktop/mobile scenarios) to do profile guided optimisation, you can end up utilising machine resources more effectively than it might otherwise appear.

Re:V8: A Tale of TurboFan by pjmlp on Thursday March 02, 2017 @06:41AM (#13773379)
That was just catching up with what mainframes have been doing since the 60's. :)

That is why when you research old papers about compilers, Assembly opcodes and Bytecode are often intermixed.

It was common for the OS, language runtime to work with bytecodes, with the execution being done at microcode level. What nowadays would be better done as FPGA.

On IBM i (aka OS/400), there is a kernel JIT and all languages target bytecode, including C. Also JVM bytecodes get translated into IBM I ones. It was implemented in PL/M, nowadays with many modules ported to C++. And if you want to generate actual machine code, you need the Metal C compilers.

There are plenty of other examples.

Like with containers, it is just recycling mainframe success stories.

Re:V8: A Tale of TurboFan by mwcampbell on Thursday March 02, 2017 @09:21AM (#13774119)
Meanwhile, iOS has used strictly AOT compilation from the beginning for native applications. And doesn't iOS have a reputation for buttery smooth UIs? Perhaps the original AOT compilation approach of ART wouldn't be so bad if it didn't have to be done on the user's device, taking up the user's time while installing apps or upgrading the OS.
Re:V8: A Tale of TurboFan by mike_hearn on Thursday March 02, 2017 @10:15AM (#13774544)
Smooth but for many years, smooth only until you tried to switch app. iPhones did not have enough RAM to really do more than one thing at once, so multi-tasking came years late and the legacy of that is still present in the very weak, hacked together way in which apps collaborate on that platform. Perhaps this lack of emphasis on multi-tasking and linking apps together is partly due to the decision to AOT compile everything, creating additional RAM pressure. Also, Android was able to scale down to cheap devices, and scale up to very high end devices. Apple basically ceded the entire budget smartphone space to Android, they didn't even try, and budget phones (especially in developing countries) are often characterised by being RAM constrained.

So it's a complex topic.

Re:V8: A Tale of TurboFan by mwcampbell on Thursday March 02, 2017 @10:19AM (#13774595)
Interesting. I was under the impression that Android had more problems with RAM pressure than iOS, because Android uses tracing GC rather than reference counting. I observed that as late as the iPhone 6, the flagship iOS device had only 1 GB of RAM, whereas contemporaneous flagship Android devices had 2 or 3 GB. I guess I succumbed to the tendency to assume that Apple can do no wrong, leading to the assumption that Apple shipped with less RAM because they didn't need it, not because they were just being cheap.
Re:V8: A Tale of TurboFan by mike_hearn on Thursday March 02, 2017 @01:53PM (#13777003)
GC technically speaking can reduce the amount of RAM you need, as it can compact the heap whereas a native malloc can't. In practice this effect is hard to measure as the sorts of languages that use compacting GCs tend to be rather pointer and allocation happy, so any gain from compaction gets outweighed by other factors.

The specs of the various devices have changed over time. Android scales better: you can give it very little RAM, or a lot, and it'll make best use of it by e.g. keeping more background apps loaded at once. iOS was at least historically much less flexible about this: boosting RAM was not worth as much as in the Android space because app devs would still target older devices for quite a while and so the additional RAM would go unused, and the OS wasn't capable of using the spare for much due to the lack of multi-tasking.

Eventually Apple implemented Android-style task switching, so I don't know if that's still true. I haven't done any mobile dev for years. But I also think at some point Apple realised nobody who buys an iPhone actually cares about whether they're getting value for money or what the specs are, so they just stopped competing on that area. I mean they have never reduced the price of the iPhone once, right? Despite the huge fall in underlying component prices over the years. They could ship a device with 128mb of RAM and if it did the same thing as the iPhone 4 people would still buy it, simply because they see themselves as iPhone users and not "smartphone users.

Re:V8: A Tale of TurboFan by mwcampbell on Thursday March 02, 2017 @05:49PM (#13779055)
It goes to show that several factors influence the performance, memory usage, and real hardware requirements of a platform. I'll gladly admit that my original, simplistic assumptions were wrong.

ObjC, as typically written (i.e. more object-oriented than C), is also pointer and allocation heavy. so that might make compaction more of a benefit for Android. I wonder what proportion of memory in a typical Android application process is on the C heap rather than the garbage-collected, compacted heap. For example, where does image data usually end up?

To throw in another complication, are there any significant problems that come with layering a garbage-collected runtime on top of a high-level framework based on C heap allocation and reference counting? That's what Xamarin, React Native, and AOT Java runtimes (e.g. Multi OS Engine) do on iOS, and what .NET (even .NET Native) does on UWP. Or how about two garbage-collected runtimes in one process, e.g. Xamarin and React Native on Android?

Re:V8: A Tale of TurboFan by pjmlp on Thursday March 02, 2017 @12:28PM (#13776057)
Google did lots of work to fit Android on 512MB devices, also modern Android does use a parallel concurrent GC with just one pause. They also made the JNI stricter, in order to allow compactation on future versions.

Windows Phone .NET runtime also uses tracing GC, although the underlying UWP APIs are COM based.

Re:V8: A Tale of TurboFan by bitmapbrother on Thursday March 02, 2017 @08:04PM (#13779913)
An interesting watch:

Does Android use more memory than iOS?

https://www.youtube.com/watch?v=lCFpgknkqRE

Re:V8: A Tale of TurboFan by FullyFunctional on Saturday March 04, 2017 @02:16AM (#13789770)
His conclusion overlooks the key issue: memory burns power! This is why Apple tries to put as little in as possible.
Re:V8: A Tale of TurboFan by Twirrim on Thursday March 02, 2017 @10:10AM (#13774506)
iOS's advantage in this is that the hardware range is small. It's feasible to do full AOT away from the end user's device.

With Android there are hundreds or thousands of combinations of various bits of hardware and drivers, with various levels of compatibility. Device manufacturers have a terrific advantage in being able to produce hardware that is more specialised for the purpose, but there are trade-offs. Compiling all possible variations AOT away from the end device is going to be difficult.

Re:V8: A Tale of TurboFan by mwcampbell on Thursday March 02, 2017 @10:14AM (#13774541)
Not really. Practically all Android devices support ARMv7 binaries. They have to, in order to work with applications that use the NDK. If what you say about Android were true, then AOT compilation for Windows on x86 wouldn't be feasible either.
Re:V8: A Tale of TurboFan by tachydyne on Thursday March 02, 2017 @07:38PM (#13779793)
This comment reminds me, there are actually a bunch of Android tablets that shipped with Intel Atom x86 processors. Intel worked some magic with libhoudini that allowed them to run ARM binaries surprisingly fast. If you're an app maker who actually cares about those devices (like my employer) then you end up building all your native code for x86 too. I recall the ARM binaries running at more than 50% of the native x86 ones, running optimized threaded and vectorized image processing code. I wish Intel would talk more about that library. I'd love to know what techniques it used.
Re:V8: A Tale of TurboFan by mike_hearn on Thursday March 02, 2017 @10:15AM (#13774550)
The Play Store could AOT compile things on the server side. I don't know why it doesn't, it seems like such an obvious win.
Re:V8: A Tale of TurboFan by kllrnohj on Thursday March 02, 2017 @02:18PM (#13777263)
It's done on-device to handle the problem of linking. All calls from the app into framework have to be linked when you do AOT, and Google can't do that without having device images of every android device and compiling on-demand for whatever device is requesting the install.

And then you'd still have to fall back to on-device compilation if Play Store doesn't know what the device is anyway (aka, all custom ROMs), or when you take an OTA (otherwise you'd need to re-download all installed apps for the new OTA image).

Plus, the dex bytecode is considerably smaller than the AOT result, so wire transmission costs are cheaper doing on-device compilation - call it a form of compression.

Re:V8: A Tale of TurboFan by esprehn on Thursday March 02, 2017 @11:16AM (#13775207)
The binaries produced from the AOT process are too large to download over the network for typical users and data plans.
Re:V8: A Tale of TurboFan by pjmlp on Thursday March 02, 2017 @12:29PM (#13776061)
Yet Apple and Microsoft managed to do it.
Re:V8: A Tale of TurboFan by mike_hearn on Thursday March 02, 2017 @01:55PM (#13777030)
In fairness, they managed to do it only for the highest of high end users. Apple institutionally doesn't care about anyone in places like China, India or Africa where data is expensive. That's why Android completely dominates the global smartphone market share.

I guess nothing would stop the Play Store from AOT compiling binaries just for users with fast connections though.

Re:V8: A Tale of TurboFan by pjmlp on Thursday March 02, 2017 @02:14PM (#13777223)
Windows Phones were widely available in India, before everything kind of felt apart, though.
Re:V8: A Tale of TurboFan by kllrnohj on Thursday March 02, 2017 @02:20PM (#13777283)
Microsoft ships .NET bytecode (so basically the same thing Android does) and Apple doesn't care about the low end.
Re:V8: A Tale of TurboFan by pjmlp on Thursday March 02, 2017 @02:38PM (#13777457)
No it doesn't.

WP 8 and 8.1 use MDIL, Machine Dependent Intermediate Language, basically machine code with jump targets kept as symbolic links, replaced at installation time.

WP 10 uses .NET Native.

I can gladly point out the respective Microsoft documentation, BUILD and Channel 9 presentations.

Re:V8: A Tale of TurboFan by pjmlp on Thursday March 02, 2017 @12:23PM (#13775991)
Yet, now bitcode is a thing on iDevices, to the point LLVM guys started to discuss a possible portable version without the low level hardware bitcodes.

The difference from Apple and Microsoft's bytecode solutions versus Google, is that the AOT compilation of bytecode takes place at the store, instead of the device.

Re:V8: A Tale of TurboFan by kllrnohj on Thursday March 02, 2017 @02:14PM (#13777222)
iOS's smoothness doesn't come from being AOT. If it did you'd have seen a huge difference in responsiveness in Android between interpreted, JIT & AOT, but you don't. Raw code perf does improve from interpreted -> JIT -> AOT, but the rendering is part of the platform not the app and that's been C++ since the beginning.

dalvik's GC was a big problem for smooth UIs, though.

Re:V8: A Tale of TurboFan by flat6turbo on Thursday March 02, 2017 @09:55AM (#13774365)
ios used to be buttery smooth, until they decided to put probably 10,000 instructions behind every button press event.