Offline Compression with Nginx

Published

Tags: , and

There’s a clear tradeoff with compressing HTTP responses on the fly: compress “harder” and you’ll (hopefully) get a smaller file that takes less time to send over the network – but the net benefit might be negative if the extra work takes too much time, or (when under heavy load) too much CPU. A lot of work has been done analysing this tradeoff, but for static content there’s a neat and simple way to avoid the tradeoff completely: compress offline before serving. Nginx supports this using the gzip_static module.

The Enterprise Pushbutton

Published

Tags: , and

Let’s talk about a hardware driver for a pushbutton. A pushbutton driver isn’t as completely trivial as it might sound because you need debouncing logic to ensure a crisp on/off signal, but it’s hard to imagine how it might need more than about 100 lines of C code.

After working on this particular embedded system, I didn’t need to stress my imagination any more. This pushbutton driver was modelled as an explicit finite state machine, and all the possible states and transitions were specified in a spreadsheet. Then there was a python script that processed this spreadsheet and generated state table data as C code. This was linked to an FSM evaluator in C. The FSM was controlled by a bare-metal driver and triggered callbacks on each state transition.

Most of the callbacks were marked “not yet implemented”. In fact, only two states were even reachable: BUTTON_UP and BUTTON_DOWN. Eventually the entire project was canned, but not because of missing support for BUTTON_TIMEOUT or any of the other states.

Oh, yeah, the FSM didn’t do any debouncing, so the low-level driver had to do that before passing button up/down events to the FSM.

Turning any Live CD ISO into a Live USB with Writable Storage Partitions

Published

Tags:

Here’s a guide to installing a bootable CD-ROM ISO image as a bootable partition on a (USB) drive, while using other partitions as normal filesystems. The most complicated part is partitioning and formatting the drive, which is well documented elsewhere, but I didn’t see anyone else explaining exactly how to do this trick.

Code Jam 1A :/

Published

Tags: and

Well, I started off round 1A feeling good. By the 6min mark I had solutions for the small and large inputs for problem A submitted, then… everything fell apart. It was my first time in a long while writing code under live competition conditions, and I’d forgotten just how tough it can be. Time for more practice.

Ignoring my total failure to get working solutions across the finish line, the next two problems weren’t actually hard – they could fit in as medium problems in a qualification round. In fact, so many people solved them that getting a perfect score wasn’t even enough to get to Round 2.

By the way, my Python solution for problem A brings me to 9 languages in this Code Jam. Will I keep going? That depends on how jet-lagged I am for rounds 1B and 1C.

Code Jam 2016 Qualification Round

Published

Tags: , , , , , , , , and

This year’s qualification problems were all straightforward puzzles. None of them needed sophisticated implementation techniques, or advanced algorithms, or complex data structures. They just needed patient analysis and careful coding. That’s nice from one perspective, but unfortunately it’s hard to make a good comparison of programming languages when every problem can be solved with just loops and arrays. Still, I succeeded in my goal of producing every output with a different (sometimes terrible) language.

Here’s my code and commentary on the problems. Warning: spoilers ahead. If you haven’t looked at the problems yet, go do that first.

How Dirtying Pure Functions a Little Can Be Useful

Published , Updated

Tags: , , and

Functional purity is a valuable concept for writing maintainable code, though outside of functional programming languages like Haskell, it’s often treated like a nice-but-expensive luxury. But it turns out that pure functions that aren’t quite so pure can be cheap while still having concrete benefits for code in non-functional languages like C++, Java and Python. For D code, this is supported by the language itself, but there’s nothing D-specific about the overall idea.

Registered for Code Jam 2016

Published

Tags:

I’ve just registered for Code Jam, Google’s annual programming contest. Unfortunately, the schedule means I might have to do a round or two while on the road, but I’ll give it a shot.

I’m a firm believer that programming languages are tools, and using the right tool for the job matters, so I’ve always used multiple languages for Code Jam. For a bit of extra fun this year, I think I might try for a personal record for language count.

In particular, this is my first Code Jam in a few years, so it’ll be the first one where I’ll use D. D’s standard library is weak on containers (at least for now), but otherwise it’s a really good language for problems like this.

Omoide Kaeru

Published

Tags:

Today is the fifth anniversary of the 2011 Japanese Tohoku earthquake disaster. When disasters happen, news sources tend to focus on dramatic statistics like the number of deaths or the number of homes destroyed, and not so much on the very long process of recovery. In the 21st century, though, it’s much easier to find videos on the internet produced by the disaster victims themselves. Unfortunately, as in the Japanese disaster, language barriers can stop the messages reaching far beyond the disaster area.

So I thought I’d translate one of the videos. It’s about Omoide Kaeru, a non-profit organisation that’s been restoring and returning items found washed up after the tsunami hit Sendai. The video mostly talks about photos, but the group has also been handling personal documents like certificates.

By the way, the original video is from 2013, but Omoide Kaeru is still working in 2016.

Why Defensive Coding Matters - A War Story

Published

Tags: , , and

Story time.

What Difference Can Order Make When Hashing?

Published

Tags: and

I saw this thread about password hashing on the D language forums. The original post had a good question that didn’t get answered at the time: if you’re hashing a bunch of things, can it make any difference (for security) what order you do it?

The answer turns out to be yes, and it’s a neat example of the difference between theoretical ideals and real-world systems. Because I think this stuff is worth knowing if you’re using cryptographic hash functions for, you know, actual crypto, I thought I’d write up a blog post about why it can matter.