Skip to content
This repository has been archived by the owner on Sep 19, 2019. It is now read-only.

Fix stack overflow issues #29

Open
deepakjois opened this issue Feb 17, 2012 · 4 comments
Open

Fix stack overflow issues #29

deepakjois opened this issue Feb 17, 2012 · 4 comments

Comments

@deepakjois
Copy link
Owner

repeat 4000 [repeat 34 [fd 12 rt 10] rt 90]

causes stack overflow.

@byorgey
Copy link
Contributor

byorgey commented Mar 10, 2012

I looked into this a bit. After doing a bit of profiling I'm pretty sure the problem is that logoseg and modifyTrail are too lazy:

-- Adds a segment to the accumulated path.
logoseg :: Monad m => (Segment R2) -> TurtleT m ()
logoseg seg = ST.modify
  (\(TState d ang p) ->
     TState d ang $ modifyTrail
       (\(Trail xs c) -> Trail (rotate ang seg:xs) c) p)

modifyTrail :: (Trail v -> Trail v) -> Path v -> Path v
modifyTrail f (Path ((p, t) : ps)) = Path $ (p, f t) : ps
modifyTrail _ p = p

Each time logoseg is called, instead of actually updating the state, it just creates a thunk -- only when the final path is needed at the end are all these thunks actually evaluated.

Not sure off the top of my head what the best way is to add the required strictness.

@deepakjois
Copy link
Owner Author

This blog post may have some useful pointers. http://blog.johantibell.com/2012/02/forcing-values-returned-from-monadic.html

@deepakjois
Copy link
Owner Author

Very relevant stack overflow post: http://stackoverflow.com/questions/7998458/why-does-this-simple-use-of-the-state-monad-cause-a-stack-overflow

@byorgey on IRC suggests

  1. compile with -rtsopts (cabal install --ghc-options="-rtsopts")
  2. run with +RTS -hT -RTS
  3. visualize output with hp2pretty.

@byorgey
Copy link
Contributor

byorgey commented Apr 9, 2012

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants