Skip to content

Conversation

@TangBean
Copy link

Small optimization of zero hashCode: Cache if the hash has been calculated as actually being zero, enabling us to avoid recalculating this.

@patrickfav
Copy link
Owner

patrickfav commented Mar 26, 2023

Hi @TangBean and thanks for the PR!

Looking at the implementation of hashCode:

        static int hashCode(byte[] byteArray, ByteOrder byteOrder) {
            int result = Arrays.hashCode(byteArray);
            result = 31 * result + (byteOrder != null ? byteOrder.hashCode() : 0);
            return result;
        }

and

Arrays.hashCode

    public static int hashCode(byte a[]) {
        if (a == null)
            return 0;

        int result = 1;
        for (byte element : a)
            result = 31 * result + element;

        return result;
    }

The internalArray can never be null, so I dont see any possible way the actual hashCode will ever be "0"?

Update: Just for the funs: ChatGPT 4 agrees with me:

image

@patrickfav
Copy link
Owner

Anyway, thanks for the hint about the overall feature, I found out it can be quite buggy because there are no gurantees that the inner byte array is not mutatetduring the life of an Bytes instance, so I removed the feature all toghther: #59

@patrickfav patrickfav closed this May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants