Skip to content

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,

http://www.haskell.org/pipermail/glasgow-haskell-users/2008-May/014739.html

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.

Trac metadata
Trac field Value
Version 6.8.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system Multiple
Architecture Unknown
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information