What is a Scala cheap type?

I need to use a type (any type) as a marker in implicit form as a type parameter in order to distinguish it from another implicit one. This is strange, but it may be another question.

Since I can use any type, I thought about using the cheapest, in terms of memory and initialization time . In this case, this may not greatly affect performance, but the question is interesting: which one is the cheapest Scala?

In Java, the answer is obviously java.lang.Object . But Scala has several “interesting” types: Any , AnyVal and lower types with possible optimizations around them. The Nothing type cannot be created, so it is excluded from this comparison.

+8
types scala
source share
5 answers

If you select Any or AnyVal , then any primitive that you pass will be boxed to probably exit.

AnyRef really good choice.

If there is no parameterization of parameters, then "primitives" are also good options - Boolean or Int , for example.

And there is also Null , which is a very interesting choice, because it does not highlight anything at all, and it is literal, so it should be fast. Not knowing what exactly you are doing with this, I can’t say whether this choice really is.

Another interesting option is java.lang.Integer (using the static valueOf method), since it guarantees reference equality for small values ​​(you will need to check the documents to find out what the exact range is), which means no allocation.

+3
source share

It depends on your specific problem, but the cheapest construct in Scala or in any language should be one that does not exist at all ... (at least not at runtime)

Let me introduce Phantom Types , allowing the compiler to statically do the right thing, but wear out to nothing before they get into the JVM.

In a similar vein, you can easily distinguish two different typed objects without any marker fields. This is a good candidate for case object s, and it really goes well with pattern matching.

+13
source share

I'm not sure I get what you mean with the “cheapest type” ... In Java, the cheapest numerical type in terms of memory may be a byte, but from POV performance, Java is optimized for working with int. In fact, many (or most) languages ​​are optimized for working with int (even with a DBMS). Scala is a JVM language, so I would say it is better to use int.

UPDATE: if it is a “tagged / unchecked” suitable data type, I would use a boolean primitive.

if you type boolean into a Scala program, the type you really get is scala.Boolean. Or, if you print a float, you get scala.Float. when you compile your Scala code for Java bytecodes, however, Scala will compile these types for Java primitive types where it is possible to get the performance benefits of Java primitive types.

Nothing is Any, which is a kind of object in Java, and any object requires more memory than any primitive type.

And btw, using a logical marker, of course, it improves the readability of the code (and, therefore, supports, understands others). Even if there is something else that is more optimized, I would not do premature optimization and choose a logical one.

+4
source share

Since Scala runs on top of Java, I would suggest that the same answer would work in the Scala world. a Scala object of type Any is just a Java object.

In Java, I think that a primitive type like int, short or byte will be cheaper than Object, but these types can be wrapped / packaged in Scala. (Not 100% sure of that.)

Update

If for some reason it should be an object, not a primitive type, String may be better, since the strings are interned in the virtual machine. Thus, there will usually be only one instance of an object in the entire application.

+1
source share

Scala wraps primitive types when you invoke shell method calls that the primitive type does not implement. So, i.e. 1024 * 512 does not wrap, but 1024.toInt * 512 wraps 1024.

0
source share

All Articles