Implementing state-machines: An evaluation

Posted: Sunday, 2015-10-18 10:15 | Tags: Programming, C++

In the previous article I raised the question on whether -- from a performance standpoint -- one should implement state machines using switch-statements or function pointers.

We saw that switch-statements are compiled to a number of jump/compare instructions that grow at least logarithmically with the number of case-labels. I had to close with the observation that a too contrived example however leads to our compiler actually analyzing our state machine good enough to be able to rewrite our code into unrolled instructions and loops.

In this post I want to discuss a slightly less contrived example that is a (stupid and useless) "parser" for XML data. We will observe that even when the state logic is more complex and transitions depend on unforeseeable input data, the compiler can do smart transformations with switch-statements.


Implementing state-machines: switch() vs. method pointers

Posted: Saturday, 2015-10-17 15:51 | Tags: Programming, C++

Finite-state machines are useful for many purposes: Say we have a long running computation that goes back and forth between different phases and is executed chunk-by-chunk in a main loop that does other things in between (e.g. updating some user interface). Yes, you scream "threads", but there are some situations where those are just not necessary or wanted for whatever reason.

Another common use is building a lexer/parser that reads some kind of input and interprets the incoming characters differently depending on "in what state we are" e.g. whether we just read a '' or not might influence how we interpret the current character.

Most state-machine code I've seen so far is built with giant switch()-blocks and recently I've come to wonder whether this is always the way to go.


Some pinball pictures

Posted: Saturday, 2015-10-17 14:57 | Tags: Pinball

Lots has happened in the last time, among other things, I got a new Job and moved to Berlin. Unfortunately I can not house my pinball project here, so it will stay at my fathers place and I'll hope we will manage to continue it from time to time.

There was some progress on the electronics side meanwhile. Unfortunately I miss some more recent pictures, but still, have some impressions here:


New VIM config

Posted: Saturday, 2015-10-17 14:21 | Tags: VIM, Environment

Albeit still somewhat chaotic (and full with paths specific to my local setup), I believe my current .vimrc might be of use to some.

It features several plugins and features by now which I use every day:

  • NERDTree & CtrlP for easy and quick access to files
  • Some useful git-related plugins
  • A combination of ctags, cscope and clong for C++ symbol lookup and autocompletion
  • Muted mouse operations and arrow keys (makes it much easier to learn the more efficient muvement operations!)
  • What I consider a pretty sexy look. The theme btw. is a slight modification of "base16-default".

My current .vimrc.

VIM Themes & (old) config

Posted: Saturday, 2015-10-17 13:53 | Tags: VIM, Environment

Since my VIM config on this page was a little outdated, I decided to move VIM-related stuff into the blog so I can update more easily.

This post just captures the old state of the formerly static vim page, stay tuned for more.