Archive for November, 2005
Powered by Sorcery
Federico and I had one of our long, meandering discussions today, touching on a range of topics. One of the things we’ve been talking about recently is how to get beginners interested in the fundamentals of computing, including how said fundamentals should be taught, and how to enable exploration. We agreed that since the 80s, computers have become easier to use (for mundane tasks), but in many instances also harder to program.
For instance, one of the exciting things we could do when we were kids was make the computer draw pixels on the screen. When you learn how to do this, the possibilities for further exploration are endless (no I’m not coming out to play, I’m pushing pixels!). Maybe you’d show off your first wicked graphics hack (random pixel spray #5) to your dad, and he’d go “oooh,” and reward you with a floppy disk on which to save it. After a while, and with some help, you’d be exploring curves, fractals, automata or perhaps a Snake clone of some sort. Eventually you’d grow up and be excited about serious shit.
So how would you go about doing that today, on a PC running GNU/Linux? The answer is, you need to learn an API. If you’re lucky, someone will set you up with a sandbox, or you’ll run into SVGALib and get it to work on your graphics adapter. If you’re unlucky, you’ll try to do it with a GUI toolkit - and fail. Case in point, GtkDrawingArea takes some figuring out. You must be a very motivated beginner if you’re ever going to see your pixel using that.
Now, for fun-loving hackers who somehow do manage to produce pixels and such, I have a book recommendation; it’s called The Magic Machine, and is a gold mine of fun hacks, based on a column in Scientific American from back when it didn’t suck quite as much. Ignore the title and cheesy cover - the book introduces topics like fractals, artificial life and text processing (including markov chaining) in a way that makes for easy implementation, but without being tied to any specific architecture, technology or knowledge base. I lost my copy somewhere between Fredrikstad, Norway and Xalapa, Mexico, but I remember it as one of the books that got me excited about programming. Technology books like XML in a Nutshell don’t do that.
Ready, on, boil
Got a new boiler installed today. This one is automatic, so we now have hot water on demand - which means I can stumble out of bed and directly into the shower, instead of getting dressed, stumbling outside, fiddling with flammable gas supply, stumbling back inside, staring at my desk for 20 minutes, undressing, showering and remembering to turn the boiler back off. Quite an improvement.
Telmex has been dicking around with my precious Internet all day. After weeks of different technicians demanding I cycle the modem’s power, replace my filters, replace most of my phone cabling (which I, out of sheer desperation, let them do), &c, they’ve been slowly arriving at the conclusion I suggested to begin with: Something’s wrong at the central.
I’ve been patient, letting them do their thing by the book, remembering that if 1) you fail to listen to an engineer and 2) something bad happens, you may be subject to 3) additional screwage, even if 1) and 2) are unrelated.
The central may apparently take another couple of days to fix. To their credit, though, this hasn’t cost me a thing apart from my copious spare time.
Nipples
Last night I dreamt about a man who dived down to the bottom of a lagoon to collect the nipples, resting at the bottom, that people had lost swimming. The man may or may not have been me. Slowly and carefully, he collected the nipples in a jar which was already half full, smiling as he did so.
I don’t think dreams are significant in and of themselves (they’re more like madlibs that sometimes reveal something about the subject), and that’s why it’s fun to assign meaning to them. Maybe this one is about recovering from alienation, or ideas for an intuitive user interface.
Maybe I’m just obsessed with nipples (after all, they’re pretty neat!), or maybe I’m overlooking something obvious, like the psychology student who relates his dream of “firing a gun with a glove over one end”, wondering what it all means.
Derivative work
It’s funny when the US administration accuses someone of trying to rewrite history.
Lord of Construction
I finally seem to be pulling out of a trying couple of months. It started when, having bought an “almost finished” little house out in the bush, Maru and I hired an architect to take care of the remaining work. This work included constructing stairs to the second floor (previously only reachable by a shaky ladder arrangement), tiling the bare concrete floors and applying some paint. The architect was recommended by a friend, and he seemed like a knowledgeable, friendly kind of guy. At first, I wanted to see if we could contact an agency to do it with known skilled, insured workers, all proper like, but that’s apparently not the way it’s done here. Anyway, people who actually understood what I meant by that advised me that this does in fact not exist.
So we went with Architect, who claimed it would take two weeks. I assumed this meant four weeks, and planned for that.
In the end, it’s taken ten weeks, six of which we’ve spent in an unfinished house filled with construction noise, dirt and local fauna. Suffice to say it’s been fairly unpleasant. However, we now have one (1) finished, working house with a roof and reasonably modern facilities to show for it. All that remains is to unpack (!) and have Telmex get the lead out and actually fix my DSL connection, which drops every ten minutes or so during daytime (I suspect the central).
In the midst of this, something occurred to me: Although my experience is insignificant in the bigger picture, construction projects of all sizes and in all parts of the world are late and/or over budget. We’ve been pouring concrete for centuries (or millennia, depending on your definition of concrete), and we still can’t do it to specifications. Yet, when software engineering goes wrong, it’s because it’s an “immature” field. As if software is easy, and we’re doing it wrong. Maybe it’s just hard, like plain old construction work?
At least your industry sucks too. I’ve been sleeping better lately.
Or is it?
Optimizing really tiny things is hard, and maybe my initial feeling was correct in that this cannot be optimized. However, I ran Behdad’s test on my computer, and it came out as above. This is the exact same test on the dataset that federico provided (however, I profiled only the original and Behdad’s code, as that should be sufficient to demonstrate any differences between his test setup and mine). It still shows a significant gain, especially for latin-1 languages.
I compiled the test with -g -O2, as is necessary to see the benefits of optimized C code. Behdad: I don’t know which flags you used. If we’re using the same compiler flags, the difference must be down to the fact that I’m testing on an Athlon and you’re on a P4. That would be pretty amusing.
Oh, and a profile of a GtkTextIter test case that I got from Larry the other day had g_utf8_offset_to_pointer() show up as using 50% of the CPU time. I don’t know if it’s a realistic case, though. At least we’re learning about optimization in the context of distributed efforts. Or something.
Mouthfuls of UTF-8

Federico’s mention of g_utf8_offset_to_pointer() inspired me to take a look at this function; after all, it’s pretty low-level and bound to show up on profiles for a lot of widgets.
My initial reaction to the tiny bit of code there was that this cannot be optimized significantly. However, once I realized that loading and checking a single byte at a time is pretty wasteful when we know the minimum number of bytes we’re going to process, things started to get interesting.
It turns out that on a 32-bit arch, we can load 4 bytes at a time into a register, and then use that to skip several letters ahead per iteration for some common cases. These cases are: 4 ASCII letters, 2 2-byte letters and 2 3-byte letters (we only need the first byte of the sequence to know if it’s 3-byte, so we can fit two of these into our 4-byte register).
Initially I was a bit worried that unaligned accesses would slow us down, but it appears that at least on IA32 the penalty is much lower that what we’d get trying to ensure alignment (my initial try, which is not profiled here, did that). I hope people with access to other platforms can test those and fix pathological cases.
Thanks to Federico’s awesome plotting utility, I can now present to you the performance history of various approaches I tried to arrive at something that should be very close to the best we can do. There’s also a plot of the final gain, which is easier to read.
It appears I can’t attach text files to Wordpress posts, so I’ll just link to the patch. It’s a little half-baked currently - it needs to be made endianess-agnostic (currently it assumes little-endian).
OMG blog!!!
So I’m thinking, maybe I’ll bother to write if I have an automated blogz0ring system. I couldn’t really get into the habit with my last one, nor with the one before that. Not that those were blogs. My, do I hate that word.
Anyway, a lot of stuff has happened since then. We’ve moved to the Mexican outback, for one. Hans Petter, homeowner. It’s just weird.
I’m not the only one going through changes, though. Joakim, for instance, seems to be starting a new career. I can’t wait to see what comes out of that.
