IASRA is a stupid recursive acronym!

•October 19, 2009 • Leave a Comment

To recurse or not to recurse? This is of course a brilliant idea if you’re in search for a cool and geeky name for your new pet project. But is it also wise to code your-favorite-algorithm ™ in a recursive manner?

Let’s start with the basics, Wikipedia has a good definition available:

Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition.

Furthermore, in our first Computer Science semester we where taught the logical equivalence of coding an algorithm in an applicative or imperative way. So what’s the matter then? As usual, problems arise when reality hits (Computer Science) theory hard. Generally spoken, those two methods of encoding an algorithm differ physically. Ok, what’s meant by that? Well, depending on the used language (more to that soon), it is wise to use the former or the latter way. Let’s start out with good old procedural languages like C, C++, Java and all the others that actually matter. Procedural languages use functions to encapsulate sub-problems. For example, when a C++ program is transformed into machine-specific assembly, the compiler enforces certain conventions that ensure that these functions can be called in an uniform way. Executing a function usually means to jump to a different portion of code, do something and to jump back to where you’ve been before to use the computed result. So we have to store that return address. The function concept becomes more powerful if you can send parameters to them, so we have to store those too. Functions tend to use local variables for saving intermediate result and every function call needs a local copy of those variables, so these have to be stored somewhere too. The convention on how to store all this is usually referred to as a stack frame. As the name implies, a certain frame is stored on the stack, a certain memory area that was set up by your kind operating system for your proggy’s process in advance. Let’s shamelessly steal a picture from Wikipedia to visualize this concept:

Stack frame example

Note that the actual frame layout differs with the language that you use. For example Pascal-derived languages use a different layout than the C world. It easy to see that this unavoidable overhead gets bigger (relative, not absolute) the shorter your functions are. Recursive algorithms generally tend to use short functions, which is shown exemplarily with this C++ implementation of the Fibonacci numbers:


int fib(unsigned int n)
{
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fib(n - 1) + fib(n -2);
}
}

This means big relative overhead. Let’s look at the imperative (and optimised) version of the same algorithm:


int fib(unsigned int n)
{
static unsigned int tmp[] = {0, 1};
static std::vector fibs(tmp, tmp + sizeof(tmp) / sizeof(unsigned int));

if (n >= fibs.size()) {
for (unsigned int i = fibs.size() - 1; i < n; i++) {
fibs.push_back(fibs[i] + fibs[i - 1]);
}
}
return fibs[n];
}

One function means one stack frame, easy peasy. So to say, the overall runtime and memory cost of those stack frames increases the more excessive you execute short functions from within functions (hey, that’s recursion!). The imperative version only uses one stack frame, which saves us countless bytes on the stack (speak, main memory) and reduces the risk of stack overflows. Furthermore it saves CPU cycles for those push/pop and store/fetch instructions you don’t have to execute (The inline keyword in C/C++ comes to mind here). Therefore it is always faster to go imperative than recursive in the real world. This concludes to:

NEVER EVER USE RECURSION IN A PROCEDURAL LANGUAGE !

However, the situation looks slightly different if you use a functional language (or even handcraft the Fibonacci algorithm in assembly, but that’s another story). Functional languages often use the tail recursion idiom to allow (or force, depending on personal opinion) the user to encode algorithms recursively. What happens when that get’s translated into assembly? That idiom is simply mappend back to simple loops for the exact same reason (remember, they’re equivalent logically). This post already got longer than I expected, so let’s keep it short. Functional languages allow convenience recursion idioms that get translated back to imperative idioms to run performant on real hardware. Maybe someone more experienced in Haskell can provide more detail how this is done exactly (assembly listings appreciated).

If you want to confirm the above, take the attached little test programs and run them through your favorite profiler and compare jump/call counts and stack changes. You might want to check what time one iteration of the algorithm takes (speak one loop iteration versus one recursive function call). That’s it!

GSoC updates: the client agent

•August 4, 2009 • 16 Comments

This follow-up post is about my proceedings for my GSoC project to integrate SyncML support in Akonadi. This consists basically of two parts, a server agent and a client agent. The former allows other devices to talk to your Akonadi server while the latter let’s your Akonadi server speak with others. Quite simple? Not really, SyncML is a complex and error-prone beast. That said, the server component might take some time to be ready, but the client agent is actually useful now. So let’s have a little test session!

The following stuff is pretty low-level, requires some compiler/shell knowledge, a KDE development environment set up and is not intended for end-users. Still there?

Compilation
If you have your KDE development environment up and running, just checkout trunk/playground/pim/syncml from KDE SVN. You need to have libfunambol installed, which you can either compile for yourself from source our use a package from that repository (a patched libsyncml is available too, if you want to build the server agent). Not much to say here, just build and install it.

Setup
If you’ve reached this step you now should be able to add a SyncML client agent in the Akonadi resource configuration control module (system settings -> advanced tab -> akonadi configuration):

syncmlclient-agent-3

If you did so, you should now be presented with the following configuration dialog:

syncmlclient-agent-4

There you can setup the remote server (speak web-service) you want to sync with. Your job is basically to decide what parts of your PIM-data you want to be synced (contacts, calendar, etc.) and how to match the corresponding local Akonadi resource with the remote database on the server. Some more advanced values will be exposed soon, SyncML has gazillions of options available! Here are some possible values to toy with:

myFunambol.com

URL: http://my.funambol.com/sync
username/password: you should register!
contacts remote db: card
events remote db: event
tasks remote db: task
notes remote db: note

ScheduleWorld.com

URL: http://sync.scheduleworld.com/funambol/ds
username/password: you should register!
contacts remote db: card
events remote db: cal
tasks remote db: task
notes remote db: note

Mobical.net

URL: http://www.mobical.net/sync/server
username/password: you should register!
contacts remote db: con
events remote db: cal
tasks remote db: task
notes remote db: vnote

Rumors have it that some Google services are also SyncML-capable but I don’t know the settings needed. Feel free to post a comment if you know something here. As a sidenote, you can actually have multiple agents for several remote sides in parallel.

Showtime!
For now all that stuff lacks a GUI, there’s some initial work on a KControl module but for now we have to communicate via D-Bus with the client agent. The same is true for visual feedback. The agent is able to tell you whether the sync succeeded or not. If you want to know more, you have to watch Akonadi’s debug output for now. Fire up a terminal and enter the following stuff:

$ akonadictl restart
$ qdbus org.freedesktop.Akonadi.Agent.akonadi_syncmlclient_agent_0 / sync

Before you actually try this, be warned! This is unstable code that is still in heavy development and all kind of weird things may happen to your PIM data, so make a backup before you proceed.

syncmlclient-agent

Opinions on Microsoft’s GPL’ed code

•July 23, 2009 • Leave a Comment

There’s been much debate about Microsoft releasing code under the GPL lately. Linus Torvalds was intervied by the Linux Magazine, here’s what he thinks about it. Also interesting is Greg Kroah-Hartman’s related blog entry. According to him Microsoft simply had to comply to the GPL because they used Linux code (to avoid a lawsuit) and they want to have it included in the kernel. A future model? We’ll see..

Palm Pre SDK

•July 16, 2009 • Leave a Comment

Finally, Palm opened up developer.palm.com to the public. Read more in this announcement. Apple also recently released a “fix” to shut out the Pre from iTunes, this GCDS talk talks about open alternatives. Hopefully the GSM version of the Pre gets released real soon :-)

Update:
Another article in the same vein just popped up.

Origins of the moonwalk

•July 3, 2009 • 1 Comment

GSoC status update for weeks 3-5

•June 30, 2009 • 10 Comments

Ok, this one took a bit longer than expected. But better late than never..

In the last 3 weeks I’ve been concentrating on the SyncML server agent. Here is how it currently looks:

syncml server agent 1

As you can see, I streamlined it quite a bit but there’s still a lot of options available. This is necessary to cover all kinds of weird devices (and what they think what is standard SyncML behavior). Who ever told you that SyncML was simple is wrong :-)

syncml server agent 2

One can already interact with the server, it should respond to simple queries but does no actual syncing with your Akonadi resources yet. More specific this involves to map Akonadi’s concept of resources and collections to libsyncml’s datastore concept. There’s usually one libsyncml datastore per content type available (one for notes, events, task, etc.) but Akonadi can have more than one resource for such things (For example my Akonadi instance has 3 calendar resources right now). This part turned out to be quite tricky, but I’m getting there.

A nice side-effect is that libsyncml now has one more project outside of the OpenSync project. We’re in good contact, I even got SVN access to speed up patching ;-) The bigger news is probably that libsyncml now has a stable branch.

That’s it for now, I’ll try to provide more frequent updates …

The mainframe

•June 26, 2009 • Leave a Comment

While my university semester’s end is not to far away now, I had a lot of fun with a course called “Mainframe Computing” held the first time at our university. According to this slashdot article it’s good to gain some knowledge in that old wizardry. Seems like the Mainframe is on the rise again.

Shuttleworth interview on golem.de

•June 22, 2009 • Leave a Comment

Altough the folks at golem.de are known for anything but quality journalism, they have an interesting video interview with Marc Shuttleworth (ye olde Ubuntu founder). He talks about the future of the free desktop, current issues and the competition. Altough nothing new on the content side, it’s still nice to watch.

Stop motion

•June 13, 2009 • 1 Comment

Found a nice stop-motion video on youtube entirely done with post-it’s:

And the corresponding making-of:

The Palm Pre

•June 10, 2009 • Leave a Comment

Here’s some useful user info about the recently landed Palm Pre, which I currently tend to buy when it arrives in europe. Mathew Garret also has a lenghty post about details from the recently leaked root image. Ah, not to forget, this is how to get into developer mode. Actually I’m prefering this gadget over the available Android-based phones currently …

Update:
Be sure to read also engadets everything you ever wanted to know about palm pre article.