Quantcast

will GHC optimize pattern-matching on integers?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

will GHC optimize pattern-matching on integers?

Patrick Pelletier
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: will GHC optimize pattern-matching on integers?

Rahul Muttineni
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.


Hope that helps,
Rahul

On Thu, Apr 13, 2017 at 3:55 AM, Patrick Pelletier <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



--
Rahul Muttineni

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: will GHC optimize pattern-matching on integers?

Patrick Pelletier
Thanks!  That's exactly what I was hoping.

--Patrick


On 4/13/17 1:41 AM, Rahul Muttineni wrote:
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.


Hope that helps,
Rahul

On Thu, Apr 13, 2017 at 3:55 AM, Patrick Pelletier <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



--
Rahul Muttineni


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Loading...