Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post2.news.xs4all.nl!newszilla.xs4all.nl!not-for-mail Date: Mon, 04 Jul 2011 17:43:30 +0200 From: jaap User-Agent: Mozilla/5.0 (X11; U; Linux i686; nl-NL; rv:1.9.2.17) Gecko/20110424 Lightning/1.0b2 Thunderbird/3.1.10 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Eclipse generated hashCode() and Compiler Efficiency References: <8ee12802-2786-472a-815f-94dc14112d7d@c29g2000yqd.googlegroups.com> <906ef757-d407-45a3-85be-da345e4c51a2@d19g2000prh.googlegroups.com> In-Reply-To: <906ef757-d407-45a3-85be-da345e4c51a2@d19g2000prh.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Lines: 77 Message-ID: <4e11dfa2$0$25133$e4fe514c@dreader25.news.xs4all.nl> NNTP-Posting-Host: 83.160.252.3 X-Trace: 1309794210 dreader25.news.xs4all.nl 25133 [::ffff:83.160.252.3]:37268 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5852 On 04-07-11 13:13, schreef lewbloch: > On Jul 4, 4:07 am, Robert Klemme 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 Don't forget the JIT compiler, she makes your code beautiful to run (and awkward to read, but you never do). Jaap