The Rise and Fall of Ziggy Stardust

Three months ago, we adopted Jedi who was promptly renamed Ziggy. Here he is, on the day we adopted him, while we were filling out the paperwork.

1597815_687460474698583_1176302324_n

 

We were told he was probably two years old1, and his slight limp was probably from his horrible maltreatment or a previous injury. He was treated incredibly badly, you see. Found chained up outside, with no hair from his shoulders back due to a rampant flea infestation, he was not, shall we say, loved. Until he came to us.

He came to us and found rather too much love in the shape of the #geekling who struggled to adjust her level of affection from Labrador to fragile Terrier mix. But still, it was love.

Over time, he started to limp a little more, to walk hunched, and to occasionally cry in his sleep. The vet suggested a slipped disc, we tried anti-inflammatory medication, and that seemed to relieve a little of the symptoms, but then it came back. His gait became much more awkward and he started shuffling, so we went to a specialist who dropped him into an MRI with a view to operating to repair the slipped disc. He instead found a mass on Ziggy’s spinal cord. A meningioma, actually, but details aren’t all that important. The specialist declined the opportunity to operate and instead referred us to a neurosurgeon.

Today, the neurosurgeon operated to remove the mass. He was in surgery for around six hours but the surgeon believes she removed it all. She expressed surprise at the size of the mass, and noted that his spinal cord is not in the best of conditions. Well, whose would be after being compressed 75%. Yet again I find myself amazed at the resiliency of the dog. With half that compression we wouldn’t be walking and there he was trying to jump up on things still2.

And now I’m sitting, waiting. Waiting to hear if his spinal cord is in good enough shape to allow him to breathe off the ventilator. Waiting to hear if he overcomes that challenge whether he’ll be walking at all. Waiting to hear if we still have a Ziggy.

Update (2015.05.29): Ziggy is off the ventilator and breathing on his own. Tomorrow we’ll find out if he’s able to walk.

Update (2016.01.06): Six months later, and not only is he walking and running, but he is more affectionate than he was before. I can only assume that he was in more pain or discomfort than we realised. Here’s to a lot more Ziggy in our lives.

 

  1. Uh-huh, and I’m still 28
  2. Failing spectacularly, but trying

The only winning move is not to play

Like many, many others, I’ve been following the events in Ferguson, Missouri with a mixture of fascination and horror. After a couple of nights of reading reports of post-curfew tear gas, armed protesters, tanks, and automatic weapons, it strikes me that escalation on either side of this dispute is a strategy that can only end in wholesale tragedy. Perhaps an alternative approach should be considered.

Simplistically, and possibly naively, there are three classes of agents to consider: Protesters, agitators, and the Police Department.

Protesters are non-violent and exercising their right to protest against a situation they feel is unfair to try to affect change. Agitators are civilians who are using the protests to further their own agenda. The Police Department are the people in camo, pointing automatic weapons, and riding tanks. They are trying to keep the situation under control and avoid innocents being hurt in the process.

It’s clear that the agitators cause the police to escalate, the police escalation causes the protesters to escalate, and more agitators to join. The end game is Global Thermonuclear War.

There are, I think, three potential alternative strategies:

Protesters stay away
This leaves the streets clear for the Police Department to more effectively identify and deal with the Agitators.

Police Department stay away
This is a potentially more dangerous strategy that allows the Protesters to demonstrate and be heard. However, the Agitators are left without control. This may allow the Protesters to separate themselves from the Agitators but at significant risk to themselves.

Protesters and Police Department stay away
With nowhere to hide, the Agitators are trivial to identify. However, it isn’t clear what they’ll do in that situation. Perhaps looting, vandalism, maybe random acts of violence. Best case, they get bored and leave. Worst case, they run wild over the town.

Either way, the continued cycle of military-enforced curfew and escalating protests, and increased attention from unwanted elements, can only end in further and significantly more devastating consequences.

How about a nice game of chess, indeed.

s/GOOG/TWTR/

A little over three years ago1, I left LucasArts to join Google. I have now left Google to join Twitter.

I have an amazing opportunity to join the Mesos team at a point in their development that is ideal: The technology is built, the company has invested heavily in it, but there’s still a huge amount to do and a chance to have a hand in building something great.

When I joined Google, I thought I’d never leave; there are so many projects at the company that I thought if I was ever bored, or just felt the itch, I’d be able to find a new one. This is completely true, but is also part of the reason that I’m leaving.

Google is big. You just won’t believe how vastly, hugely, mind- bogglingly big it is. I mean, you may think it’s a long way down the road to the chemist’s, but that’s just peanuts to Google2. When I joined, Google could be described as an online lifestyle company: Search, Mail, Docs, Android, and Chrome formed the backbone of an internally consistent suite of products. The advent of Google+ fit that model and added the necessary feature of consistent identity. I wouldn’t say Google’s been entirely successful in rolling it out3, but the end goal is reasonable.

The addition of Google[x], robotics companies, and more recently home automation projects, have left Google’s focus unclear. To be more concrete, in any production environment it is necessary to have a shared vision of what everyone is ultimately working on. If that doesn’t exist, it is difficult to understand the context behind senior management decisions, or even to trust your peers are working towards the same goals that you are. I somehow lost sight of that shared vision at Google. Having said all that, there are many brilliant people working on astounding projects at Google, and I’m proud to have been a small part of it for a little while.

What Twitter offers is a clarity of purpose that keeps every line of code focused on a well-defined product. Features may be added, but at its core Twitter is not going to significantly change. Add to that a widely used open-source project that is a key part of Twitter’s infrastructure, challenges of scale that no-one has had to work at before, and you have quite the compelling opportunity.

Replacing a two hour bus-ride with a forty minute walk doesn’t hurt either. There’s also less chance of me being barricaded into my home by protesters.

  1. 1186 days, to be precise
  2. With apologies to Douglas Adams
  3. the conflation of identity with real identity is specifically problematic

URL shortener in go

I recently combined all my various blogs into this one and then realised that, yet again, the length of my name is a pain. Especially when it comes to URLs. I also realised that I’d never tried to build a URL shortener, so I did. Full source available here.

There are a few tutorials around about writing one in PHP, but I decided to use Go. The basic idea is the same.

Database setup

The URL shortener uses a single table with the following structure:

Field Type Null Default Extra
id int(11) No None AUTO_INCREMENT
short varchar(8) No None
long varchar(64) No None
created timestamp No CURRENT_TIMESTAMP
accessed timestamp No 0000-00-00 00:00:00
access_count int(11) No 0

I think the fields are self-explanatory.

Code

The code itself is really simple. The idea is to handle every HTTP request and capture the path of the URI. Look the path up in the database, and return either a redirect or not found. I decided to use go-sql-driver for the mysql integration and gorilla mux for more advanced request routing.

func main() {
        flag.Parse()

        c, err := sql.Open("mysql", *user+":"+*pwd+"@("+*host+")/"+*db)
        if err != nil {
                log.Fatal("Failed to open sql dbConnection")
        }
        dbConn = c
        defer dbConn.Close()

        r := mux.NewRouter()
        r.HandleFunc("/{shorturl}", Handler).Methods("GET")
        http.Handle("/", r)

        log.Fatal(http.ListenAndServe(":"+*port, nil))
}

As you can see, all we do in main is open the connection to the database given the passed-in connection parameters, then set up a simple request routing table.

func Handler(w http.ResponseWriter, r *http.Request) {
        shorturl := mux.Vars(r)["shorturl"]                                                                                                               

        // lookup shorturl
        var longurl string
        var access_count int
        err := dbConn.QueryRow("select `long`,`access_count` from `url` where short=?", shorturl).Scan(&longurl, &access_count)

        switch {
        case err == sql.ErrNoRows:
                log.Printf("%q not found", shorturl)
                http.NotFound(w, r)

        case err != nil:
                log.Fatal(err)

        default:
                access_count++
                log.Printf("%q -> %q", shorturl, longurl)
                http.Redirect(w, r, longurl, http.StatusFound)
        }
}

The single handler is almost as straightforward. It queries the database to see if the short URL is registered and then either returns a redirect or not found, or fatals in the case of a SQL error. An extra feature that I might use later in a dashboard view is that it tracks the access count and last accessed time for each registered short URL.

We also don’t need to worry about little Bobby Tables as Go escapes the parameters for us.

Running

One final wrinkle is that I use Dreamhost for hosting and so can’t run a native Go server. However, with a little mod_rewrite trickery I can persuade Apache to forward requests to my server. Here’s the relevant lines of the .htaccess file:

RewriteEngine on 
RewriteCond %{SERVER_PORT} !^4242$ 
RewriteRule ^(.*) http://%{SERVER_NAME}:4242%{REQUEST_URI} [R=301,L]

This ensures that the given port isn’t already the one our server is listening on, and then redirects to the host with the write port number.

Next steps

Currently, short URLs have to be added manually through a SQL interface. Ideally, there’d be some UI for adding short URLs, and maybe even generating them through some hash function. Though checking for and handling hash collisions in a database is a pain.

All the small things

Someone, on checking out my github page, recently commented that I’m more of a breadth than a depth developer. I’m not sure that this is entirely accurate, though I didn’t take offence, but it is true that many of my personal projects are small one-off toys. This post is a tour of some of them.

dmanix

I didn’t study Computer Science at university1 and I sometimes feel like I missed out on some key programming projects like writing your own operating systems, compilers, and path tracers. I’ve been steadily remedying this and dmanix is my attempt at an operating system.

It only runs on 386 and has a scheduler, ramdisk, memory allocation, etc. The next step will be writing an ELF parser and compiling programs for it.

The only thing that separates it from other *nix OSes is that it’s written in more C++ than C.

lru-cache

One of the interview questions I ask on occasion2 is to implement an LRU cache in C or C++. The idea behind an LRU cache is that you have a fixed capacity cache in which you want to store data such that only the most recently used things are in it. In this case, I’m storing key/value pairs.

The specific reason why this is an interesting implementation is that it uses only operations that are average-case O(1). Ie, it should scale without significant performance reduction. This is managed by using both a list and an unordered_map; the unordered_map to store the key-value pairs for fast lookup, and the list to track how recently items are accessed.

buckets

There’s a well-known class of puzzles in which one is asked to fill a container using two other odd-sized containers. For example, fill a 5l container using a 3l and 4l container. I don’t enjoy these puzzles so I decided to write code to solve them for me.

It uses a depth-first graph search algorithm  to explore the solution space and then prints out the steps needed to solve the problem. There’s one issue which I should fix at some point: It might be better to use a breadth-first search as the solution space is relatively small and this would return the shortest solution.

queueserve

One of the things I’ve been doing recently is learning Go3. I felt that I needed to get to grips with the scalability and use for a web service, so I built a highly concurrent queue server and client.

It uses JSON over HTTP as a wire protocol and supports many, many concurrent clients thanks to Go’s built-in support for concurrent HTTP serving. The server is using lock-free queues to maximize concurrency.

evernote

Evernote have a series of coding challenges on their careers website. I enjoy a challenge so I tackled the problems. No-one ever contacted me so I don’t know if I did well, but my code passes all of the test cases and is relatively fast. All the other solutions posted around the web are in Python (that I found, anyway) so I thought having C++ solutions would be a neat change.

 

  1. Theoretical physics, if you were wondering
  2. and presumably can’t ask any more after posting this
  3. the language, not the game

gomud

I needed an excuse to learn go, and none of the usual candidate projects that I usually use to pick up a new language seemed to fit well. I was thinking back to how I started programming, and the transition from hobby programming to professional was largely thanks to working as a creator on Discworld MUD. It struck me that a MUD could be the perfect platform, given the support for concurrency and networking. And then I realised it would be even more interesting to write a MUD engine that others could use to develop MUDs. So I did: gomud.

Right now you can define rooms that have exits, players are persistent (though there are no passwords yet) and supported commands include ‘say’, ‘tell’, ‘me’, ‘shout’, ‘who’, ‘look’, and ‘go’.

Future plans include defining commands through data for easy expansion, add passwords for players, items, creatures, and then things like combat, skills, etc.

It turns out that it really is the perfect vehicle for learning go. The network support and concurrency in the language lend themselves really well to the core loop for a MUD, and the message passing works wonders for sending text to different connected players. Built in testing means that unit test coverage is strong, and it’s really easy to hack something up that works before iterating it into something more modular and well-structured.

If you can’t tell, I’m somewhat enamoured with both the project, and the language. I’m open to pull requests, so if you also want to get started with the language, or if you want to use it to build a MUD, please get in touch and help me build something neat.

Hole hearted

Rodney Jones is a jazz guitarist who has played with some of the greats, including James Brown and Dizzy Gillespie. This may seem largely irrelevant but it’ll make sense later.

I rarely play the guitar around those outside my immediate family. I’ve been playing for over 20 years but I have no sense if I’m any good, or just another schmuck with a guitar. Every now and then I get the urge to record something, and that urge has been getting stronger ever since the geekling appeared. It’s almost as if I have a greater need to leave a legacy, and I’d like her to be able to hear me playing before I get too old to pull off the more complex stuff. That urge never really amounts to anything, though I did recently get a step closer by buying a cable. Maybe eventually I’ll get a mic, sit down and put something down. But even if I did, I doubt that I’d do anything with it other than squirrel it away on a drive somewhere to be discovered in a few years.

Through some unlikely course of events, I won a one-on-one lesson with a Juilliard guitar teacher. I didn’t expect much from it, maybe a prescription of scales from some completely unimpressed classical guitarist, sneering at my weak attempts to channel Nick Drake and Robert Johnson. Instead, at today’s lesson, I met my teacher: Rodney Jones (see, I told you it would be relevant).

Rodney asked me to play for him, my fingers froze, my pores opened and sweat soaked my neck. I couldn’t remember anything and my fingers wouldn’t do what I wanted. I took a breath and played some Simon and Garfunkel; something I’ve been playing for so long that muscle and mind memory carried me through. Once I’d loosened up I ran through some Robert Johnson, Eric Clapton, and finished up with a bit of Nick Drake. He asked me to play something with a pick (something he’s known for) and I couldn’t resist banging out some Extreme1.

He asked if I wanted the good news or the bad news. I laughed and asked for both, in any order. His response: “The good news is, there’s no bad news”.

He then went on to tell me that I have a natural touch with the guitar, that I have plenty of talent, and he was pleasantly surprised. And yes, I’m bragging a little, but this is my blog and if I can’t do that here, where else can I do it? So we talked a bunch more, he played for me for a few minutes to show his picking technique2, and then he prescribed me scales after all.

But I played for a stranger, a stranger who is a legendary guitarist, and he didn’t laugh me out of the room. Maybe I should get that mic after all.

And practice my scales.

  1. Kids, ask your parents. Pornograffitti was a seminal album.
  2. o_O

Typed data for performance boost

After reading John McCutchan’s recent post on numeric computation in Dart, and a conversation with Srdjan Mitrovic from the Dart VM team, I made some changes to the Dart port of Box2D in the hope of improving performance. The specific commits that make up this change are here and here, and this change has been released to pub as version 0.1.6.

Originally, I had attempted to port the internal math library to John’s vector_math library. For one, this would mean that I no longer need to maintain a chunk of code, and for another it means any work he does to introduce SIMD into the library will automatically be available to me. However, this integration introduced some major instabilities into one of the collision solvers so I abandoned it.

Instead, I made two key changes to the code: Converted all num types to double, and converted the vector and matrix types to use typed data instead of double stored in fields. First, the results. All benchmark code can be found here and were run under SDK version 0.5.11.1_r23200. In all the below charts, the y-axis is the number of steps simulated per second, hence bigger is better.

Results

ball_drop

This simple benchmark shows little difference in performance across the board.

ball_cage

This simple benchmark shows a performance regression for higher step counts. This may be a sign that some parts of the code haven’t been updated to use doubles so the VM is having to do extra work.

circle_stress

The first of the complex benchmarks with many collisions per frame shows a huge improvement across the board. The performance was always fairly consistent, but now it is consistently better.

domino_platform

The most complex benchmark (the one that exhibits the collision instabilities under vector_math) also shows huge improvement in performance.

Rationale

It is clear from the above results that these changes have made a significant positive difference to performance, but why? We’ll tackle each part of the change in turn.

doubles

The VM can work with doubles in an unboxed form which makes computations very efficient. This does, however, break down when passing doubles to functions or returning them from methods as this causes a box/unbox operation pair. Similarly, storing a non-smi number in a field, as the old math library was doing, causes a box operation as fields can only hold smi values or object pointers. And that’s where typed lists come in.

Typed lists

Typed lists in Dart can only hold numbers, not regular objects. Further, unlike when working with integers, the double value does not need to be tagged. This means that the storing and retrieving doubles from a typed list is very fast, and also very memory efficient.

JavaScript

This is all well and good for the VM, but what happens when this code is converted to JavaScript, which is going to be the majority use-case at this point? JavaScript stores all numbers as double precision floating point numbers anyway, so there’s no conversion necessary there. Also, the typed lists map trivially to the native typed arrays in JavaScript which also means no further overhead.

Conclusion

If you’re doing any kind of numeric computation in Dart, read John’s article again and follow his advice. The tips he gives are not theoretical; they have a significant practical performance impact.

Star Trekkin’

My review of Star Trek Into Darkness can be succinctly written: As a film, I enjoyed it. As a Star Trek film, I didn’t.

There are two specific reasons why it didn’t work for me as a Star Trek movie. First, after the first J J Abrams movie, I was encouraged that they’d found a way to build a fresh start for the young versions of well-known characters. They completely threw that away in the sequel, though, rehashing old stories with the most simple, and not as clever as they seemed to think, twists.

Secondly, one thing that differentiates Star Trek from other science fiction movies is the attempt to justify the science bits while presenting the fiction bits. In this film, however, when someone asked why one ship could catch up to another in warp, the only reason given was that they had “advanced warp technology”. Not enough detail for this science nerd and long-time Star Trek fan.

However, in the spirit of being constructive, here’s my pitch for the next film.
Warning: Possible spoilers for Star Trek Into Darkness ahead. 

Dr Carol Marcus takes the blood from Kahn and his army in combination with the prototype photon torpedoes to create the Genesis Device. When it is used to terraform a planet, the doctor is caught in the blast. This causes her unborn son (David Marcus, fathered by Kirk who is not aware that Dr Marcus is carrying his baby) to develop at a rapid rate. During the final showdown with the Klingons, who are trying to steal the Genesis for use as a weapon, an elderly David sacrifices himself to save Kirk and the crew, leaving Kirk forever embittered towards the Klingons.

Bonus points if old David Marcus is played by William Shatner.

Getting started with DartBox2d part 2

This is an update to this post as dartbox2d has undergone some drastic changes since that was written over a year ago.

Getting the code

The first of the main differences is that dartbox2d is now a package hosted on pub. Getting the code is a matter of having a pubspec.yaml file in the root of your project that looks something like:

name: dartbox2d-tutorial
version: 0.0.1
author: Dominic Hamon <dominic@google.com>
homepage: http://dmadev.com
dependencies:
    box2d: ">=0.1.1"
    browser: any"

The dependencies section is the important bit. This lets pub know that this application depends on any version of box2d with a version number greater than 0.1.1. That version uses the vector_math package instead of a hand-rolled math library and runs against the latest (as of this writing) dart sdk r19447. It also depends on browser which we’ll need to pull in the script to allow the code to run both as dart and javascript.

Then, just call $DART_SDK/bin/pub install and the box2d package will be installed. If any errors about conflicting dependencies are printed, please let me know.

The HTML page

<html>
  <body>
    <script type="application/dart" src="tutorial.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>

The Dart code

At the top of the dart file, you would now import the libraries you need:

library dartbox2d_tutorial;
import 'dart:html';
import 'package:box2d/box2d.dart';

Note that the box2d package actually contains two libraries; box2d.dart for use with the VM and box2d_browser.dart for use in the browser. The only difference is that the latter enables debug drawing functions using dart:html. If you’re planning on running through DartEditor, you probably want box2d_browser.

The rest of the tutorial works almost as is, though the new math library means that initializeWorld should look like:

void initializeWorld() {
    // Create a world with gravity and allow it to sleep.
    world = new World(new vec2(0, -10), true, new DefaultWorldPool());

    // Create the ground.
    PolygonShape sd = new PolygonShape();
    sd.setAsBox(50.0, 0.4);

    BodyDef bd = new BodyDef();
    bd.position.setCoords(0.0, 0.0);
    Body ground = world.createBody(bd);
    ground.createFixtureFromShape(sd);

    // Create a bouncing ball.
    final bouncingBall = new CircleShape();
    bouncingBall.radius = BALL_RADIUS;

    final ballFixtureDef = new FixtureDef();
    ballFixtureDef.restitution = 0.7;
    ballFixtureDef.density = 0.05;
    ballFixtureDef.shape = bouncingBall;

    final ballBodyDef = new BodyDef();
    ballBodyDef.linearVelocity = new vec2(-2, -20);
    ballBodyDef.position = new vec2(15, 15);
    ballBodyDef.type = BodyType.DYNAMIC;
    ballBodyDef.bullet = true;

    final ballBody = world.createBody(ballBodyDef);
    ballBody.createFixture(ballFixtureDef);
}

And the switch to dart:html leaves initializeCanvas looking like:

CanvasElement canvas;
CanvasRenderingContext2D ctx;
ViewportTransform viewport;
DebugDraw debugDraw

void initializeCanvas() {
    // Create a canvas and get the 2d context.
    canvas = new CanvasElement(width:CANVAS_WIDTH, height:CANVAS_HEIGHT);
    document.body.append(canvas);
    ctx = canvas.getContext("2d");

    // Create the viewport transform with the center at extents.
    final extents = new Vector(CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2);
    viewport = new CanvasViewportTransform(extents, extents);
    viewport.scale = VIEWPORT_SCALE;

    // Create our canvas drawing tool to give to the world.
    debugDraw = new CanvasDraw(viewport, ctx);

    // Have the world draw itself for debugging purposes.
    world.debugDraw = debugDraw;
}

and run as:

void run() {
    window.animationFrame.then((time) => step());
}

void step() {
    world.step(1/60, 10, 10);
    ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
    world.drawDebugData();
    run();
}

Also, though the frog and dartc compilers have been replaced by dart2js so this is compiled to javascript using:

$ $DART_SDK/bin/dart2js tutorial.dart -otutorial.dart.js

There are a host of examples in the demos folder of the DartBox2d source here.