« January 2007 | Main | March 2007 »

February 16, 2007

Apple's Take on Application Programming Environments?

This is a followup to yesterday’s post on Application Programming Environments. As I thought more about what I wrote, it brought to the forefront some stuff I’ve been wondering about in this area. Specifically, does Apple have a comprehensive strategy/philosophy/outlook on the issue of native code vs. JIT code, or on Objective-C vs. dynamic languages? If so, could someone please explain it to me (and no, I’m not being facetious - I really want to know).

But First, A Mini-rant on Objective C

I don’t like Objective C. Never have. There. I said it. Flame away!

Why don’t I like it? First, let me say what I do like: I think the runtime is amazing in the way it combines dynamic behavior with native speed. I think Cocoa is fantastic. Toll-free bridging is brilliant. But all this discussion of bridging between native languages like C and Objective C and dynamic languages like Ruby and Python has really crystalized for me what I dislike about Objective C: the constant need to do a cognitive shift back and forth between dynamic Smalltalk goodness and static C minutiae. Here’s an example of what I’m talking about, courtesy of Wil Shipley:


- (id)_realItemForOpaqueItem:(id)findOpaqueItem outlineRowIndex:(int *)outlineRowIndex
    items:(NSArray *)items;
{
  unsigned int itemIndex;
  for (itemIndex = 0;
       itemIndex < [items count] && *outlineRowIndex < [self numberOfRows];
       itemIndex++, (*outlineRowIndex)++) {
    id realItem = [items objectAtIndex:itemIndex];
    id opaqueItem = [self itemAtRow:*outlineRowIndex];
    if (opaqueItem == findOpaqueItem)
      return realItem;
    if ([self isItemExpanded:opaqueItem]) {
      realItem = [self _realItemForOpaqueItem:findOpaqueItem outlineRowIndex:outlineRowIndex
          items:[realItem valueForKeyPath:[[self _treeController] childrenKeyPath]]];
      if (realItem)
        return realItem;
    }
  }
}

This mix of id and C-style pointer dereferencing just sets my teeth on edge, and after years of using “scripting” languages I’m not super-fond of all those square brackets either. The equivalent code in Ruby would be both more readable and more succinct. I am, of course, aware that this is a subjective aesthetic judgement. If you don’t mind this style of programming, I’m happy for you. But for me it was enough to keep me out of the Cocoa/Mac OS X programming universe - kinda ironic since I once worked in MacDTS and knew more about Mac internals than most folks.

Apple’s Investments in JITs and Dynamic Languages

So despite my inability to overcome my aversion to Objective C, I still try to keep up with all things Mac OS X. It is clear that Apple has a strong commitment to Objective C. They’re making lots of improvements in Objective C 2.0. But they’re also making some interesting technology investments around JITs, dynamic languages, and so forth:

  • Core Image has a nifty JIT compiler that takes a GLSL code and dynamically compiles it for the target GPU or CPU. Cool.

  • OpenGL in Leopard uses LLVM to dynamically recompile graphics code on the fly for systems that don’t do hardware vertex shading. This code replaces a previously existing JIT compiler (who knew!).

  • Apple is doing LLVM work targeting the ARM processor. Is this for the iPhone or something else? Beats me. But using LLVM would allow Apple to build a higher level Java-style VM on top of it but targeting Objective C, or it would let Apple dynamically recompile programs for new processors (kinda like Rosetta on steroids) and architectures: no more fat binaries. Or it could be something else entirely.

  • Apple is shipping Java with OS X but doesn’t really put a lot of investment into it. They killed the Cocoa-Java bridge. Can’t say I miss it.

  • They ship Python, Ruby, and Perl with Mac OS X. New bridge technology makes these quite usable with Cocoa, which is cool, but it isn’t clear whether or not these bridge technologies will be included with Leopard or not.

On the other hand, you have the current situation with fat binaries: PowerPC, x86, x64, and maybe even ARM binaries. This is obviously not a scalable solution when you start talking about optimizing for many different configurations of CPU cores, GPUs, etc.

Microsoft’s Strategy

By comparison, Microsoft’s strategy is obscenely simple: .NET everywhere, and dynamic languages are welcome to come along for the ride. IronPython, JRuby, etc. are all supported on .NET. LINQ adds more dynamic behavior to C# and VB.Net. .Net Compact is part of the Windows Mobile story. WPFE will someday have a miniCLR that runs on Macs and maybe elsewhere. Microsoft has buried the hatchet with Novell (unfortunately they seem to have buried the hatchet in the back of the Linux community, but that’s another topic), so the Mono team no longer needs to worry about patent litigation from Microsoft. Heck, they even want Apple to support Cocoa#.

A Plea to the Lazyweb

What I want to know is this: exactly where does Apple see the future of JIT compilers and dynamic languages? Will Apple maintain a slavish devotion to natively compiled Objective C, or do they plan to start moving more and more to dynamic language development as so many others are doing? I’d love to hear what someone from Apple has to say, of course, but welcome contributions from other industry watchers.

One Last Tangent

As an update to my earlier piece, see this interview with Mark Hamburg about the use of Lua in Lightroom:

So what we do with Lua is essentially all of the application logic from running the UI to managing what we actually do in the database. Pretty much every piece of code in the app that could be described as making decisions or implementing features is in Lua until you get down to the raw processing, which is in C++. The database engine is in C; the interface to the OS is in C++ and Objective C as appropriate to platform. But most of the actually interesting material in the app beyond the core database code (which is SQLite) and the raw processing code (which is essentially Adobe Camera Raw) is all in Lua.

[Update 02-17-2007] Corrected some editing errors and typos.]

February 15, 2007

Desktop Application Programming

There has been an interesting discussion going on about the future of desktop applications, involving John Gruber, Jesper, and Daniel Jalkut, with many comments form other luminaries in the Mac programming world. There were a number of themes in the conversation, but the one that struck a chord with me came from this quote from John Gruber:

I think a few years from now, desktop application programming is going to look more like web programming does now, with most of the lines of code written in scripting languages, and the performance-sensitive parts written in C / C++ / Objective-C.

Now I have a lot of respect for John and the other folks involved in this conversation, but I have to say that this whole line of reasoning suffers from several flaws:

  1. First of all, what John is predicting has already happened to a good extent. On the server side, languages like C have long been supplanted, first by Java (JSP, J2EE, etc.) and .NET (ASP.NET, etc.), more recently by PHP, Ruby on Rails, and so forth. On the desktop, many games have a substantial portion of their functionality implemented in dynamic languages like Lua (a.k.a. scripting languages, but I hate that term). Adobe’s Lightroom has a substantial amount of code written in Lua as well. Safari and Firefox contains functionality implemented in JavaScript (not to mention Dashboard) and the next version of Firefox will use JavaScript extensively. Most of the Adobe CS2 applications have extensive ExtendScript interfaces. Photoshop CS3 allows extensibility via Flash/ActionScript. And so on and so on. The trend is for less and less of the code to be written in C/C++ over time.

  2. John qualified his prediction with the statement that “most of the lines of code” would be written in scripting languages. The inadequecies of measuring LOC are well known so I won’t belabor them here. A better measure would be to compare the number of man hours spent developing C/C++ code vs. the number of hours developing dynamic language code, or the percentage of functionality implemented in dynamic languages, or some such. My prediction is that any one of those measures will show a continuing trend towards dynamic languages over time.

  3. It assumes, incorrectly, that dropping down into native C/C++ is the only/best way to get good performance. This is often not the case: JIT-ed languages can often outperform native code on certain types of algorithms, particularly those that benefit from whole-program optimization. That said, there are certain types of operations that do perform better in C/C++ than in current implementations of dynamic languages, and dropping down into C/C++ is often the most expedient way of correcting those problems.

  4. A more compelling reason for dropping down into C/C++ is that there is so much good C/C++ code out there that it doesn’t make sense to rewrite. Look at Lightroom, for example: it heavily leverages Adobe’s decades of work on image processing. I don’t have hard numbers, but I wouldn’t be surprised if a majority of the new code in Lightroom was written in Lua. I expect more and more applications to evolve in this manner: taking an existing code base and building new functionality onto it using dynamic languages.

  5. It ignores the problems associated with scaling existing C/C++ code into “our megacore teraflop future”. When you think about things like Intel’s recently announced 80-core system, the massive parallelization available in GPUs, and so on, leveraging existing single-threaded C code (even when using MMX intrinsics) suddenly doesn’t seem so compelling compared to code written in languages that were built for parallelism like Haskell and Erlang, especially when combined with a multicore-aware JIT compiler. There is a ton of good discussion going on in this area lately.

  6. Can’t comment on this without mentioning Apollo. Desktop applications built on top of a high quality JIT compiler, with security, portability, and good performance.

Bottom line is that the future is here. Desktop application programming is not what it once was. Dynamic languages are an integral part of programming applications today, and will be even more so in the future due to the advent of massive parallelism.

February 12, 2007

SuperFetch Rocks!

I’ve been using Vista as my main OS on my desktop PC for quite some time now - knowing what Vista can and can’t do is part of my job. One thing I’ve really been impressed by is the SuperFetch feature, which intelligent preloads application code into memory based on a machine learning algorithm.

Now Patrick Schmid of TG Daily has done some benchmarking on the performance effects of SuperFetch and ReadyBoost on application launch times, using Outlook, iTunes, and Acrobat as sample applications. Its an interesting article, well worth a read. His conclusions, though: he recommends buying a 1GB machine then buying a USB Flash memory stick. I’m not sure I’d agree: he didn’t actually test with 2GB of RAM.

Based on my experience with a 2GB machine, I recommend that as the way to go if you can afford it. PhotoShop CS3 launches a lot faster than CS2 did, but with SuperFetch it starts up super quickly - sweet! I'm told that an x64 machine with 4GB+ of memory is even more impressive when launching PhotoShop. :-)

Kudos to the team(s) at Microsoft who pulled off this nice piece of applied research.

ECMAScript the Next Big Programming Language

Steve Yegge of Google has an interesting post up on his blog about The Next Big Language. In it, he discusses what he feels are the necessary criteria for a successful programming language, but doesn’t actually name the language he thinks will be it. What is most fun about the article is reading the comments from people trying to guess the language he is discussing: so far ECMAScript (aka ActionScript 3) as implemented by Flash/Flex/Apollo/Tamarin seems to be the leading contender, though Groovy is also mentioned a fair bit.

For my part, I’m not sure I agree with all of his criteria, especially the ones around static typing and the “Kitchen Sink”. That said, I find the fact that people are focusing on dynamic languages as the next big thing to be very encouraging.

February 06, 2007

Jobs on DRM: The Elephant in the Room

Today Steve Jobs posted a blog-style message on the Apple website about iTunes and DRM. It was a highly polished, well written piece. Many people have written about it: some have speculated about whether or not Jobs has enough clout to make it happen; others have pointed out that this is really a way for Apple to position itself against European arguments against iTunes/iPod lock-in; others have questioned how sincere Jobs really is in asking the labels to change without really making any changes on its own unilaterally. Personally, I think all these people have good points, but despite all that I think the statement was a good move for Apple and the industry. I hope Jobs continues to discus things in the open instead of retreating back into the Infinite Loop bunker.

That said, I find it interesting that no one is pointing out what Jobs didn’t say anything about: DRM and video. Last time I checked, Apple also sold TV shows, music videos, and films on iTunes Music Store, and they are all protected by FairPlay DRM. Why didn’t Jobs make the same courageous stand against DRM on video? Unfortunately, the answer isn’t very pretty: Apple doesn’t have anywhere near the same clout in the movie and TV business that it has in music, and has only signed film deals with two of the major studios as a result. Taking a stand against DRM for movies would anger the same people he is trying to make deals with. So much for courageous principles - the people who think his message was mostly about positioning in Europe are spot on.

[Update 2-6-2007] I just realized that I kind of cribbed the title from my post from Tim Bray, who posted an article that used it as a subhead only two weeks ago. Credit where credit is due... I've also added a digg link as an experiment, and corrected a few typos.