转之http://www.scala-notes.org/2011/04/specializing-for-primitive-types/
One interesting feature that was added to Scala in version 2.8 is specialization, using the @specialized
annotation. First, a little background information.
Generics in Java and the JVM, and consequently also in Scala, are implemented by using type erasure. That means that if you have an instance of a generic class, for example List[String]
, then the compiler will throw away the information about the type argument, so that at runtime it will look like List[Object]
.
In Java, primitive types are treated very differently from reference types. One of the things that you cannot do in Java is use primitive types to fill in type parameters – for example, you cannot make a List<int>
. If you want to create a list that holds integers, you’ll have to use the wrapper class Integer
. A drawback of this approach is that you need to box each of your primitive int
s into an Integer
object that takes up a lot more memory. I’ve done some tests and found that on my JVM a double
only takes up 8 bytes, but a Double
object takes up 24 bytes. Also, the boxing and unboxing takes up processor time, which can become a serious bottleneck if you’re dealing with large collections of numbers.
In Scala, the distinction between primitive types and reference types is far less great than in Java, and you can use types like Int
directly for type arguments. But a List[Int]
is still converted to a List[Object]
at runtime because of type erasure and since primitive types are not subclasses of Object
on the JVM, Scala will still box the Int
s to objects of some wrapper class – which makes a List[Int]
in Scala just as inefficient as aList<Integer>
in Java.
The @specialized
annotation and compiler support are meant to get rid of this inefficiency. Iulian Dragos explains it very clearly in this video. If you create a generic class and you use the @specialized
annotation, for example like this:
1 |
class Container[ @ specialized(Int) T](value : T) {
|
the compiler will actually generate two versions of the class: the normal, generic one, in which the type parameter is erased, and a special subclass that uses the primitive type Int
, without the need to box or unbox the value. You can see this when you compile the code using the -print
option of the compiler:
01 |
jesper @ jesper-desktop : ~/Projects/boxing$ scalac -print Container.scala
|
02 |
[[syntax trees at end of cleanup]]
|
border-top-width: 0px !important; border-right-width: 0px !important; border-bottom-width: 0px !important; border-left-width: 0px !important; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; font-size: 12px !important; margin-top: 0px !important; margin-ri
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Foundations for Programming Languages is distinguished primarily by its inclusion of material on universal algebra and algebraic data types, imperative languages and Floyd-Hoare logic, and advanced ...
Business Analytics for Managers conveys ideas and concepts from both statistics and data mining with the goal of extracting knowledge from real business data and actionable insight for managers....
Now an independent consultant specializing in IT services and network security, he has worked with Unix technologies since 1979 as a user, developer, system administrator, and consultant. Glass and ...
In 2011, he moved to Cardiff, UK, for an assignment to migrate C# Business tier components to BizTalk Server for his client named Confused.com, a British price comparison website specializing in ...
Chris Bowen is Microsoft’s Developer Evangelist for the northeastern United States, specializing in development tools, platforms, and architectural best practices. Asoftware architect and engineer ...
Use both types of C# constants for efficiency and maintainability (see Item 2) Employ immutable data types to promote multicore processing (see Item 20) Minimize garbage collection, boxing, and ...
You are an expert in UX/UI design, coding languages, and website development tools specializing in web design consulting. You have helped many people before me to create comprehensive plans for ...
UMS TECHNOLOGIES PRIVATE LTD UMS Technologies Pvt Ltd was ... Recently, UMS Technologies Pvt Ltd has been specializing in the manufacturing of very high precision aerospace components, Engines for radio-
UMS TECHNOLOGIES PRIVATE LTD UMS Technologies Pvt Ltd was ... Recently, UMS Technologies Pvt Ltd has been specializing in the manufacturing of very high precision aerospace components, Engines for radio-
You are an expert in graphic design and SVG specializing in creating pictures and images for documents. You have helped many people before me to create images for documents. Your task is now to teach...
You are an expert in public speaking and elocution specializing in developing engaging material for presentation. You have helped many people before me to practice delivery of speeches with proper ...
Written by Dan Sullivan―a well-known author and software architect specializing in analytics, machine learning, and cloud computing―this invaluable study guide includes access to the Sybex ...
You are an expert in marketing and product positioning specializing in creating compelling content that highlights the benefits of a product or service for a target audience. You have helped many ...
Psyco,全称为"Python Specializing Compiler",是一款专为Python设计的优化编译器,它的目标是提高Python代码的运行速度,使Python程序能够接近甚至达到原生编译语言的执行效率。Psyco的工作原理是通过对Python字节...
You are an expert in logic and reasoning specializing in identifying logical errors. You have helped many people before me to identify logical errors for evidence-based feedback. Your task is now to ...