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


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

Re: Eclipse generated hashCode() and Compiler Efficiency

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!d19g2000prh.googlegroups.com!not-for-mail
From lewbloch <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Eclipse generated hashCode() and Compiler Efficiency
Date Mon, 4 Jul 2011 04:13:59 -0700 (PDT)
Organization http://groups.google.com
Lines 72
Message-ID <906ef757-d407-45a3-85be-da345e4c51a2@d19g2000prh.googlegroups.com> (permalink)
References <8ee12802-2786-472a-815f-94dc14112d7d@c29g2000yqd.googlegroups.com>
NNTP-Posting-Host 108.89.33.208
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1309778414 7056 127.0.0.1 (4 Jul 2011 11:20:14 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Mon, 4 Jul 2011 11:20:14 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info d19g2000prh.googlegroups.com; posting-host=108.89.33.208; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-Header-Order HNKRUAELSC
X-HTTP-UserAgent Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30,gzip(gfe)
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5848

Show key headers only | View raw


On Jul 4, 4:07 am, Robert Klemme <shortcut...@googlemail.com> wrote:
> Hi,
>
> Eclipse generates hashCode() methods like this for a class with a
> single int field "i":
>
>   @Override
>   public int hashCode() {
>     final int prime = 31;
>     int result = 1;
>     result = prime * result + i;
>     return result;
>   }
>
> I always found it weird to define the constant "prime" at method
> level.  I wanted to know what happens and disassembled with
>
> $ javap -c -verbose -private -classpath classes bytecode.T1
>
> This is what I got for the method:
>
> public int hashCode();
>   Code:
>    Stack=2, Locals=3, Args_size=1
>    0:   bipush  31
>    2:   istore_1
>    3:   iconst_1
>    4:   istore_2
>    5:   bipush  31
>    7:   iload_2
>    8:   imul
>    9:   aload_0
>    10:  getfield        #18; //Field i:I
>    13:  iadd
>    14:  istore_2
>    15:  iload_2
>    16:  ireturn
>   LineNumberTable:
>    line 9: 0
>    line 10: 3
>    line 11: 5
>    line 12: 15
>
>   LocalVariableTable:
>    Start  Length  Slot  Name   Signature
>    0      17      0    this       Lbytecode/T1;
>    3      14      1    prime       I
>    5      12      2    result       I
>
> (This was from Eclipse compiler but Sun's javac yields the same albeit
> with a different ordering of constants.)
>
> Apart from other inefficiencies the weird thing is that in lines 0 and
> 2 the constant 31 is stored in local variable 1 ("prime") although it
> is never read again.  Is this an optimization they didn't bother to do
> (or left for the JVM) or is there any reason why the local variable
> must be there?  So far I could not find any hints in JLS or JVM spec.
>
> You can find the full examples here - including a variant where
> "prime" is a constant on class level:https://gist.github.com/1063153
>
> Instruction reference: http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc....
>
> Kind regards
>

Eclipse's compiler has an option whether to retain or eliminate unused
local variables.  How do you have that switch set?

--
Lew

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


Thread

Eclipse generated hashCode() and Compiler Efficiency Robert Klemme <shortcutter@googlemail.com> - 2011-07-04 04:07 -0700
  Re: Eclipse generated hashCode() and Compiler Efficiency lewbloch <lewbloch@gmail.com> - 2011-07-04 04:13 -0700
    Re: Eclipse generated hashCode() and Compiler Efficiency jaap <niemand@meel.homelinux.net> - 2011-07-04 17:43 +0200
    Re: Eclipse generated hashCode() and Compiler Efficiency Robert Klemme <shortcutter@googlemail.com> - 2011-07-06 00:20 -0700
  Re: Eclipse generated hashCode() and Compiler Efficiency Roedy Green <see_website@mindprod.com.invalid> - 2011-07-04 08:54 -0700

csiph-web