harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Beyer" <ndbe...@apache.org>
Subject Re: svn commit: r676866 - /harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/
Date Wed, 16 Jul 2008 02:00:23 GMT
I've noticed a bunch of patches lately, like this one, that have inline
fields assignments that duplicate the default value.

private boolean hashcodeComputed = false;

Besides being redundant and unnecessary, it's inconsistent when coupled with
other field declarations that don't assign anything inline.

private boolean hashcodeComputed = false;
private int cachedHashCode;

Save some typing, save some bits and save the unnecessary steps in my
debugger -- don't assign default values to fields.

Thanks.

-Nathan

On Tue, Jul 15, 2008 at 6:01 AM, <sjanuary@apache.org> wrote:

> Author: sjanuary
> Date: Tue Jul 15 04:01:03 2008
> New Revision: 676866
>
> URL: http://svn.apache.org/viewvc?rev=676866&view=rev
> Log:
> Apply patch for HARMONY-5907 ([classlib][pack200]CPUTF8.hashCode() is slow)
>
> Modified:
>
>  harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/ByteCode.java
>
>  harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPClass.java
>
>  harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPFieldRef.java
>
>  harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPInterfaceMethodRef.java
>
>  harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPMethod.java
>
>  harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPMethodRef.java
>
>  harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPNameAndType.java
>
>  harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPString.java
>
>  harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPUTF8.java
>
>  harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/ClassFileEntry.java
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/ByteCode.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/ByteCode.java?rev=676866&r1=676865&r2=676866&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/ByteCode.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/ByteCode.java
> Tue Jul 15 04:01:03 2008
> @@ -104,13 +104,17 @@
>         return getByteCodeForm().getOperandType();
>     }
>
> +    private boolean hashcodeComputed = false;
> +    private int cachedHashCode;
> +
> +    private void generateHashCode() {
> +        cachedHashCode = objectHashCode();
> +    }
> +
>     public int hashCode() {
> -        final int prime = 41;
> -        int result = 1;
> -        result = prime * result + getName().hashCode() +
> getByteCodeForm().hashCode();
> -        // Don't forget to take the operands = rewrite into account
> -        result += (prime * rewrite.hashCode());
> -        return result;
> +        if (!hashcodeComputed)
> +            generateHashCode();
> +        return cachedHashCode;
>     }
>
>     /*
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPClass.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPClass.java?rev=676866&r1=676865&r2=676866&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPClass.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPClass.java
> Tue Jul 15 04:01:03 2008
> @@ -61,8 +61,18 @@
>         return new ClassFileEntry[] { utf8, };
>     }
>
> +    private boolean hashcodeComputed = false;
> +    private int cachedHashCode;
> +
> +    private void generateHashCode() {
> +        hashcodeComputed = true;
> +        cachedHashCode = utf8.hashCode();
> +    }
> +
>     public int hashCode() {
> -        return utf8.hashCode();
> +        if (!hashcodeComputed)
> +            generateHashCode();
> +        return cachedHashCode;
>     }
>
>     protected void resolve(ClassConstantPool pool) {
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPFieldRef.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPFieldRef.java?rev=676866&r1=676865&r2=676866&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPFieldRef.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPFieldRef.java
> Tue Jul 15 04:01:03 2008
> @@ -54,14 +54,24 @@
>         return "FieldRef: " + className + "#" + nameAndType;
>     }
>
> -    public int hashCode() {
> +    private boolean hashcodeComputed = false;
> +    private int cachedHashCode;
> +
> +    private void generateHashCode() {
> +        hashcodeComputed = true;
>         final int PRIME = 31;
>         int result = 1;
>         result = PRIME * result
>                 + ((className == null) ? 0 : className.hashCode());
>         result = PRIME * result
>                 + ((nameAndType == null) ? 0 : nameAndType.hashCode());
> -        return result;
> +        cachedHashCode = result;
> +    }
> +
> +    public int hashCode() {
> +        if (!hashcodeComputed)
> +            generateHashCode();
> +        return cachedHashCode;
>     }
>
>     public boolean equals(Object obj) {
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPInterfaceMethodRef.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPInterfaceMethodRef.java?rev=676866&r1=676865&r2=676866&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPInterfaceMethodRef.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPInterfaceMethodRef.java
> Tue Jul 15 04:01:03 2008
> @@ -32,4 +32,23 @@
>     public int invokeInterfaceCount() {
>         return nameAndType.invokeInterfaceCount();
>     }
> +
> +    private boolean hashcodeComputed = false;
> +    private int cachedHashCode;
> +
> +    private void generateHashCode() {
> +        hashcodeComputed = true;
> +        final int PRIME = 31;
> +        int result = 1;
> +        result = PRIME * result + className.hashCode();
> +        result = PRIME * result + nameAndType.hashCode();
> +        cachedHashCode = result;
> +    }
> +
> +    public int hashCode() {
> +        if (!hashcodeComputed)
> +            generateHashCode();
> +        return cachedHashCode;
> +    }
> +
>  }
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPMethod.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPMethod.java?rev=676866&r1=676865&r2=676866&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPMethod.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPMethod.java
> Tue Jul 15 04:01:03 2008
> @@ -31,4 +31,22 @@
>         return "Method: " + name + "(" + descriptor + ")";
>     }
>
> +    private boolean hashcodeComputed = false;
> +    private int cachedHashCode;
> +
> +    private void generateHashCode() {
> +        hashcodeComputed = true;
> +        final int PRIME = 31;
> +        int result = 1;
> +        result = PRIME * result + name.hashCode();
> +        result = PRIME * result + descriptor.hashCode();
> +        cachedHashCode = result;
> +    }
> +
> +    public int hashCode() {
> +        if (!hashcodeComputed)
> +            generateHashCode();
> +        return cachedHashCode;
> +    }
> +
>  }
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPMethodRef.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPMethodRef.java?rev=676866&r1=676865&r2=676866&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPMethodRef.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPMethodRef.java
> Tue Jul 15 04:01:03 2008
> @@ -29,4 +29,23 @@
>         return new ClassFileEntry[] { className, nameAndType };
>     }
>
> +
> +    private boolean hashcodeComputed = false;
> +    private int cachedHashCode;
> +
> +    private void generateHashCode() {
> +        hashcodeComputed = true;
> +        final int PRIME = 31;
> +        int result = 1;
> +        result = PRIME * result + className.hashCode();
> +        result = PRIME * result + nameAndType.hashCode();
> +        cachedHashCode = result;
> +    }
> +
> +    public int hashCode() {
> +        if (!hashcodeComputed)
> +            generateHashCode();
> +        return cachedHashCode;
> +    }
> +
>  }
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPNameAndType.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPNameAndType.java?rev=676866&r1=676865&r2=676866&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPNameAndType.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPNameAndType.java
> Tue Jul 15 04:01:03 2008
> @@ -76,12 +76,22 @@
>         return "NameAndType: " + name + "(" + descriptor + ")";
>     }
>
> -    public int hashCode() {
> +    private boolean hashcodeComputed = false;
> +    private int cachedHashCode;
> +
> +    private void generateHashCode() {
> +        hashcodeComputed = true;
>         final int PRIME = 31;
>         int result = 1;
>         result = PRIME * result + descriptor.hashCode();
>         result = PRIME * result + name.hashCode();
> -        return result;
> +        cachedHashCode = result;
> +    }
> +
> +    public int hashCode() {
> +        if (!hashcodeComputed)
> +            generateHashCode();
> +        return cachedHashCode;
>     }
>
>     public boolean equals(Object obj) {
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPString.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPString.java?rev=676866&r1=676865&r2=676866&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPString.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPString.java
> Tue Jul 15 04:01:03 2008
> @@ -53,4 +53,21 @@
>     protected ClassFileEntry[] getNestedClassFileEntries() {
>         return new ClassFileEntry[] { name };
>     }
> +
> +    private boolean hashcodeComputed = false;
> +    private int cachedHashCode;
> +
> +    private void generateHashCode() {
> +        hashcodeComputed = true;
> +        final int PRIME = 31;
> +        int result = 1;
> +        result = PRIME * result + name.hashCode();
> +        cachedHashCode = result;
> +    }
> +
> +    public int hashCode() {
> +        if (!hashcodeComputed)
> +            generateHashCode();
> +        return cachedHashCode;
> +    }
>  }
> \ No newline at end of file
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPUTF8.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPUTF8.java?rev=676866&r1=676865&r2=676866&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPUTF8.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/CPUTF8.java
> Tue Jul 15 04:01:03 2008
> @@ -57,9 +57,19 @@
>         return utf8.equals(other.utf8);
>     }
>
> -    public int hashCode() {
> +    private boolean hashcodeComputed = false;
> +    private int cachedHashCode;
> +
> +    private void generateHashCode() {
> +        hashcodeComputed = true;
>         final int PRIME = 31;
> -        return PRIME + utf8.hashCode();
> +        cachedHashCode = PRIME + utf8.hashCode();
> +    }
> +
> +    public int hashCode() {
> +        if (!hashcodeComputed)
> +            generateHashCode();
> +        return cachedHashCode;
>     }
>
>     public String toString() {
>
> Modified:
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/ClassFileEntry.java
> URL:
> http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/ClassFileEntry.java?rev=676866&r1=676865&r2=676866&view=diff
>
> ==============================================================================
> ---
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/ClassFileEntry.java
> (original)
> +++
> harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/unpack200/bytecode/ClassFileEntry.java
> Tue Jul 15 04:01:03 2008
> @@ -50,6 +50,10 @@
>         return resolved;
>     }
>
> +    protected int objectHashCode() {
> +        return super.hashCode();
> +    }
> +
>     public abstract String toString();
>
>     public final void write(DataOutputStream dos) throws IOException {
>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message