- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Simple constant values
Sun, 2010-12-19, 15:46
2010/12/19 Piotr Kołaczkowski <P.Kolaczkowski@elka.pw.edu.pl>
W dniu 2010-12-19 15:13, David Pollak pisze:class X {
private final def const = 1234
}
HotSpot will inline the call and you'll wind up with the efficiency (performance and space) you're looking for.
The call yes, but I doubt it will get rid of the space taken by such field. Have you got any evidence?
class X {
private final def const = 1234
def getConst = const
}
class Y {
private final val const = 1234
def getConst = const
}
Look at the bytecode generated (javap -c -verbose -private X). There's no slot in the class for the constant.
BTW: Are you sure the client HotSpot does inlining at all? I'm curious why I usually see a 2x-3x slowdown compared to the server compiler. I mean, why so much a difference.
I think that answers your question about inlining. HotSpot running in "server" mode does much more aggressive inlining. The issue is that in class Y, the slot is taken up, but the Scala compiler inlines the call to a ldc (load constant), where in class X, the compiler does the method call which needs to be inlined by HotSpot and HotSpot in server mode does much more aggressive inlining.
Regards,
Piotr
2010/12/19 Piotr Kołaczkowski <pkolaczk@elka.pw.edu.pl>
Hi,
If I write such simple code:
class X {
private final val const = 1234
}
Does the const take up space in the objects of the class X or not?
If it does, how to get such behaviour like in good old C with using #defines - simple constants that are inlined?
Regards,
Piotr Kołaczkowski
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics
-- Piotr Kołaczkowski Instytut Informatyki, Politechnika Warszawska Nowowiejska 15/19, 00-665 Warszawa e-mail: pkolaczk@ii.pw.edu.pl www: http://home.elka.pw.edu.pl/~pkolaczk/
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics