Tuesday, September 18, 2007

Pointers

People are really bad at writing memory management.

We make mistakes. We will always make mistakes. Heaps of modern software ships with memory leaks these days. Vista still bluescreens.

There's two ways this problem is solved:
  • Application-level memory protection
  • Don't give the programmer pointers (Java, .NET, python, etc)
Memory protection means that if an application uses pointers in a buggy way, it won't stuff up other programs. That program will die, but others won't.

The problem with memory protection is that its really expensive. Context switches take ages because the TLB has to be flushed. On modern operating systems its about ~5 million clock cycles per context switch. I don't know how much of that is due to memory protection, but I think its quite a lot.

Pointerless languages solve the problem in another way. They just don't give the programmer pointers. Simple.

Languages without pointers are often shouted down by the C and C++ zealots because they're slow. (Oh have a cry). Remember though, when you use a pointerless language on a modern computer you're still paying the context switching overhead because memory protection is still used.

How do we fix it?

I don't think telling everyone to become better programmers actually works. Its really hard to write large systems with no memory bugs. Microsoft sure hasn't done it with vista.

Actually, I think the answer is to stop using pointers entirely. If we do that, memory protection is no longer necessary. Without memory protection, everything goes faster (especially threaded programs). Once compilers are more mature, JIT stuff like .NET should perform better than C and it will have no memory bugs.

Best of both worlds for the win.

Hi!

I'm Joseph. I'm a programmer who has lots of opinions and lots of ideas. Unfortunately, I don't have time to implement them all. At least, not today.


The world sucks pretty hard. But its ok - we can fix it.


Why do things suck?
  • People are idiots
  • It takes 10 years to get good at anything.
  • Given enough time, people accept poor design decisions.

The solution?
  1. Pick a problem
  2. Work out how to solve it
  3. Solve it
Duh.

Here I will talk about 1 and 2.