Odds and Ends

  • Today I finally grokked how the Java2D Image API could be implemented. Basically the biggest problem (to me) always has been the BufferedImage constructor, which allowed BIes of all kinds to be created. What about image formats that are not supported by the native rendering engine? How are we supposed to render into that? Well, the solution would be to have 2 buffers like:
    RE --> nBuffer  < -- > jBuffer <-- Java2D API

    where the native buffer has a format that is supported by the RE and the java buffer (I call it so, it is not required to have that in Java, could be any memory area thanks to the fine DataBuffer abstraction) holds the data in the requested ColorModel/SampleModel. Well, the obvious problem here is when to sync the data and what to sync. Fortunately (that’s what I discovered today), BufferedImage implements WritableRenderedImage, which provides a couple of methods to grab the buffer for writing and release it. This is find, as it provides very nice syncpoints.One remaining obstactle is when the RE does not support a super-format (I call it like this. For instance, ARGB8888 is a superformat of RGB565) of the requested format. For instance, when the RE only supports ARGB1888 (which is the case for one system I am currently implementing this API for) but the app requires ARGB8888. How is the RE supposed to render into that. The API is a little weak in this respect as it doesn’t allow to reject the request. The only real solution would be to have a Java only renderer do the rendering, but this is very ugly for several reasons: The rendering would be noticably different and noticably slow (well the latter isn’t that bad. what do you expect when you request something not supported by the HW). Any comments and ideas on this are very appreciated.All in all, nowadays I feel about Java2D almost like I feel about NIO: Quite confusing for the start, but once things are sorted out and have found their place in the mind-picture, it seems relativly well thought out and nice. Only problem with Java2D probably is that it carries quite some cruft from the <= AWT1.1 days, which makes it even more confusing.

  • Today a collegue pointed me to this, makes my mind boggle. I copy it into here as a riddle to solve. So please make up your mind and find out what this is supposed to do. And yes, this is legal C code compilable by GCC. 🙂
    switch ( count % 8 )  /* count > 0 assumed */
    case 0:        do {  *to++ = *from++;
    case 7:              *to++ = *from++;
    case 6:              *to++ = *from++;
    case 5:              *to++ = *from++;
    case 4:              *to++ = *from++;
    case 3:              *to++ = *from++;
    case 2:              *to++ = *from++;
    case 1:              *to++ = *from++;
    } while (( count -= 8 ) > 0);
  • Found an interesting JSR: Application Framework for Swing Gotta have a deeper look at it ASAP.

Happy interpreter hacking

The last couple of weeks I got my hands dirty (really dirty) with VM hacking, namely the Java interpreter of the JamaicaVM. I’m applying a couple of well known optimization techniques (besides loads of cleanup). The most promising so far (I’ve done only prototype-like implementations so far) are:

  • Superinstructions. Combines frequent ‘cheap’ instruction sequences into bigger units. For instance, in order to add two values, a common sequence is iload* iload* add istore*. Of course, there are variations (like only one load or without store and many more). Such a sequence would be combined into a new opcode which would do all this without accessing the operand stack. The net effect is that the overhead of moving stuff onto and from the stack is saved as well as the dispatch overhead between these instructions (which is quite significant as it is at least as expensive as the operations themselves in these cheap cases). Theoretically it would be possible to eliminate operand stack completely (this is called stack folding and implemented in one of the java processors iirc — quite amazing stuff), but this would unnecessarily bloat up the binary. Instead I’m doing some statistics to find the most frequent patterns and implement these.
  • Threaded dispatching. So far the interpreter has been implemented in a straightforward while-switch fashion. This creates a certain overhead, because we have to jump into the bytecodehandler, out of the handler and back to the loop condition. That’s pretty bad, especially considering that many opcodes are really cheap, and it tends to intercept the processor pipeline badly. The better way to do this is to jump directly to the next bytecode handler at the end of the previous one. That’s called threaded dispatching. Unfortunately, you can’t really implement this with ANSI C. So I did quite some macro magic and utilized a GNU C specific feature (label pointers). The outcome was quite impressive, ~15% improved performance in one go. After having gone through all this I was told by Ian Rogers that GCC has a -funswitch-loop optimization flag which makes a threaded dispatcher out of a while-switch dispatcher. Gotta compare it to my homegrown implementation. I really hope that mine does better than GCC’s one. I don’t like wasted weekends very much 😉

I’d like to implement some more intersting stuff, like direct dispatching or a primitive JIT, but that’d require to be able to get hold of some contiguous memory, which isn’t exactly easy in Jamaica (due to the hard realtime nature of the GC). Let’s see how this works out in the end.
On a related note, I’ve been digging into the Hotspot and JikesRVM sourcecode lately, mostly out of curiosity (until now). I’m quite excited by the idea of a VM implemented mostly in Java itself, even hosting itself. It requires some magic, but in my experience, beeing able to do things in Java without needing to touch C makes development more rapid and tends to produce better and more readable code. And if you think a VM implemented in Java itself must be slow, think again.

Audio CD URI?

Dear Lazyweb,

I’d like to configure GNOME to start up rhythmbox when an audio CD is inserted, and start playing this CD. I see that there is a program named rhythmbox-client which takes the commandline argument –play-uri to play whatever I tell it to. However, I can’t seem to find any documentation or whatever about an URI for audio CDs. So, is there an URI for audio CDs in gnome-vfs and if yes, what is it?

Looking for a job

In april this year my time as student will finally end. During the last two years I have been employed by aicas, which meant that I’ve been able to work on what I like most, GNU Classpath (not exclusively, but for a significant part). That has been a great time, but I’d like to do something new now. I am starting to look for opportunities and exciting new jobs that might be waiting for me to be done. So, if you are looking for a creative and productive programmer, or know somebody who is, you might review my resume and interests and contact me if you’re interested.

Borrowed Tune

I’m climbing this ladder
My head in the clouds
I hope that it matters
I’m having my doubts

Native properties? Please not!

Seems like there are many opinions about native (language-level) property support for the Java language. I guess I am more the conservative type of programmer. I can’t figure out enough good reasons to add such a feature to the language without introducing a whole lot of possibilities to shoot yourself into the feet.

One of the cool things about properties (IMO) is that they are actually _not_ necessarily only getter and setter methods around field. If I understand it correctly, this is the whole idea behind properties. You can implement certain behaviour for properties (like, dynamically creating/fetching the property values). How would you do that if your property looks similar to a field? Ok, you could probably still write getters and setters, but this is where it starts confusing. And, honestly, auto-boxing, varargs, static imports, that is more confusion to the originally-clean language Java. I don’t need more of this.

But then again, maybe all those discussions will finally lead to a clean implementation of native properties. Then I will of course not stand against it. But please don’t add such half finished shit like it happened with auto boxing and some other things in jdk5. Ah, and did I already mention that I don’t like the idea of language-level XML support either? I mean, there’s JDOM, a couple of objectXML mappers etc. Who needs language level XML support then? But ok, this is another story…

5 minutes pasta

Today we were hit by a suprising holiday and no chance to buy stuff for the weekend. So we had to improve a little. Out comes a very delicious and simple recipe that I want to share with you. You cook spagetti like usual. Then you mix (fine italian or greek) olive oil with garlic (I prefer lots of, protects against vampire-like creatures 😉 ) and chili and serve the spagetti with this fine dressing and parmesan cheese, maybe drink a good wine with it. Simple and delicious. Enjoy your meal!


Today I found this gem on /. (must be the needle in the hay). This is simply ridiculous: Having an XML tag (or attribute, whatever) named autoSpaceLikeWord95 with the explanation: To faithfully replicate this behavior, applications must imitate the behavior of that application, which involves many possible behaviors and cannot be faithfully placed into narrative for this Office Open XML Standard. If applications wish to match this behavior, they must utilize and duplicate the output of those applications. Eh what? This is about to go into some kind of ‘official standard’? WTF?
And that is not the only one. This (6000 pages) spec seems to be full of it. I agree with the above article: This is not a specification; this is a DNA sequence.

*Laughing my ass off*. Oh wait, this is actually not funny. :-/

One Laptop Per Child revisited

In my latest rant about OLPC I probably sounded a little extremistic. I’d like to add that I don’t think that computers are generally bad for children. Especially in the developing countries there is probably good use for them, especially when it is not possible to have schools or something similar. I think I got a little annoyed because many discussions that I’ve seen take the OLPC as a cure for many problems that it can’t be, and the focus is mostly on technical issues, on 3rd world issues and others, but not on the children. When done right, the OLPC is surely a nice opportunity to help the developing countries. For this to work we would have to include the children and the special needs of 3rd world countries into our focus. And that doesn’t (only) mean childish themes and desktops. That means reasonable and sustainable pedagogic concepts that are tailored to these children in these countries. If there’s documents about this please point me to them, I am really interested.

A better comparison

I did a run of GNU Classpath’s AWT and J2D benchmarks. The results are more fair now (JDK6 vs Cairo vs X/Escher). Cairo now beats the X peers, but the overall speed is still impressive given that it’s done all in java. I guess my own small benchmark was a nuance too artificial 😉