- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Proliferation of Classes?
Tue, 2009-03-10, 15:06
Hi,
I'm learning Scala using the "Beginning Scala" beta
chapters, "Programming In Scala," various materials from the
scala-lang.org and other resources from the Web. While I really like
what I'm learning about Scala, and will without doubt be using it,
something that continues to bother me is the seeming proliferation of
classes needed to encapsulate all the functions and blocks that give
Scala code its higher-order functional power as well as its conciseness
and freedom from repetition.
Am I unaware of or overlooking some tricks the compiler can pull off to
mitigate this? Or am I simply misunderstanding how Scala code
translates to JVM bytecode? To what extent, if any, and how, if at all,
should one take this (putative) characteristic into account when
designing Scala software?
If this really is an issue, is it one that's likely to be addressed in
the next generation of JVMs?
Randall Schulz
Tue, 2009-03-10, 19:37
#2
Re: Proliferation of Classes?
On Tue, Mar 10, 2009 at 11:06 AM, Randall R Schulz <rschulz@sonic.net> wrote:
Hi,
<snip>
If this really is an issue, is it one that's likely to be addressed in
the next generation of JVMs?
I had the same concern when I started. But I haven't seen anything terribly bad in termos of size or performance. I the beginning I looked at all the generated classes, and was concerned if performance was going to be bad. I have since relaxed.
I think that while there are a lot of classes for my projects performance has been good. I've been following a tip I found on a Lisp book: "don't worry about consing until you have a real preformance problem". I think that if you final code is slow you can profile it and refactor the critical parts. But for the most part Scala and FP will help you produce clear code quicker.
I think the scala team puts a lot of effort into optimizing the code. So I would say whatever can be used in the JVM will be used.
Thomas
Hi,
On Tue, 2009-03-10 at 07:06 -0700, Randall R Schulz wrote:
> Am I unaware of or overlooking some tricks the compiler can pull off to
> mitigate this? Or am I simply misunderstanding how Scala code
> translates to JVM bytecode? To what extent, if any, and how, if at all,
> should one take this (putative) characteristic into account when
> designing Scala software?
It can have an effect on start-up time as well as application size. The
issue is not only that there are a large number of classes, but they
contain a lot of duplicated information too. For incremental
improvements, it's possible to do things like:
http://www.nabble.com/-scala---half-a-kb-of-bytecode-overhead-per-anonym...
There was another proposal from David MacIver that could reduce the
amount of duplication in the constant pool.
> If this really is an issue, is it one that's likely to be addressed in
> the next generation of JVMs?
Scala could, in theory, use the method handles introduced in JDK7 for
anonymous functions. I don't know if there are plans to do so.
Note that pack200 does a great job when it comes to compressing Scala
binaries given the amount of duplication that exists.
Best,
Ismael