Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36530

Re: Generics and downcasts

From Marcel Mueller <news.5.maazl@spamgourmet.org>
Newsgroups comp.lang.java.programmer
Subject Re: Generics and downcasts
Date 2017-04-25 22:07 +0200
Organization MB-NET.NET for Open-News-Network e.V.
Message-ID <odoaaj$9q$1@gwaiyur.mb-net.net> (permalink)
References <generics-20170424231405@ram.dialup.fu-berlin.de>

Show all headers | View raw


On 25.04.17 00.23, Stefan Ram wrote:
> Cast-iron guarantee: the implicit casts added by the
> compilation of generics never fail.
>
>    This, of course, immediatly leads to the question:
>
>    Since the downcasts added during the compilation of
>    the above generic code can never fail and since they
>    add run-time costs, can't they be omitted?

It depends on your definition of "omitted".

Technically at machine instruction level cast expression are not 
required at all. Everything is an object at this level, no more no less.

But to get meaningful exceptions instead of undefined behavior and JVM 
crashes cast expressions evaluate to type checks. I.e
   x = (sometype)y;
is basically the same than
   if (!(a instanceof sometype))
     throw new ClassCastException(...);
   x = y;
except that the latter does not compile because of compiler 
restrictions. So a cast expression does not generate much executable 
code. It is just a conditional expression that is typically false.

>    Maybe the purpose of these downcasts is to add
>    additional safety for the case of some parts of a
>    program not using generics. (I.e., in the presence of
>    "unchecked" warning.)

Yes, this is an important issue.

>    A program might define a /private/ generic class and make
>    sure that /all/ accesses to this class use generics, yet it
>    still has to pay the price of those downcasts!

It has to pay for the concept of type erasure with compatibility to old 
code.

If you dislike that, choose another language. .NET for instance has 
strong typing at IL level and does not share this impact. (If you anyhow 
manage to get a wrong object type in your container you get really funny 
runtime exceptions.)
The presence of structured value types in .NET add further performance 
improvements if used well.

If this still does not fit to your needs use C++. This can generate code 
similarly efficient than machine code with almost all checks done at 
compile time.
But even in C++ type erasure is a very efficient concept. The bad news 
is that have have to do it yourself by using a non template base class. 
The latter does the work and the strongly typed subclass just adds type 
safety but does not generate any executable code.

But in practice these differences are not relevant in most use cases.

There are other pitfalls that are by far more expensive.
E.g. if you miss to declare a trivial Java method as final. This 
prevents the JIT from inlining the method body. This may add an overhead 
of an order of magnitude or even more. In C++ getter or delegation 
methods may not generate even one machine instruction. In managed 
languages like Java there is always a small overhead if you want the 
stack trace of exceptions to be reliable. But this is by far less than a 
virtual method call.
And even this is negligible compared to an additional invocation of the 
new operator.

Basically type erasure reduces the working set size of your code. This 
can improve cache efficiency which may be more relevant than the impact 
by type checks.

If you worry about performance have a look at you algorithms and data 
structures first. Only if you can't squeeze out more performance at this 
level dig down to lower levels. From my experience optimizing algorithms 
and data structures often has a gain of orders of magnitudes while doing 
optimizations at code level only adds a small factor.


Marcel

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: Generics and downcasts Marcel Mueller <news.5.maazl@spamgourmet.org> - 2017-04-25 22:07 +0200
  Re: Generics and downcasts Arne Vajhøj <arne@vajhoej.dk> - 2017-04-25 16:20 -0400
    Re: Generics and downcasts Eric Douglas <e.d.programmer@gmail.com> - 2017-04-25 13:35 -0700
      Re: Generics and downcasts Arne Vajhøj <arne@vajhoej.dk> - 2017-04-25 19:54 -0400
    Re: Generics and downcasts Marcel Mueller <news.5.maazl@spamgourmet.org> - 2017-04-25 22:40 +0200
      Re: Generics and downcasts Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-04-25 18:13 -0400
      Re: Generics and downcasts Arne Vajhøj <arne@vajhoej.dk> - 2017-04-25 19:53 -0400

csiph-web