Stack-hack optimization causes much re-computation in GUI callbacks
This is a duplicate of #1168, recorded for posterity here, at the request of Simon PJ,
An IO lambda is created within main's scope, and this is given to the GLUT GUI library (or it could be GTK or wxHaskell) as a callback to draw the contents of the window. The callback lambda captures a value from the outer scope, but the state-hack inlines the value's defining expression into the callback. Thus, the value is re-computed every time the callback is called.
-fno-state-hack fixes it.
The attached program is a 3D model viewer. I've attached two models, one is small, the other larger. The performance hit is quite noticeable on the large one. The models need to be unzipped before running.
gunzip torus.obj.gz ./ObjView torus.obj
use x y z to rotate and force a redraw. When compiled with -O0 or -fno-state-hack, you'll see "BUILDING MESH" output once, otherwise it will be output on every redraw.