Changeset 18777 in josm for trunk/test/unit


Ignore:
Timestamp:
2023-07-20T22:21:29+02:00 (17 months ago)
Author:
taylor.smock
Message:

Reduce allocations during startup from Shape

Shape#hashCode made between 8 and 24 MB of allocations at startup. This was
reduced by moving the math out of Arrays#hash and the default List#hash methods.
This additionally reduced the allocations from the Shape#equals method.

As an implementation note, the Shape#hashCode method returns exactly the same
hashcode as it did prior to this change. This may change in the future.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/imagery/ShapeTest.java

    r18341 r18777  
    55import static org.junit.jupiter.api.Assertions.assertEquals;
    66
     7import java.awt.Polygon;
     8import java.util.ArrayList;
    79import java.util.Arrays;
     10import java.util.Objects;
    811
     12import nl.jqno.equalsverifier.EqualsVerifier;
    913import org.junit.jupiter.api.Test;
    1014import org.junit.jupiter.params.ParameterizedTest;
     
    5357                () -> assertEquals(coordinate, shape.getPoints().get(1).getLat()));
    5458    }
     59
     60    /**
     61     * Ensure that the hashcode is semantically the same as what it previously was
     62     */
     63    @ParameterizedTest
     64    @ValueSource(strings = {"47.1 11.1 47.2 11.2 47.3 11.3", "-47.1 -11.1 -47.2 -11.2 -47.3 -11.3"})
     65    void testHashCode(String shapeString) {
     66        final Shape shape = new Shape(shapeString, " ");
     67        assertEquals(Objects.hash(shape.getPoints()), shape.hashCode(),
     68                "The hashcode for shape should be the same as that for the point list (specific coord list)");
     69        assertEquals(Objects.hash(new ArrayList<>(shape.getPoints())), shape.hashCode(),
     70                "The hashcode for shape should be the same as that for the point list (non-specific)");
     71    }
     72
     73    @Test
     74    void testEqualsHashCodeContract() {
     75        EqualsVerifier.simple().forClass(Shape.class)
     76                .withNonnullFields("coords")
     77                .withPrefabValues(Polygon.class, new Polygon(), new Polygon(new int[] {1, 2, 3}, new int[]{4, 5, 6}, 3))
     78                .verify();
     79    }
    5580}
Note: See TracChangeset for help on using the changeset viewer.