repo.or.cz
/
ruby.git
/
commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
log
|
graphiclog1
|
graphiclog2
|
commit
|
commitdiff
|
tree
|
refs
|
edit
|
fork
raw
|
patch
|
inline
|
side by side
(parent:
4a67ef0
)
Array#sort_by! return early if sorting is useless
author
Jean Boussier
<
[email protected]
>
Thu, 13 Feb 2025 10:08:38 +0000
(13 11:08 +0100)
committer
Jean Boussier
<
[email protected]
>
Thu, 13 Feb 2025 10:38:02 +0000
(13 11:38 +0100)
`Array#sort!` does that check, but `#sort_by!` always tries to
sort, which is wasteful.
array.c
patch
|
blob
|
blame
|
history
diff --git
a/array.c
b/array.c
index
e799c2f
..
7ba887d
100644
(file)
--- 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;
}