Wifi network and computer network names
September 24, 2007
Will wants to know the scheme I use for Wifi network and computer network names. Though I’m not really keen on these memes, I’m not above admitting I’m feeling a bit flattered to have been tagged. So I’ve decided to dish the dirt:
- My current wireless network is called “Icon”. It is named after an album by Paradise Lost. (Which was my first wireless network.)
- My home machine is called Russet and my work machine Idared.
Anybody spot the link between the the computer names without googling? :)
Embedding Cocoa Frameworks
September 22, 2007
Cocoa has a really neat feature that allows you to embed a framework inside your application bundle. Why would you want to do this? Not all frameworks are available everywhere. Instead of forcing your users to install lots of frameworks before they can start using your app, simply bundle them inside your app.
You cannot simply embed any framework; it has to be built specifically for embedding. A framework is a dynamically linked library. These have the path they are installed at hardcoded inside them. (For security reasons, or some such; it’s damn inconvenient at any rate.) Normally these paths are of the form `/Library/Frameworks/`. For an embedded framework, however, we have to use something else. The special path `@executable_path` refers to the path of the executable inside your application bundle. You can then refer to the `Frameworks` directory in your application bundle by setting the installation path of your framework to be `@executable_path/../Frameworks`.
If you want to embed a framework inside another framework you’ll need the even more special `@loader_path`, which is only available in OS X 10.4 Tiger and later. I find this useful, so that’s what I used in the example above. Now add the framework to your application the way you’d add any other existing framework:

Add a “copy files” build phase and chose destination `Frameworks`. Drag the embedded framework you added into it.

Voilá!
Jonathan “Wolf” Rentzsch has a screencast that shows you in more detail how to embed frameworks. It is for a rather older version of Xcode than what is available now, but it is still good. The Cocoa developer guide to embedding a framework is also good.
Goodbye GPL, hello BSD?
September 2, 2007
Here’s a question that’s been on my mind lately:
Should I switch away from using the GPLv2 for my software?
So far I’ve been using the GPLv2, but now that it has been superseded by version 3 I’m a bit at a loss at what to use for new projects. Do I stick with using version 2? Do I read and try to understand version 3 enough that I’m comfortable using it? Or do I use something else entirely?
It’s important to me that I’m able to fully comprehend the license I put on my own software. The BSD license wins hands down in this category: it is short and written in very simple language. For comparison here’s the number of words in the BSD, GPLv2 and GPLv3 licenses:
| License | Number of words | Number of lines |
|---|---|---|
| BSD | 223 | 26 |
| GPL2 | 2968 | 340 |
| GPL3 | 5644 | 674 |
A strong argument in favour of the BSD license there, but the length of each license is not the full story. Lowering the barrier of entry is also important. You can use BSD-licensed code in any closed-source applications—be it a commercial application or binary-only freeware. (Maybe you’re too embarrassed of the code to release it?)
More users means increased likelyhood of receiving feedback—in the form of code, bug reports or otherwise—that benefit all users. Thus I’d rather have a larger pool of “free” and “non-free” users than a smaller pool of “free” users of my software, even if the “non-free” ones use it in a commercial application and don’t directly contribute code back.
At this point I’ve pretty much made up my mind. Unless someone hits me over the head and points out a glaring flaw in my arguments upcoming releases of my software (at least the libraries) will be released under the revised BSD license.
Best..retort..evar!
June 14, 2007
> > As the great Alexi Sayle said: “Pedantic, I ?”
> Um, that would be _Alexei_ Sayle.
From a mailing list.
To be honest, this exchange is so good that you’d almost think it has been choreographed. It reminds me of the following quote from irc over at bash.org.
Shell hacks #1 - renaming lots of files
June 11, 2007
I was astounded to find out a couple of days ago that one of my colleagues was not aware of shell parameter expansion. He’d been confronted with the following problem:
Given a directory full of files, write a shell one-liner to rename all the ones ending in .doc such that they now end in .txt.
I almost choked on my drink as I heard suggestions involving Perl. No need to complicate things. Here’s how to solve the task using parameter expansion (both Bash and Zsh certainly have these):
for f in *.doc ; do mv $f ${f%.doc}.txt ; done
Now I know that my previously mentioned colleague won’t need the following explanation (he’s already dived into the Bash man page by now, I suspect), but I’m feeling wordy for a change. Here’s the above line with white space added for readability:
for f in *.doc ; do
mv $f ${f%.doc}.txt
done
On the first line we use a shell glob (the *) to select all the files in the current directory that ends in .doc. For each iteration of the for loop, the variable f contains the name of one of those files. The third line just says that we’re done with the looping, so all the really interesting bits are happening in the second line.
That line says: rename[1] the file named $f (in shell programming we put a $ in front of the variable name when it can be confused with a literal string) to the result of the following expression: ${f%.doc}.txt. Of that ${f%.doc} is the parameter expansion part. From the zsh man page:
${name%pattern}
${name%%pattern}If the pattern matches the end of the value of name, then substitute the value of name with the matched portion deleted; otherwise, just substitute the value of name. In the first form, the smallest matching pattern is preferred; in the second form, the largest matching pattern is preferred.
What that means is that if the string in the variable f contains .doc at the end of it (which we know that it does) strip it off. This will be the case for all the files, since our for loop only iterates over files with this ending. The dot over the i is that we simply append .txt.
There are loads of other parameter expansion tricks you can do, including the following, which should feel familiar to Perl people: ${f:gs/foo/bar/}—substitute with the content of the variable f, but with the string “foo” replaced by “bar” everywhere it occurs in f.
Enough from me. Dive into the Bash/Zsh man page already!
[1] — on Unix a rename is just a move[2] to a new name.
[2] — Unix geeks are lazy typists, hence “move” was shortened to “mv”.
Can of Worms
June 5, 2007

You know the feeling: you’re at your desk, planet-sized brain poring over some code through your beady browns. Then out of the blue somebody arrives with a can of worms, and there can be no mistake: it’s got your name written on it. It’s yours.
This happened to me at work today. It wasn’t your regular baked-bean tin either. No, sir! It was a large green plastic container. With a carrying handle. Subsidised by the council, even. And not to forget: my name on the shipping label.
Normally I’d kicked the bucket at this point, but today it was a pleasant surprise. I did not expect my new wormery until tomorrow.
It’s a wonderful thing. I think. I’ll feed it leftover food, cardboard, egg-shells, dust-rabbits, and the contents of my Dyson; in return, the little crawlies inside will produce excellent compost for my plants.
(Pictures of the actual worms in question over here.)
It’s all lost and stoof
January 30, 2007
Someone over at ibiblio.org apparently has a good sense of humour. Witness it’s all lost and stoof, which has “file not found” translated to many languages. My particular favourites were h4×0r, C, Mac User and Webmaster.
(Via reddit.)
Bad typesetting
November 28, 2006
While I’m on the subject of writing, I thought I’d sneak in a note about typesetting. I’m a closet typesetting geek; you probably have to be to care about whether to use an en-dash or a plain dash to separate ranges of numbers, for example. In case you’re wondering: use an en-dash—the difference is appreciable.
There are some beautifully typeset books (TAOCP, for instance) and some really atrocious ones. One of the worst offenders is Just For Fun; Linus Torvalds’ autobiography. Its constant changing of fonts, font weights and styles appears to be the work of a rabid monkey on acid rather than an actual human typesetter. It’s headache-inducing at best, and very nearly made me put the book down and not finishing it.
Now, what this typesetting did manage was to make me remember the book. Unfortunately the only thing I can remember is that the typesetting was excruciatingly bad. From this I infer that the content wasn’t all that great, but that’s a blog post for another time.
I don’t quite know what came over me, I just needed to get this rant off my chest. My usual happy & chirpy tone will resume shortly.
Punctuation geekery
November 28, 2006
I must confess: I’m a bit of a punctuation geek. And no, I’m not referring to how missing or erroneous apostrophes leap out at me with such force that I immediately rush to enlighten the author of the offending text. Although they do that. Sometimes, however, I manage to suppress the urge to correct and just point and laugh instead. Alas, it is of my penchant for em-dashes, en-dashes and semicolons I speak; what is more, I know how to use them all correctly.
Personally I find that use of the em-dash and semicolon can lead to more naturally flowing and easily-read prose. However, I’ve often heard the opposite—even to the extent of an author being praised for writing a book without a single semicolon in it. (It turned out this was because his typewriter didn’t have one, otherwise he would gladly use it.) What say you, astute readers? Are semicolons and em-dashes an abomination? Do they impair reading? Answers in the comments please…
Rise of the Machine
August 12, 2006
Last week Norman Nunley got me interested in the ICFP challenge 2006, despite it being over. It involved implementing a Universal Machine following a (hilarious) spec. This was needed to run a program that you were given; this program would then give you instructions on what to do from there.
I had never written a virtual machine before: I always imagined it too hard to even try. I was wrong. It took me around 7-8 hours; spread over an evening, the following morning and a couple of hours at work (sorry boss, it was too addictive—I blame Norman); to get a working machine. I did most of the work alone, but I needed Norman’s help to clarify parts of the spec and in some debugging.
I was amazed when it turned out this “huge scary thing”, which I’d imagined a VM to be, clocked in at less than 300 lines of C. It wasn’t fast though. A self-test and benchmarking program for the UM was available from the ICFP website. Norman’s UM ran this benchmark in minutes but mine, distressingly, took over 10 hours.
Last Saturday, after eliminating a few variables and assignments in the hotpath and using macroes instead, I managed to get the time for the benchmark program down to just over two hours. A whole lot better, but still way slower than others’ VMs. From basic instrumentation I knew that the benchmark program does a lot of memory allocation and deallocation, and I surmised that this the cause of my performance problems. During the course of the weekend I tried various things to speed my VM up further by caching previously allocated arrays in various ways. Whatever I tried, however, simply made it slower. It was a humbelling experience that reminded me that I should have faith in the library implementors; they’re most likely smarter than me.
The performance of my VM continued to bug me and Monday morning. My VM has an array of pointers to unsigned integer arrays, which the benchmark program required a whole bunch of. Indexes into the parent array are used to identify which leaf array it required and therefore I could not change the index of a leaf array and when a leaf is freed up I end up with a sparse parent array. My fallacy was to use a sequential scan to find the first free location in the parent array, when allocating a new leaf; as it turned out, this was really expensive. When I tried to just allocate new leaf arrays at the end of the parent array and simply allowing the parent array to be very sparse at the front, my VM suddenly ran the benchmark in just over three minutes. A considerable speedup, but it didn’t come for free. As I didn’t reuse any of the slots in the parent array the memory usage grew steeply to about 140 MB, up from around 3 MB throughout the entire run previously.
The morale is clear: Find your bottlenecks, then eliminate them; don’t try to eliminate imagined bottlenecks (I already knew this, but it doesn’t hurt to be reminded once in a while). I originally assumed that my performance issues was related to memory allocation. I was partly right, but it was not due to malloc() or free() being slow, as I had assumed. It was down to my stupid algorithm for finding somewhere to stash the newly allocated array.
Update: By making use of C99’s flexible array member and introducing an extremely simple free list I’ve now managed to get the memory usage for my VM down to 2.16 MB (as reported by top on my Powerbook) for the benchmark run, at a cost of adding about 40–50 seconds to the run time. The flexible array member feature allows me to do only one allocation instead of two when allocating an array wrapped in a struct; this can yield large memory savings if you’re allocating a lot of structs containing short arrays. The free list simply holds indices to free slots in the parent array.
Update, 2007/09/03: Here’s my Virtual machine, should you wish to take a look.