You probably shouldn’t do this, but it works really well

I just added the following config to a ruby test suite:

RSpec.configure do |config|
  config.before :each do
    GC.disable
  end
 
  config.after :each do
    GC.enable
  end
end

It’s pretty tragic how well this works. It knocked about 40% off the runtime of the test suite for our Padrino + DataMapper app running on 1.8.7 (yes, I know. I’m sorry. I actually discovered this as part of my work on getting it off 1.8.7), and a friend is reporting a more than 50% drop for his Rails app running on 1.9.3.

Why? Well, I added this as an experiment when I noticed how much time the test suite was spending in the GC (I initially ran with GC turned off entirely. This ate about as much memory as you’d expect). I’m assuming it’s a combination of MRI’s GC being really quite shit and the fact that test setup tends to produce a lot of garbage.

This is kinda an awful solution to this problem, but I must admit I’m finding it hard to argue with the results. If you can afford to throw memory at the problem it might actually be worth doing.

But still… ouch.

This entry was posted in Code on by .

My personal dietary policy

Let me start this by saying something: I eat both fish and meat. I do not have a problem with this. I have personally bought into the idea that it is OK to kill and eat animals for no other reason than that they are tasty.

Animal suffering? I’m broadly against it. If you give me a choice between two equally tasty steaks, one of which has been factory farmed in horrible conditions and the other of which has been lovingly reared in wide grassy fields, with regular massages and a fulfilling spiritual life before it died peacefully, but this steak costs twice as much, I’d definitely go for the pricier steak. If it cost 10 times as much I might ask if you had somewhere something in between where maybe it was pretty OK with its life, but it wasn’t really fulfilled, ya know? And maybe it had a bit of a headache when it finally got turned into burger. So call it a more than mild preference but not an overarching moral commandment. I feel like I buy into worse things than animal suffering on a daily basis just by being part of the modern world, and that once I’ve agreed that it’s OK to kill them it’s a bit hypocritical to go “As long as you do it without any suffering!”. Other people might set the mark there differently, and that’s fine. This is a personal preference.

So I’m OK with eating meat.

But not, you know, a lot of it.

I think, culturally, our relationship with meat is very dysfunctional. We eat far too much of it – I believe more than we have at any point in history (that may not be true right at this minute – I believe it’s gone down a bit, especially since the recession, but this is certainly true if you count the last 50 years or so). Meat used to be a luxury item, and now it’s a staple.

That being said, we do a lot of things differently than we did historically. That’s not necessarily a bad thing. I state this merely as evidence that we don’t necessarily need to be eating this much meat.

So why do I care?

The main reasons I care are ecological ones: Meat production is polluting and energetically expensive. It’s by no means our worst crime of this sort, but it’s a pretty significant one. Fish is even worse. We are literally driving the fish supply to extinction by our fish addiction (I haven’t formed a very coherent opinion on farmed fish. I’m broadly pro it, but think it is usually ecologically worse than farmed meat. This may not be true. I haven’t done the research as well as I should)

Additionally, I think our current expectations around meat are the source of a lot of the worst excesses of the meat industry: We expect a lot of it and we expect it cheaply. What did we think farmers were going to do achieve this? Plant more cow-trees? When you’re dealing with something where the overwhelming majority of the costs are from living creatures, the obvious way to cost-cut is to treat those living creatures worse. This is true with animals, and it’s also true with humans. Why do you think sweatshops exist?

How do I propose to fix this?

Well. Were I in charge, I would probably fix this by increasing taxes on meat production and using the proceeds to subsidise suitable vegetarian alternatives. It’s a blunt tool, but an effective one. Sadly, my plans to become supreme world leader have been delayed slightly, so in the mean time I’m limited to following my personal plan (and trying to convince you to do so as well).

It’s a pretty radical proposition, which you probably wouldn’t have thought of as a solution to the “People eat too much meat” problem.

Are you ready for it?

Eat less meat.

Shocking, right?

This is, of course, completely fucking useless advice.

The problem with advice like this is that it’s not quantifiable. How much is less meat? It’s easy to slip, and to let boundaries shift. I should know: I tried to live by this nebulous advice for a year or two after stopping being vegetarian. One day I woke up and realised something: I was eating a hell of a lot of meat.

Which is why I imposed my current set of rules. I have meat quotas. My rules are as follows:

  • Eat meat no more than once a week (exception: If I cooked it myself, I’m allowed to eat leftovers)
  • Eat fish no more than once a month
  • Given the option, prefer ethically sourced meat
  • Given the option, prefer less energetically intensive meat (in practice this means “If there’s a tasty chicken based option, take the tasty chicken based option. Otherwise don’t worry about it”)

These are stricter quotas than I expect most people to follow. I was vegetarian for 5 years and pescatarian for another 5 after that, so I know well how to live without meat and fish. It forms the core idea of how I think this should work though: Hard and fast numbers, flexibility about how you implement them.

If you are currently a meat eater but agree that you should be eating less meat, this is the strategy I would propose following:

  1. Think about how often you eat meat (or whatever you’re trying to adjust your intake of) in a given week. Take the median, or maybe a value slightly above median but well below the maximum
  2. Impose that as your starting quota
  3. Once a month (say on the first Monday of a month), consider how hard your meat quota has been to stick to. If the answer is “easy”, adjust it down slightly
  4. Stop tinkering when you hit a point you are ethically and practically comfortable with

You start off at more or less your usual habits, and gradually adjust downwards. The quotas give you a specific, measurable, goal and makes you generally aware of how much meat you’re eating. You do have to adjust your lifestyle but, well, that’s the point, and you have a real sense of progress whilst doing so.

As an aside, this is quite distinct from approaches like Meatless Monday, which anecdotally seems not to help. I’ve seen a few people do it, and all that happens is that they eat more meat on the surrounding days. Additionally, the lack of flexibility makes them really resent it because often they’ll get invited out on that day.

The nice thing about the quotas is that they are strict but flexible – you impose hard limits about how much you can eat, but allow flexibility in terms of when you do it. It’s worked very well for me, and I think with some experimentation to find boundaries it should work very well for everyone.

This entry was posted in Food, life on by .

Nontransitive dice are unsurprising

Every now and then someone finds out that nontransitive dice exist and is surprised by this fact. I in turn am always kinda surprised that their existence is surprising.

Why? Well, basically I don’t expect things to be transitive. Transitivity is a very nice property. When the universe hands you a transitive (total) property it’s basically saying “Hey, here. You’re in luck. The decision problem on this is easy, so you can spend your time worrying about other things”. The universe is a bit of a dick, so I don’t really expect it to do that very often.

So what is transitivity? Transitivity basically says “If A is better than B and B is better than C then A is better than C”. So something is nontransitive if you can find A, B and C where A is better than B, B is better than C and C is better than A.

In particular, nontransitive dice are a set of dice A,B,C such that if you roll pairs of them and the one with the highest score wins, A will consistently beat B which will consistently beat C which will consistently beat A.

They’re quite neat, but their existence shouldn’t be surprising. This is basically a post about how you might go about constructing non-transitive dice if all you had was an inclination that they might exist. Hopefully by the time you get to the end of it you should find the fact that they exist as unsurprising as I do.

We’re going to start with something apparently unrelated: An example of non-transitivity which is especially dear to my heart. Condorcet’s Paradox.

What is Condorcet’s Paradox? It’s the following:

Suppose you run a voting system in which each voter ranks each candidate in order of preference. That is, a vote looks something like “A,B,C” to mean “I prefer A to both B and C and I prefer B to C”.

Given votes cast like this, it is possible to find yourself in a situation where the majority of people prefer A to B, B to C and C to A (where “prefer” here means “ranked higher”). i.e. even though each individual vote gave a transitive ordering of the candidates, the majority preference does not.

How is this possible? Well, lets see if we can construct a simple example.

Well, first lets convince ourselves that this is impossible with only two voters:

The only way two voters can have a majority preference is if they both agree on an ordering.

So if A is preferred by the majority to B and B is preferred to the majority by C then both voters have ranked A ahead of B and B ahead of C. This means that also both voters have ranked A ahead of C, and so A is preferred to C.

So it’s impossible with two voters. Can we do it with three?

Well, in the same way as with two voters, if we have a unanimous preference between two candidates this isn’t going to work. So if this can possible work, for each pair we need to have two voters ranking it one way and one ranking it the other. So lets start with how the votes should look for A and B.

A,B
A,B
B,A

Now we need to figure out a way to insert C into the rankings in order to make this work.

We need to put C after B twice. If we do this for both the A,B votes then we’ll have A and B both preferred to C, which we don’t want, but we have to do it for one of them, which means we need one vote which is “A,B,C”. We also need to do it for the third vote, so this must look like either “B,C,A” or “B,A,C”.

We’re now down to few enough possibilities that we can just try things and see if they work. Let’s try B,C,A.

So we have two votes:

A,B,C
B,C,A

And one which is either A,C,B or C,A,B (because we need C to be before B in this ranking). We want C to be preferred to A in order to get non-transitivity, and they’re currently tied, so we need to have C before A, so this has to be C,A,B.

So our votes are now:

A,B,C
B,C,A
C,A,B

Our tallies now look like:

A is preferred to B because votes 1 and 3 put it before B.
B is preferred to C because votes 1 and 2 put it before C.
C is preferred to A because votes 2 and 3 put it before A.

So we do indeed have A preferred to B preferred to C preferred to A.

I regard Condorcet’s Paradox as basically the protoexample of intransitivity: I find that 9 times out of 10 if you have an intransitive relation you can construct an example that looks like Condorcet’s Paradox.

Let’s see how to do that with our dice:

We’re going to construct dice which basically look like voters.

Specifically each of our dice will have opposite sides marked with the same value, so they can take only three distinct values. We will label these values “Low, Medium and High”. Values may be in the range 1-9, with low values being 1-3, medium being 4-6 and high being 7-9.

The idea of our construction is that any high roll will beat any medium or low roll, etc. Further each die rolls low, medium or high with equal probability. So the only thing that determines the winner will be which one wins when they have the same value type. These categories low, medium and high will be our voters 1, 2 and 3, and one die will beat another most of the time if and only if the majority of voters rank it more highly.

So we want:

Low: A,B,C
Medium: B,C,A
High: C,A,B

Thus A will beat B if we roll low or high. B will beat C if we roll low or medium. C will beat A if we roll medium or high.

Conveniently, we have constructed our categories with three values each, so all we need to do is assign each of those values to a different die to get the ranking.

So:

A takes the values 3,4 and 8 (best low, worst medium, middle high)
B takes the values 2,6,7 (middle low, best medium, worst high)
C takes the values 1,5,9 (worst low, middle medium, best high)

And we get the desired result: On a category tie, A will beat B two thirds of the time, B will beat C two thirds of the time and C will beat A two thirds of the time.

We didn’t deliberately try for it, but it’s also worth noting that these all have the same expected score: 3 + 4 + 8 = 2 + 6 + 7 = 1 + 5 + 9 = 15. So they all have an expected score of 5.

As a side note, the actual victory chances are much lower than this: A tie will only happen a third of the time, and the rest of the time each die wins exactly half the time, so the real advantage is \(\frac{1}{3} \times \frac{2}{3} + \frac{2}{3} \times \frac{1}{2} = \frac{5}{9}\). So they will win more than half the time (just shy of 56% of the time), but not by much.

So there we have it. Non-transitive dice. This isn’t the only way you can get them – you can make it work with only the normal values 1 to 6 for example – but it’s probably the simplest example to understand, and now that we’ve shown that it’s possible the rest is just optimising for specific details.

This entry was posted in Numbers are hard on by .

Yesterday Was Change Everything Day

Advance warning: This blog post is a bit rambly and pointless. It’s pretty much “Hey, here’s some stuff I changed about my computer” brain dump. You probably don’t care.

Yesterday I made massive invasive changes to the software I’m running as part of my day to day life.

What triggered it? Well, the initial triggering event was something I’m sure you’ve heard about by now: Google reader shutting down.

An appreciable proportion of the information I consume comes through RSS (or Atom), so shutting down google reader is kinda a big deal for me, so I spent some time playing with replacements. I’m currently trying Feedly, although the Android app is so bad I’m not sure I can stick with it.

This put me in the mood to start tinkering with my setup though (a mood which hits me about annually, and then I lose interest for another year, though in this case it’s only been six months).

So I was kinda in the mood to change my software around, and I was feeling fairly hacked off with Google and uncomfortable with how much of my life lived in their software.

Including my browser. Fortunately, there’s this quite nice alternative to Gogle Chrome. It’s a bit niche, but you may have heard of it. It’s called Firefox. Check it out. It’s not bad.

Actually, you know, it’s really not bad. It’s only been a day or so of using it, but honestly it’s dramatically better than I expected. I’ve not had any problems with it so far and it’s generally been a fairly pleasant experience. Previously when I’ve tried to switch back I had all the expected problems – slow, memory hungry, blah blah blah, but this is easily the equal of Chrome and hasn’t caused me any problems so far.

Discussions around this and google reader caused me to resurrect an old rant to a friend. What rant? This rant!

Tabs are fundamentally wrong.

Tabs are window management reinvented badly by each application. Your application should not be doing your window management. If applications do your window management then each one will reinvent it differently and probably badly. Further, if you actually need your applications to be doing window management that’s a sign that your window manager needs improving.

Of course… pretty much every window manager needs improving in this way.

Then it occurred to me: This is a problem I’ve solved before. I used to run wmii. For various reasons (a post for another day) I stopped. However, when I was using it I felt absolutely zero inclination to use tabs, and in my current xmonad setup I really did need to use them (this is a problem that I can and have fixed in xmonad before, but the fix was a bit clunky when I last used it).

So I decided to give wmii another try. There were some fraught moments when I accidentally broke everything doing it (wmii is not the most user friendly software in its failure modes), but I’ve now got it working and useful.

Honestly? wmii is fucking amazing. If you have the opportunity to try it out you should take it.

So, TLDR, beginning of yesterday I was running this:

  • Google reader
  • Google chrome
  • Mint desktop environment with an Xmonad window manager

Today I am running this:

  • Feedly
  • Firefox
  • wmii with no desktop environment

The only change I’m unhappy with is the one that prompted the whole thing: While Feedly nominally satisfies my requirements for a reader, it’s pretty grossly inadequate. So, still in the market for changing that one.

The resulting changes though? I’m way happier with this setup than with my previous one. So, thanks Google, I guess.

This entry was posted in How do I computer?, life on by .

Blasting patents for fun and profit

My friend Dave Stark has just released his first commercial game.

It’s themed around a subject which I suspect may be dear to many of my audience’s heart: Specifically that of setting on fire, shooting and generally annihilating patents.

The game’s name? Patent Blaster.

It’s a neat little game. It plays a little bit like a particularly bad acid trip, but in a good way. Go check it out.

This entry was posted in Uncategorized on by .