Original link: Don't be too clever: Optimization through simplification / Stac.
The code shown is C++, but the principles are pretty universal.