The Trampoline

Most programmers readily make use of recursion – it is indeed a very elegant solution for several problems, and results in much cleaner code. However, recursion brings along with it a drawback, the stack is filled up very quickly. Usually, this isn’t really much of a problem, since most desktops these days have decent memory and most language runtimes grow the stack automatically for you.

On embedded systems however, it’s a different story. Limited availability of resources forces you to write efficient code which is usually not very elegant. This is also true for recursions that loop a large number of times on normal machines – imagine finding the fibonacci series for the largest 64 bit number.

I recently had to write a cryptographic routine that was tree-recursive. I had 5 functions, let’s call them F, A, B, C and D. F was the entry point for the routine, and F calls one of A, B, C or D depending on the parameters passed to it. A, B, C and D, in turn call F again with a different set of arguments. This goes on until F detects a terminating condition, upon which it returns a value. A typical run would result in around 400 nested calls, which worked fine on my desktop, but not on the target platform (an embedded system).

The first step I took was to roll all A, B, C and D into F itself. This meant F became larger and more ugly (no more modularity!) but it reduced the nesting level by half. Now I had a single recursive function (which was not tail-recursive, obviously). This, unfortunately, wasn’t enough.

Then, I leant about a programming device called the trampoline. It’s a great way of attaining recursive properties while saving stack space. A trampoline is kind of marshal function that repeatedly calls your recursive function until a terminating condition is reached. The recursive function doesn’t recurse anymore, but instead just returns a true/false value that determines if there’s more work to be done or not:

boolean F(/* notice we don't have arguments */) {
arg1 = this.arg1; /* we load them from the class/global variables instead */
...
if (more_work) {
this.arg1 = something; /* save arguments for next call */
return false;
} else {
this.finalVal = finalReturnValue; /* we've reached termination */
return true;
}
}

int trampoline(arg1, ...) {
this.arg1 = arg1 /* initial argument setting */
...
while (true) {
if (F())
break;
}
return this.finalVal;
}

This way, we never reach a nesting level of more than 2, saving stack space by drastic amounts, with only a minimal addition to the heap load. The code doesn’t look that bad either. Definitely one of those ‘Aha!’ moments for me :)

Posted by Anant on December 30th, 2007 in FOSS, Gentoo, Hacks, Programming | 5 Comments

Back from Goa

My Goa trip was simply fantabulous. Apart from the fact that Goa is a great place for a vacation, I was accompanied by 7 of my college friends which made the trip one that I will cherish for a long time to come.We left Bangalore by bus on the 15th. The journey was pleasant and the view next morning was absolutely stunning:

The bus dropped us off at the Panaji bus terminus, and we took a shuttle from there to Vasco – where Ameya (our host) lived. After a nice lunch and a nap, we took off to the beach closest to base camp – Bogmalo. The beach was a quiet and clean with relatively few people around, which made it possible for us to play a game of beach football. We returned home after jumping around in a sea for a while.

We hired a couple of Activa’s the next day (this seems to be the norm for transportation in Goa) and reached Old Goa in an hour or so. We visited the really old church, which was really impressive – it also contained the remains of St. Francis Xavier. The archaeological museum next door was fun too, very informative about the history of Goa. We proceeded to the capital city of Panaji next, and after booking tickets for a river cruise aboard the Princess de Goa for the night, had lunch at the QuarterDeck.

After lunch, we visited Donapaula, a popular Jetty, which was unfortunately under renovation or something. The view was great though, and we enjoyed a nice little ride on the water scooter. After hanging out in Cafe Coffee Day for a while (These places are *everywhere*, I think they’re trying to become the Indian Starbucks :)) we reached the Panaji river coast to board the Princess de Goa. These river cruises seem to be a popular attraction, they basically consist of a few dance shows, a dance floor and an amazing view. We got to see the (in?)famous floating casino on the way too.

My Summer of Code mentor, Matt Lawless, happened to be in Goa too, so we scheduled lunch for the next day. We met at the Calangute post office (which was somewhat close to Matt’s home) and proceeded to the Calangute beach after lunch. My friends, meanwhile, reached the Baga beach, which was just next door to the Calangute beach (the two most famous beaches in Goa). We splashed around in the water for a while, joined by Matt, and then a second lunch :)

Matt decided to leave, and we went on to try some of the water sports at the beach. We went for a banana ride, a water scooter trip, but the one that took the cake was the parasailing. Nothing like a gentle ride in the sky to rejuvenate you. Flying over water with the beach behind you and the sunset in front is an experience I can’t put in words :)

The third and last day began with a long ride to south Goa, where we first visited the Benauli beach. This beach was beautiful, the sand was different than the others, and the most fun part was when I was buried by the others:

The rest of the day was spent at GoaKart, which was apparently a national Karting track. Parasailing was great, but karting was really the most exhilarating, especially because we raced and went for 4 rounds :D

Flickr didn’t let me upload more than 100MB of photos at once, so I moved to Picasa Web Albums instead. I wrote a small script: backr.py that uses James Clarke’s flickr.py to back up all my photos and uploaded them to Picasa Web, which allows me to create as many albums as I want (unlike Flickr). UPDATE: I finally got myself a Flickr Pro account, so pictures are back there again. This time I just used iPhoto to migrate :)

So, I guess that’s a few more items off my “list of things to do before I die”!

Posted by Anant on December 23rd, 2007 in College, Fun, Google, Hacks, Life, People, Photos, Places, Plan9, Python, SoC | 2 Comments

Leopard: A First Look

I couldn’t help but notice that close to 70% of developers who attended foss.in had Macs with them. And half of them had even upgraded to Leopard, giving me a few glimpses of what Apple’s new operating system looked like.I decided to upgrade to Leopard too, and got myself a copy from the iStore on M.G. Road. It cost me Rs. 6,200 which is (suprisingly?) a little more than the dollar-converted rate. Anyway, I decided to do a clean install after backing up all my data – it was the perfect chance to remove all the cruft in the system. The installation went smoothly and the traditional multi-lingual ‘Welcome’ was simply stunning. Now I began installing the applications one by one:

  • QuickSilver: The first app that anyone would need. It worked with all the standard plug-ins on Leopard without any glitches. I did notice, however, that it wouldn’t launch any applications that you just installed (specifically, those that you downloaded; yes, Leopard keeps track of that!). You have to manually launch the application once (you get a dialog asking you whether you really want to) before it appears in QuickSilver’s catalog. Mildly irritating, but it’s a one-time thing for every app.
  • Macports: I compiled this one, also worked out-of-the-box without any problems. All the ports I used so far work fine, although I will expect some of them to fail (which isn’t a macports, but rather an upstream problem).
  • Adium/Colloquy: Clean install, no trouble. Imported old logs just fine.
  • iScrobbler: Version 1.5.2 wouldn’t post to last.fm for some reason, version 2.0beta works great (and has a bunch of new features).
  • VMWare Fusion: Works great, but it looks like you’d need some small tweaks to get the tools for Linux to work fine. I’m going to give Fusion a try for sometime to see how it measures up to Parallels (which is what I was using earlier).
  • Poisoned: This one doesn’t work on Leopard. It starts up fine, and gIFT appears to start but none of the networks connect. After searching the forums for a bit, I settled for FrostWire which works fine.

Let’s take a look at all the new features that caught my eye:

  • Tabbed Terminal.app! There’s no need to use iTerm now. I did, however, have to spend a couple of hours in trying to get the key combinations to work in a sane manner. This post proved to be very helpful, DoubleCommand didn’t.
  • Uniform interface. Finally. I was getting sick of the varied brushed-metal/grey/what-not styled interfaces. Leopard finally has a smooth grey across all applications. No need for UNO anymore.
  • Spaces. Eliminates the need for VirtueDesktops (Notice the pattern here?). Really nifty if you run a Fusion virtual machine in full-screen on every space and assign sane keyboard shortcuts to switch.
  • Stacks: Snazzy looking way of representing Downloads and Documents. You can add your own folders to ’stack’. I like the new Downloads directory, I used to create one in Tiger anyway :)
  • dtrace. The all-encompassing debugger from OpenSolaris made easy. I haven’t used it for anything useful yet but I like what I see.
  • Mail 3.0. Slicker interface, and I really love the Mail Activity area which tells me exactly what Mail is upto – Mail 2.0 always left you wondering!

I haven’t had a chance to look at Quick Look yet, but that should be another thing to look forward to. All-in-all a good experience so far, but was it worth the 6 grand? I’ll wait and see.

Posted by Anant on December 10th, 2007 in Apple, Mac, Programming | No Comments

FOSS.IN: Day 3

Day 3: The first day of the main conference. We thought we were running late (left home only at 09:50 after getting our Gentoo T-Shirts on) but the inauguration ceremony started half-hour late (as usual!) so we were able to catch the whole action. After FOSS.IN/2007 was kicked off by Atul & Kishore, Naba Kumar came up to give the keynote on Anjuta DevStudio. I didn’t know the origin of the name Anjuta earlier, but it was certainly fascinating :)

I had my talk on contributing to Gentoo right after the keynote, and we started at 11:30 on the dot (the schedules in other rooms were on-time). Gora gave an excellent introduction, and I began speaking to a somewhat-filled room about the different entry points to Gentoo development. The audience were really interactive and the questions were brilliant – this is something that I really liked about this years edition of FOSS.IN. There was a lot more interest in Gentoo than I had originally anticipated and it was nice to see our stall really crowded immediately after the talk. Hopefully, we’ve brain-washed atleast two-dozen people into using Gentoo :). The remainder of the day was spent talking to people who approached our stall – it got a bit monotonous though, answering the same question “Why is Gentoo different?” over and over again. We’ve decided to print out an FAQ poster and put it up to make things a little more easier for us ;)

I had my third talk on Plan 9 from Bell Labs scheduled in the evening, right beside some really interesting stuff including the talk on PulseAudio and the lightning talk session. Again, I really didn’t expect much of a crowd for my talk, but I was happily mistaken. The room was not only full, but there were also people seated on the stairs and near the door! The talk went off really well, and I think it was *the* best talk I’ve delivered so far. The crowd was really smart and it was fun to interact with such an audience.We’ve planned to have a small Mozilla hack-a-thon today, let’s see how that goes. Besides that I’ve planned to attend a few other interesting talks. Looking forward to keeping the pace up, I’ll catch you all tomorrow!

Posted by Anant on December 7th, 2007 in Conferences, FOSS, FOSS.IN, Gentoo, Mozilla, People, Plan9 | No Comments

FOSS.IN: Day 2

We reached a little late for Day 2, because there were no talks in particular that we had wanted to attend. After reaching the venue at around 11:00, the first thing we did was to distribute the Gentoo t-shirts so folks could wear them today (the t-shirt needs one wash before wear). Shyam (fox2mike) had brought the Gentoo banner so we set that up in the stall. G0SUB and myself then attended pradeepto’s talk on setting up a KDE development environment. This was followed by an amazing demonstration of dtrace by the one and only GMan (Glynn Foster from Sun/GNOME). dtrace is really powerful, although I keep hearing about it, yesterday was the first time I actually understood how useful it is. After lunch, I attended Debarshi’s talk on Opyum, his summer of code project for this year. Also got to meet a bunch of other SoCers and we’ve all planned a SoC BoF along with a few mentors who are also present at the event.

Then we got busy distributing invites for the Mozilla party, and hung out with the Mozilla gang until it was time to leave. The party was at Opus which was a nice place with good (loud) music :). The karaoke was a big hit. After meeting a lot of people and having some good discussion, I decided to call it a day (I had two talks to prepare for!).Day 3 begins in a few hours – both my talks are today and we’re going to kick off the Gentoo stall, so I’m really excited. See you tomorrow with another update!

Posted by Anant on December 6th, 2007 in Conferences, FOSS, FOSS.IN, Gentoo, Mozilla, People | No Comments

FOSS.IN: Day 1

Quick update on the first day at FOSS.IN. We reached the venue at around 09:00 – the stalls were the first thing that caught my eye (especially the Sun & Google ones). After about 20 minutes of frantic organizers moving all over the venue at lightning speeds, all the speakers got registered and we moved to SDA/250 for the Mozilla PD.We started a little late – around 10:30 as opposed to 10:00. After brief introductions by Mary, Myk kicked off the project day with an excellent overview of the add-on scenario in Mozilla. This was followed by Prasad’s talk on building applications on the Mozilla platform. The calculator example – complete with it’s own add-on manager (for adding scientific support) – was a great way of giving the basics of Mozilla application development as was the highlight of the tutorial.

I gave the next talk on writing add-ons with JavaScript using XPConnect. Prasad and Myk had already covered a lot of ground on the basics of add-on and application development, so I was able to wrap up my talk in about half an hour – bringing us right back on schedule ;). The last talk of the first half was given by Mary which focussed on the various non-technical ways in which you could help Mozilla. The talk brought to light a lot of cool activities Mozilla was involved in. We broke for lunch at exactly 13:00, promising to meet back at 14:00 for the second half. Mary also kept a lot of Mozilla swag at the entrance of the hall – which included badges, mobile holders, tattoos, stickers and wrist bands. The crowd was ecstatic about them and needlessly to say that they were a great hit.

At lunch we caught up with a lot of other FOSS friends from #linux-india. Aanjhan transferred the Gentoo stickers which he kindly volunteered to print, and we hope to setup the Gentoo stall today so that we’re ready for tomorrow. I finally met G0SUB in real life, took me some time to recognize him because of the shaven beard though :). Post lunch we began with Krishnakant’s talk on accessibility in Mozilla, which as Gora said was an eye-opener is many ways for all of us. I was really impressed with the level of accessibility that the Gnome environment and Mozilla Firefox provided to the disabled. We discussed some ways in which accessibility could improve in Mozilla applications.The next talk was by Axel, which was about Localization in Mozilla. The coolest part of the talk was when Axel fixed a bug on localization (though it was ultimately closed as a WONTFIX!) because it gave a very good overview to the audience about the life-cycle of a bug. The final talk of the project day was by Chris Hoffman, which was about QA in Mozilla and how you can contribute to these areas which require some technical skills – “for people in the middle”.

We rounded off the project day with about an hour of one-on-one discussion with all the Mozilla folks, which was, in my opinion, the best part of the project day because we got to discuss a variety of topics (not only related to Mozilla or Technology even). We also decided to have a hack session for Mozilla, which would be tentatively on the 7th at the hack center.

After all the dust settled, we packed up around 18:30 and a group of around 12 went for dinner to “Sunny’s”, a nice Italian restaurant. Discussion on virtually everything ranging from food to movies and dtrace to macports ensued and we were done by around 21:00. After reaching home I just fell on my bed and now here I am, all geared up for FOSS.IN: Day 2! :)

Karunakar posted a few pics of Day 1 here, check them out.

Posted by Anant on December 5th, 2007 in Conferences, FOSS, FOSS.IN, Gentoo, Mozilla, People | No Comments

FOSS.IN: Day 0

FOSS.IN is here. Finally.I had been to the venue yesterday and I caught up with plenty of people, besides helping stuff the delegate kits with plenty of goodies (which I’m sure all the delegates will love). The venue looks great, the halls are awesome, and the excitement is high… I can’t wait to get started.

Later, I caught up with all the folks speaking at the Mozilla Project Day for Dinner at the Tandoor – I met Prasad, Krishnakant, Mary, Axel, Chris and Myk – all for the first time which was really nice.

Looking forward to a great day today, and I’ll keep posting events as they happen.

Posted by Anant on December 4th, 2007 in Conferences, FOSS, FOSS.IN, Gentoo, Mozilla, People | No Comments