Overhead of “boxing” primitive types via implicits in Scala?

Well escape analysis should mean that the most recent JVMs do not actually have to create your rich wrapper in order to call the addMonth method The extent to which this actually occurs in practice will obviously depend on how much of a runtime hotspot the JVM decides that these methods are adding in object creation. When escape analysis is not happening, obviously the JVM will have to "box" (as you say) the Long in a new instance of the wrapper class. It doesn't involve "class creation" - it would involve "creating an instance of a class".

This instance, being short-lived would then be GC-d immediately, so the overhead (whilst small) is: memory allocation for the instance GC-ing the instance These are obviously only going to be any kind of problem if you are definitely writing very low-latency code where you are trying to minimize garbage creation (in a tight loop). Only you know whether this is the case As for whether the approach will work for you (and escape analysis come to your aid), you'd have to test in the wild. Micro-benchmarks are notoriously difficult to write for this kind of thing The reason I don't quite like these type aliases being part of a public API is that scala does not really enforce them as strictly as I would like.

For example: type PrimitiveDate = Long type PrimitiveSpeed = Long type Car = String type Meeting = String var maxSpeeds : MapCar, PrimitiveSpeed = Map. Empty //OOPS - much too easy to accidentally send the wrong type def setDate(meeting : Meeting, date : PrimitiveDate) = maxSpeeds += (meeting -> date).

Well, escape analysis should mean that the most recent JVMs do not actually have to create your rich wrapper in order to call the addMonth method. The extent to which this actually occurs in practice will obviously depend on how much of a runtime hotspot the JVM decides that these methods are adding in object creation. When escape analysis is not happening, obviously the JVM will have to "box" (as you say) the Long in a new instance of the wrapper class.It doesn't involve "class creation" - it would involve "creating an instance of a class".

This instance, being short-lived would then be GC-d immediately, so the overhead (whilst small) is: memory allocation for the instance GC-ing the instance These are obviously only going to be any kind of problem if you are definitely writing very low-latency code where you are trying to minimize garbage creation (in a tight loop). Only you know whether this is the case.As for whether the approach will work for you (and escape analysis come to your aid), you'd have to test in the wild. Micro-benchmarks are notoriously difficult to write for this kind of thing.

The reason I don't quite like these type aliases being part of a public API is that scala does not really enforce them as strictly as I would like. For example: type PrimitiveDate = Long type PrimitiveSpeed = Long type Car = String type Meeting = String var maxSpeeds : MapCar, PrimitiveSpeed = Map. Empty //OOPS - much too easy to accidentally send the wrong type def setDate(meeting : Meeting, date : PrimitiveDate) = maxSpeeds += (meeting -> date).

Good point on the "enforcing aliases". That was one thing I was wondering about. So there is no way of creating a proper type alias for a primitive type in scala?

– ziggystar Oct 20 '10 at 14:56 1 @ziggystar - no there is not. I tend to use type aliases for one of two things: private internal stuff like Kevin talks about below; package object imports (i.e. Why List still seems to be in the scala package) – oxbow_lakes Oct 20 '10 at 15:10.

You haven't actually created a new type in your given example, it's simply an alias for the pre-existing Long type. It's a technique I use quite often to deal with unwieldy nested connections. For example, I'd alias type Grid = SeqSeqInt to avoid having to specify SeqSeqInt over and over again for various parameters.

You can quite happily pass a Long to a method taking a PrimitiveDate method, although you do have the advantage that the code is much better self-documented. If you really do want to create a new type with enforced type-safety and convenient pattern matching, I'd use a case class: case class PrimitiveDate(value:Long) and, potentially, even provide an implicit Long=>PrimitiveDate conversion for convenience.

The reason I don't quite like these type aliases being part of a public API is that scala does not really enforce them as strictly as I would like.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions