[Haskell-beginners] will GHC optimize pattern-matching on integers?

Rahul Muttineni rahulmutt at gmail.com
Thu Apr 13 08:41:31 UTC 2017


Hi Patrick,

Yes, this will optimise to "a nice balanced tree of decisions with dense
jump tables in the leafs" [1]. You can check out the comments in [1] for
more details.

[1] https://github.com/ghc/ghc/blob/master/compiler/cmm/CmmSwitch.hs#L37

Hope that helps,
Rahul

On Thu, Apr 13, 2017 at 3:55 AM, Patrick Pelletier <code at funwithsoftware.org
> wrote:

> Suppose I am doing a pattern match on a large number of consecutive (or
> mostly-consecutive) integers:
>
> foo 0 = something
> foo 1 = somethingElse
> ...
> foo 1000 = anotherThing
>
> Will GHC optimize this to a table lookup, or is it going to test each
> integer in turn?  Am I better off using a Vector or Map instead of pattern
> matching?
>
> Thanks,
>
> --Patrick
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Rahul Muttineni
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170413/88ed1ac3/attachment.html>


More information about the Beginners mailing list