Netflix / archaius

Library for configuration management API
Apache License 2.0
2.47k stars 486 forks source link

Add equality comparison for ArchaiusType #700

Closed gavinbunney closed 9 months ago

gavinbunney commented 9 months ago

This PR adds equality methods for the ArchaiusType class to ensure keyAndType references are reused correctly within the DefaultPropertyFactory.

The computeIfAbsent check for existing property references uses the type equality within keyAndType to see if a reference to a property already exists, otherwise it adds it to the property ref map.

This can cause an explosion of allocations where the same property key and type are supplied, but as the type itself is constructed on every fetch (new ArchaiusType), the default java object equals methods always return false, so a new reference is added to the property factory.

sullis commented 9 months ago

Suggested enhancement: use the equalsverifier library to confirm the behavior of the equals method.

Add the library to build.gradle:

testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.+'

And add a unit test:

package com.netflix.archaius.api;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;

public class EqualsVerificationTest {
    @Test
    public void verifyArchaiusType() {
        verifyEquals(ArchaiusType.class);
    }

    private static void verifyEquals(Class<?> clazz) {
        EqualsVerifier.forClass(clazz)
                .usingGetClass()
                .verify();
    }

}
rgallardo-netflix commented 9 months ago

@sullis, I like that idea but I think I'd rather do it separately as a bigger change adding verifications to every class with an equals method. I did test things out locally and the PR passes as it is now.