summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authorJean Boussier <[email protected]>2025-02-13 11:08:38 +0100
committerJean Boussier <[email protected]>2025-02-13 11:38:02 +0100
commit9826047f01230780c0e64072fa03a7a4aecba63c (patch)
tree45a164a6b91f2a369ac89b336a8e95e36baa0ad8 /array.c
parent4a67ef09ccd703047552b740431cfe15e32451f4 (diff)
Array#sort_by! return early if sorting is useless
`Array#sort!` does that check, but `#sort_by!` always tries to sort, which is wasteful.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/12741
Diffstat (limited to 'array.c')
-rw-r--r--array.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/array.c b/array.c
index e799c2f4e5..7ba887dc89 100644
--- a/array.c
+++ b/array.c
@@ -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;
}