Array#sort_by! return early if sorting is useless

`Array#sort!` does that check, but `#sort_by!` always tries to
sort, which is wasteful.
This commit is contained in:
Jean Boussier 2025-02-13 11:08:38 +01:00
parent 4a67ef09cc
commit 9826047f01
Notes: git 2025-02-13 10:38:19 +00:00

View File

@ -3607,8 +3607,10 @@ rb_ary_sort_by_bang(VALUE ary)
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_modify(ary);
sorted = rb_block_call(ary, rb_intern("sort_by"), 0, 0, sort_by_i, 0);
rb_ary_replace(ary, sorted);
if (RARRAY_LEN(ary) > 1) {
sorted = rb_block_call(ary, rb_intern("sort_by"), 0, 0, sort_by_i, 0);
rb_ary_replace(ary, sorted);
}
return ary;
}