Rewrite fold_lookahead to avoid using the stack and improve time complexity #66

Merged
merged 1 commit into from May 17, 2016

Projects

None yet

2 participants

@ndmitchell
Contributor

As part of my efforts to investigate space leaks, I ended up at the fold_lookahead function, which I rewrote as below. I offer this as an entirely separate patch because there are good reasons you might want to not merge it. Advantages:

  • Reduces stack usage from > 1Mb to < 1Kb.
  • Algorithm more directly states what it does.
  • Better time complexity - previously it was a combination of insertion sort on the Int and a weird order-changing nub construction on the Lr0Item.
  • Passes all the tests.

Disadvantages:

  • Has no meaningful time difference - my tests are with 0.03s.
  • Produces different output since the list is in a different order. I believe the results are equivalent, but you will need to say for sure.
@simonmar simonmar merged commit bc3e126 into simonmar:master May 17, 2016
@simonmar
Owner

The patch looks sensible to me. This codebase is very very very old you know :)

@ndmitchell
Contributor

I was impressed just quite how many foldrs I found in the code base 😄.

@simonmar
Owner

I guess I was going through a foldr-happy phase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment