RFR: 8322292: Rearrange comparison of fields in Record.equals() [v4]
ExE Boss
duke at openjdk.org
Thu Dec 21 14:57:48 UTC 2023
On Thu, 21 Dec 2023 14:47:43 GMT, ExE Boss <duke at openjdk.org> wrote:
>> Sergey Tsypanov has updated the pull request incrementally with one additional commit since the last revision:
>>
>> 8322292: Tiny improvement
>
> src/java.base/share/classes/java/lang/runtime/ObjectMethods.java line 227:
>
>> 225: }
>> 226: return rt1.isPrimitive() || rt1.isEnum() || rt1.isArray() ? 1 : Iterable.class.isAssignableFrom(rt1) ? -1 : 0;
>> 227: });
>
> The way this comparator is currently implemented, the `signum(c.compare(mh1, mh2)) == -signum(c.compare(mh2, mh1))` assertion won’t hold, which will cause the sorting to be unstable.
This should ensure stable ordering:
Suggestion:
Arrays.sort(equalsGetters, (mh1, mh2) -> {
var rt1 = mh1.type().returnType();
var rt2 = mh2.type().returnType();
return Integer.compare(
rt1.isPrimitive() || rt1.isEnum() || rt1.isArray() ? 1 : Iterable.class.isAssignableFrom(rt1) ? -1 : 0,
rt2.isPrimitive() || rt2.isEnum() || rt2.isArray() ? 1 : Iterable.class.isAssignableFrom(rt2) ? -1 : 0
);
});
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/17143#discussion_r1434173126
More information about the core-libs-dev
mailing list