Mark, On 9/23/14 9:15 AM, Mark Thomas wrote: > On 23/09/2014 14:09, schu...@apache.org wrote: >> Author: schultz >> Date: Tue Sep 23 13:09:42 2014 >> New Revision: 1627000 >> >> URL: http://svn.apache.org/r1627000 >> Log: >> Micro optimization. > > What is the performance benefit of this change?
See below. >> Modified: >> tomcat/trunk/java/org/apache/tomcat/util/buf/HexUtils.java >> >> Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/HexUtils.java >> URL: >> http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/HexUtils.java?rev=1627000&r1=1626999&r2=1627000&view=diff >> ============================================================================== >> --- tomcat/trunk/java/org/apache/tomcat/util/buf/HexUtils.java (original) >> +++ tomcat/trunk/java/org/apache/tomcat/util/buf/HexUtils.java Tue Sep 23 >> 13:09:42 2014 >> @@ -75,10 +75,12 @@ public final class HexUtils { >> if (null == bytes) { >> return null; >> } >> + final char[] hex = HexUtils.hex; >> + final int length = bytes.length; >> >> - StringBuilder sb = new StringBuilder(bytes.length << 1); >> + StringBuilder sb = new StringBuilder(length << 1); >> >> - for(int i = 0; i < bytes.length; ++i) { >> + for(int i = 0; i < length; ++i) { bytes.length requires "aload" and "arraylength" opcodes while using a local variable uses a single "aload" operation. Since I'm going to use "length" instead of bytes.length, I may as well use it to construct the StringBuilder object. Compared to sb.append() this optimization is likely to be lost in the noise, but it is still a slight improvement. >> sb.append(hex[(bytes[i] & 0xf0) >> 4]) >> .append(hex[(bytes[i] & 0x0f)]) >> ; >> @@ -94,8 +96,9 @@ public final class HexUtils { >> } >> >> char[] inputChars = input.toCharArray(); >> - byte[] result = new byte[input.length() >> 1]; >> - for (int i = 0; i < result.length; i++) { >> + final int length = input.length() >> 1; >> + byte[] result = new byte[length]; >> + for (int i = 0; i < length; i++) { Same here: fewer opcodes per loop. >> result[i] = (byte) ((getDec(inputChars[2*i]) << 4) + >> getDec(inputChars[2*i + 1])); >> } >> return result; -chris
signature.asc
Description: OpenPGP digital signature