How to avoid boxing/unboxing overhead in pattern matching?

285 views
Skip to first unread message

Aleksey Kladov

unread,
Jan 20, 2014, 12:54:02 PM1/20/14
to scala...@googlegroups.com
Hello!

Recently I have stumbled upon a problem: I was using an extractor to deconstruct a 3d vector into three doubles. Perhaps surprisingly, this "syntactic convenience" led to significant overhead. As far as I understand, it happend because Tuple3 is not specialized for [Double, Double, Double], so boxing was used.

How can I avoid this overhead for primitive types, without giving up pattern matching?

Alexey

Lieven Lemiengre

unread,
Jan 22, 2014, 8:34:04 AM1/22/14
to Aleksey Kladov, scala-user
In 2.12 it will be possible: https://github.com/scala/scala/pull/2848


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Dennis Haupt

unread,
Jan 22, 2014, 8:36:28 AM1/22/14
to Lieven Lemiengre, Aleksey Kladov, scala-user
+1


2014/1/22 Lieven Lemiengre <lieven.l...@gmail.com>

Lieven Lemiengre

unread,
Jan 22, 2014, 10:45:39 AM1/22/14
to Dean Wampler, scala-user
I got the version numbers mixed up, it's possible with 2.11


On Wed, Jan 22, 2014 at 4:29 PM, Dean Wampler <deanw...@gmail.com> wrote:
Scala 2.12 or 2.11? It looks like it was already merged to master some time ago.

dean
--
Dean Wampler, Ph.D.

Aleksey Kladov

unread,
Jan 22, 2014, 11:25:27 AM1/22/14
to Lieven Lemiengre, Dean Wampler, scala-user
Cool, I'm waiting for 2.11 :)


--
You received this message because you are subscribed to a topic in the Google Groups "scala-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-user/mFFDkKjpCgE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-user+...@googlegroups.com.

Jason Zaugg

unread,
Jan 22, 2014, 11:27:55 AM1/22/14
to Aleksey Kladov, Lieven Lemiengre, Dean Wampler, scala-user
BTW, if you don't use extractors and carry around your data in `case class Vec(x: Double, y: Double, z: Double)`, you don't pay any numeric boxing, Tuple/Some wrapping when you pattern match -- the compiler emits direct calls to the case class getters.

Paul's change in 2.11 let user defined extractors achieve the same efficiency.

-jason

Naftoli Gugenheim

unread,
Jan 22, 2014, 5:34:38 PM1/22/14
to Aleksey Kladov, scala-user

The new name-based extractors let you do this IIRC. Not sure but I think they're in 2.11.

--
Reply all
Reply to author
Forward
0 new messages