Difference in FPS between GHCi and Executable
Summary
I am trying the gloss package in Haskell and have been able to build and run some examples successfully on windows.
The problem is animations jerky.
From the gloss' official page:
Q: Animations seem jerky.
A: Make sure you're compiling with -O2 -threaded. Without the threaded runtime, the code that manages the frame rate will behave badly. This is because GHC takes too long to reschedule the gloss process after the sleep timer has expired. With the threaded runtime, most simple animations should run at about 100fps, which is our internal frame-rate cap.
Yeah. After adding the ghc-options: -O2 -threaded
in my cabal file, the jerky was eliminated (when running the .exe
file).
But it still is jerky in REPL.
I have tried the following command in REPL.
cabal repl
ghci> :set -threaded
ghci> :set -O2
when making flags consistent: warning:
Optimization flags are incompatible with the byte-code interpreter; optimization flags ignored.
ghci> main
It doesn't work.
As you can see, the "warning: Optimization flags are incompatible with the byte-code interpreter" and the animation was still jerk.
I asked the question in StackOverflow a few days ago and @Noughtmare suggested two ways to improve GHCi performance.
-
Enabling -O2 with the bytecode interpreter (Since GHC 9.8)
cabal repl yampa-examples-gloss-rotatingcolor --repl-options="-fno-unoptimized-core-for-interpreter -O2 -threaded"
Yeah. There is no "warning" for
:set -O2
in REPL now, but the animation is still jerky. -
Using -fobject-code to avoid bytecode altogether
cabal repl yampa-examples-gloss-rotatingcolor --repl-options="-fno-ghci-sandbox -O -fobject-code -threaded"
It seems faster than before (?), but the animation is still jerky.
After some discussion, @Noughtmare suggested me to open an issue with GHC.
The question is that:
Why there's clearly a difference in FPS between repl and run?
Is there a way to make the repl as fast as the run? (It is useful because repl very convenient for debugging)
Related link: https://stackoverflow.com/questions/77894178/gloss-animations-jerky-and-hope-to-add-o2-to-ghci
Thanks.
Steps to reproduce
Checkout this repo https://github.com/ivanperez-keera/yampa-gloss/tree/v0.2.1 and build with ghc-9.8.1 and cabal-install 3.10.2.1.
Note that the yampa-examples-gloss-rotatingcolor is a very small. I can't believe that such a small app can cause performance issues (in REPL only though)...
Expected behavior
No obvious difference in FPS between repl and run.
Environment
- GHC version used: ghc-9.8.1 and cabal-install 3.10.2.1