Premature Optimization is the Root of All Evil… Right?

The other day, I thought it would be fun to play with a little OpenGL cube-drawing code.

The idea came up after finishing the “Getting Started” section on Learn OpenGL (great tutorial, btw). You start with some super simple triangle stuff and build up to a few cubes spinning around. Oh, and a first person camera to check out your handy work. Basic, but a great start.

Before the tutorial, I had worked through part of the big red book to do some 2d tilemap stuff (instancing is great). However, this was my first step into the 3rd dimension.

Let’s talk about how I draw the cube

I already knew that there was a much better way to draw cubes than what Learn OpenGL presents. Instead of using 36 vertices (2 triangles per face * 3 vertices per triangle) I got it down to 24 using a simple, and not optimal, triangle strip.

Dammit, I wanted to do better than that. Naturally, I asked Google what the best way to draw a triangle was, and found this post. Nice.

You may have noticed the link to a paper on the subject. Now I have all the tools to draw the perfect cube! Kinda.

A few minutes later, I cut down my vertex buffer to only 14 vertices! But, could I go further?

Like an Obsessed Optimization Loon…

I wanted more. Martin, a friend of mine, is particularly good at figuring out interesting ways to draw stuff with computers. I went to him after figuring out that if I used the optimal number of vertices for a cube, I would need to satisfy this table:

            id     x     y     z
             0    -1    -1    -1
             1     1    -1    -1
             2    -1     1    -1
             3     1     1    -1
             4     1     1     1
             5     1    -1    -1
             6     1    -1     1
             7    -1    -1    -1
             8    -1    -1     1
             9    -1     1    -1
            10    -1     1     1
            11     1     1     1
            12    -1    -1     1
            13     1    -1     1

These are the values I would need to calculate if I wanted to only pass in the center point and dimensions of the cube. This is where my brain started to melt.

Can you see the patterns? Well, Martin could. Like a scene from A Beautiful Mind, he worked out a crazy formula that would figure out the location of all 14 vertices.

This was exactly the sort of thing I was trying to figure out, but I would have never gotten far enough to be dangerous. Here is the crux of the shader code:

float3((int3(id | id2, (id1 ^ id2) ^ (id & id2), ~id & id2) & 1) ^ (vertex_id < 7 ? 0 : int3(1, 0, 1))) * 2.0f - 1.0f

I don’t know about you, but I’m digging it

I’m guessing at least half of you are yelling at me through your monitor. “It’s just a cube, moron! You don’t need these ‘premature optimizations‘ for such a simple task!” (by the way, here’s Knuth’s entire quote)

Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

Eff that. This optimization was fun and I learned a shitload from two brilliant friends, brain-melting trial and error, and hacking away at my OpenGL code. In fact, this isn’t the only approach to making this cube as slick as possible. Jeroen has a few things to say about Karnaugh Maps and how programmers could benefit from bringing them over into the software world rather than leaving them for the electronics crew.

Don’t settle with doing things the boring way. Optimize your ass off if you enjoy it. That’s what I do: enjoy myself.

I Don’t Really Know What I’m Doing

I recently started writing more on this blog, I’m trying to be more active on Twitter and Facebook, and I’m trying to make good stuff.

There is one problem…

I don’t really know what I’m doing

Here’s the point: I’m trying to find my way and I have some plans.

  1. Write code EVERY DAY, share it on GitHub
    1. My debugger, Lysa
    2. Learning Rust
    3. Contributing to open source
  2. Read the words of brilliant people as often as possible
    1. Knuth is a bamf – The Art of Computer Programming is gonna be fun
    2. Computer Science greats
    3. Modern day wizards – the programmers I look up to
  3. Try to express my own opinions about programming
    1. I love writing systems code – let me modify the memory, please
    2. Conventional wisdom about programming is rarely wise
    3. Don’t get me started… yet

That’s It

I just wanted to quickly jot down my plans. For myself, and anyone watching.

You stay classy

What Open Source Project to Join – How I Chose the Project for Me

I’m on a sort of “mission” to become an open source contributor. For years, I have told myself that I would start writing open source code, but I’ve never actually helped out with an established piece of software.

A couple of weeks ago, I flipped a switch in my brain and decided to commit to getting to work. Here’s how I chose what to do first.

Find Some Potential Projects

I already talked about some top open source projects that I am interested in, but now we’ll get into how I picked the right one for me.

When choosing what you want to work on, figure out a few things that you’re looking for before you start trolling GitHub. Maybe even make a short list of what you need out of a project in order to commit to learning their ecosystem and writing code for them (potentially for decades to come).

Here’s my list:

  1. Most importantly, learn something
  2. The values of the project must match my values
  3. Software that I will use for many years

Find the Docs on How To Contribute

Whenever I started diving into a project page, I would go straight to their guide on contributing. Any good project will have a Markdown file or wiki page on GitHub explaining how you can get involved.

When you’re first starting out (that’s exactly the boat I’m in right now), I think it’s best to find the quickest route to getting some code into the source. You gotta stay motivated to learning and working. Getting some recognition will help with that.

For Rust, here is how the process went for me:

Rust has a big link that goes straight to their “Contribute” page

After reading the quick overview, I found the link which took me to their GitHub.

Since I started learning Rust recently, I wanted to work on some very easy issues that don’t require a lot of knowledge about the language, but will ease me into how the project is structured and allow me to snoop around the source to become more comfortable reading the code.

Now we’re talking. That E-easy tag sounds perfect for me. Time to dig a bit deeper and find an actual issue that will suit my needs. Even though I’m not ready to contribute, this will give me an example of what my first step will be if I choose Rust as my first target.

That first issue in the screenshot is the one. It’s tagged with E-easy and it has 61 comments, which means it’s probably important and has a lot of eyes on it. This is good to make sure I’m doing work that matters (again, for the motivation).

Let’s go deeper and find out what exactly needs done.

This is a metabug for rustc --explain error descriptions.

All the errors in need of descriptions are listed in this spreadsheet. If you’d like to help out, comment with the bunch of errors you’d like to tackle, and list yourself as assigned on the spreadsheet!

Alright, that sounds easy enough. After a bit of digging, I found out that this is something I can handle, it gets me reading through source to find out where these errors are, and it forces me to learn more about the language and the project.

In fact, I had to do a bit of digging just to find out if it was something I could tackle.

I even took the liberty of recommending some changes to the issue itself in order to make it easier for newcomers to help out. Since I had to spend time figuring this out, that means everyone in my situation has to do the same, just to decide to help with this issue.


The above process is pretty specific to my needs, but hopefully you can get the generic bits from it and replicate this for finding your own project. I did pretty much the same thing for each of the open source projects that I was considering and it helped me narrow down my choice to Rust.

I now have target software that I want to help with, an understanding of how the Rust team wants me to approach contributions, and an easy issue to start with.

The goal is to learn as much as I can about Rust while paying it forward to future users by improving the language. It’ll take a long time to make a big difference, but it’s all about Patience and Work.

I Want to Learn How to Contribute to Top Open Source Projects

Contributing to Top Open Source Projects

That’s the goal. At the time of writing this probably-too-long blog post, I am a total noob that has been intimidated by getting into open source software for years. Hopefully some people can learn from seeing me document how I got started and how I will eventually become a major contributor to a top open source project.

I’ll be going from open source scrub to super star contributor. Just you watch.

The Starter Projects

Finding the right project is hard and it took me a long time to narrow down the projects I want to get started with. These are the 5 projects I’m considering:

I. WordPress

You’re reading this on a WordPress-powered site, so I might as well get this one out of the way first.

It get’s a lot of crap for being insecure (among other things), especially from some of my close friends, but there is something to be said for a platform that changed the web as we know it… for better or worse. Even though it is primarily written in PHP, there is plenty to learn from a project like this.

A founding developer of WordPress and CEO of Automattic, Matt Mullenweg, seems to have admirable goals and, as an added bonus, is a cool dude. He is dedicated to open source and has made lots of money along the way. Maybe if I want to be a cool dude like him, I should contribute to his open source project?

II. Nuklear

If you search for trending projects on GitHub, you’ll find Nuklear near the top for repositories that primarily use C. It seems like a great little library for getting an interface up and running quickly using the ‘immediate mode gui‘ (imgui) style.

Unfortunately, the documentation isn’t stellar at the moment, but maybe with a bit of digging I can get good enough to help improve the project (perhaps by writing some stellar documentation).

III. Servo

This one is a doozy. I’m almost certain that I won’t be contributing to Servo from the start, but I had to put it in because it’s a great piece of machinery (and maybe someday I’ll have the balls to dive in). They are building a modern web browser engine from the ground up using Rust. What’s not to like?

Their documentation is great and there is a section for contributing that highlights what to do if you’re new (crucial for me, because I’m dumb and need all the help I can get). I’ll be keeping my eye on this one for the future. You should too.

IV. Neovim

I love Vim. I also love Neovim because it’s like Vim, but better. Their goals are to take what is great about the former and add concurrency, extensibility, and sane defaults. Outstanding.

The Neovim project has some great documentation for contributing, so this one is near the top of my list for first efforts. I also use this piece of software more than any of the others on the list. Even if I don’t settle on Neovim right now, you can bet that I’ll be putting some pull requests in soon.

V. Rust

This monster is one that I’ve only recently began to tame. I had heard about the Rust programming language for years and always thought it sounded great, but never took the time to learn it myself. My excuse was usually that it’s too young and that C is already my best friend.

Now though, it is quickly becoming my mistress-on-the-side because I’m fascinated with what the creators have done with it. Safety and parallelism are huge. I’m super pumped for the grind of learning how to use Rust effectively.

I’ve all but decided that Rust will be the first project I dig into. The docs are fantastic, the community is amazing, I have a lot of fun with it in my free time, and there are tons of issues that can easily be tackled by a scrub like me with no open source contribution experience.

Where to Go From Here

Although Rust will probably be my first target, I hope to get my code into every project on this list. Each one has something unique to learn from and I want to do it all!

Follow along while I stumble through the process of learning how to contribute to top open source projects.