diff options
author | Jean Boussier <[email protected]> | 2025-02-13 11:08:38 +0100 |
---|---|---|
committer | Jean Boussier <[email protected]> | 2025-02-13 11:38:02 +0100 |
commit | 9826047f01230780c0e64072fa03a7a4aecba63c (patch) | |
tree | 45a164a6b91f2a369ac89b336a8e95e36baa0ad8 /array.c | |
parent | 4a67ef09ccd703047552b740431cfe15e32451f4 (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.c | 6 |
1 files changed, 4 insertions, 2 deletions
@@ -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; } |