ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
@ 2024-01-30  3:26 omerby (Omer Ben Yosef) via ruby-core
  2024-01-30  3:39 ` [ruby-core:116495] " nobu (Nobuyoshi Nakada) via ruby-core
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: omerby (Omer Ben Yosef) via ruby-core @ 2024-01-30  3:26 UTC (permalink / raw
  To: ruby-core; +Cc: omerby (Omer Ben Yosef)

Issue #20226 has been reported by omerby (Omer Ben Yosef).

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116495] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
@ 2024-01-30  3:39 ` nobu (Nobuyoshi Nakada) via ruby-core
  2024-01-30 10:15 ` [ruby-core:116496] " bkDJ (Djilani Kebaili) via ruby-core
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-01-30  3:39 UTC (permalink / raw
  To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)

Issue #20226 has been updated by nobu (Nobuyoshi Nakada).


`sort` family methods do not guarantee stable sort.
That means the result order of the same value elements is not affected by the original order of those elements.

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106515

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116496] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
  2024-01-30  3:39 ` [ruby-core:116495] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2024-01-30 10:15 ` bkDJ (Djilani Kebaili) via ruby-core
  2024-01-30 10:17 ` [ruby-core:116497] " Eregon (Benoit Daloze) via ruby-core
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: bkDJ (Djilani Kebaili) via ruby-core @ 2024-01-30 10:15 UTC (permalink / raw
  To: ruby-core; +Cc: bkDJ (Djilani Kebaili)

Issue #20226 has been updated by bkDJ (Djilani Kebaili).


You can force stability by using the original index as a tiebreaker.

```ruby
arr = [*Array.new(15,1), -0.02, 0.02]
puts "Untable: #{arr.sort_by(&:abs)[0..1]}"
puts "Stable: #{arr.sort_by.with_index { [_1.abs, _2] }[0..1]}"
```

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106516

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116497] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
  2024-01-30  3:39 ` [ruby-core:116495] " nobu (Nobuyoshi Nakada) via ruby-core
  2024-01-30 10:15 ` [ruby-core:116496] " bkDJ (Djilani Kebaili) via ruby-core
@ 2024-01-30 10:17 ` Eregon (Benoit Daloze) via ruby-core
  2024-01-30 10:22 ` [ruby-core:116498] " ufuk (Ufuk Kayserilioglu) via ruby-core
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-01-30 10:17 UTC (permalink / raw
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20226 has been updated by Eregon (Benoit Daloze).


When was `sort`/`sort_by` changed to no longer be stable? I don't remind any ticket about it or NEWS entry.
IMO it's a pretty big change to make it non-stable when it has been stable for so long.
Especially for a high-level language like Ruby, where I'd think most users do not expect such low-level & error-prone issues to arise.

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106517

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116498] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
                   ` (2 preceding siblings ...)
  2024-01-30 10:17 ` [ruby-core:116497] " Eregon (Benoit Daloze) via ruby-core
@ 2024-01-30 10:22 ` ufuk (Ufuk Kayserilioglu) via ruby-core
  2024-01-30 10:25 ` [ruby-core:116499] " ufuk (Ufuk Kayserilioglu) via ruby-core
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ufuk (Ufuk Kayserilioglu) via ruby-core @ 2024-01-30 10:22 UTC (permalink / raw
  To: ruby-core; +Cc: ufuk (Ufuk Kayserilioglu)

Issue #20226 has been updated by ufuk (Ufuk Kayserilioglu).


Sorting in Ruby never was stable. There were discussions to make it stable, but Matz was concerned about the speed impact. I don't think this particular case is a regression, the behaviour of sort depends on the number of elements, and other factors since different sorting algorithms could be used for different sized lists.

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106518

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116499] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
                   ` (3 preceding siblings ...)
  2024-01-30 10:22 ` [ruby-core:116498] " ufuk (Ufuk Kayserilioglu) via ruby-core
@ 2024-01-30 10:25 ` ufuk (Ufuk Kayserilioglu) via ruby-core
  2024-01-30 10:44 ` [ruby-core:116500] " Eregon (Benoit Daloze) via ruby-core
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ufuk (Ufuk Kayserilioglu) via ruby-core @ 2024-01-30 10:25 UTC (permalink / raw
  To: ruby-core; +Cc: ufuk (Ufuk Kayserilioglu)

Issue #20226 has been updated by ufuk (Ufuk Kayserilioglu).


Here is the previous discussion from 15 years ago: https://bugs.ruby-lang.org/issues/1089

It might be about time to revisit the performance impact of a stable sort, imo, but that's not the discussion on this ticket.

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106519

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116500] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
                   ` (4 preceding siblings ...)
  2024-01-30 10:25 ` [ruby-core:116499] " ufuk (Ufuk Kayserilioglu) via ruby-core
@ 2024-01-30 10:44 ` Eregon (Benoit Daloze) via ruby-core
  2024-01-30 10:58 ` [ruby-core:116501] " ufuk (Ufuk Kayserilioglu) via ruby-core
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-01-30 10:44 UTC (permalink / raw
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20226 has been updated by Eregon (Benoit Daloze).


> Sorting in Ruby never was stable

My mistake, like Roger Pack in https://github.com/crystal-lang/crystal/issues/2350#issuecomment-550188746, my experience is sort looks stable (on Linux).
And sort is stable on TruffleRuby since it uses mergesort (currently at least).

So what changed between 3.2 and 3.3?
I can reproduce the OP's behavior on linux-amd64.

Maybe it's `sort_by`-specific? And maybe `sort_by` was stable until 3.2?

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106520

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116501] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
                   ` (5 preceding siblings ...)
  2024-01-30 10:44 ` [ruby-core:116500] " Eregon (Benoit Daloze) via ruby-core
@ 2024-01-30 10:58 ` ufuk (Ufuk Kayserilioglu) via ruby-core
  2024-01-30 13:34 ` [ruby-core:116506] " johnnyshields (Johnny Shields) via ruby-core
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ufuk (Ufuk Kayserilioglu) via ruby-core @ 2024-01-30 10:58 UTC (permalink / raw
  To: ruby-core; +Cc: ufuk (Ufuk Kayserilioglu)

Issue #20226 has been updated by ufuk (Ufuk Kayserilioglu).


`Enumerable#sort_by` was also documented to not be stable in 3.2 (and previous versions): https://ruby-doc.org/3.2.3/Enumerable.html#method-i-sort_by

What might have changed (this is a layman's guess since I don't know the internals of Ruby sorting that well) is which sort routine Ruby uses from the OS/libc by default, which might change the behaviour of the resulting sort. Since Ruby provides no guarantees of stable sorting, this is not a regression, despite what specific platform sort routines might provide.

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106521

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116506] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
                   ` (6 preceding siblings ...)
  2024-01-30 10:58 ` [ruby-core:116501] " ufuk (Ufuk Kayserilioglu) via ruby-core
@ 2024-01-30 13:34 ` johnnyshields (Johnny Shields) via ruby-core
  2024-01-30 13:48 ` [ruby-core:116507] " ufuk (Ufuk Kayserilioglu) via ruby-core
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: johnnyshields (Johnny Shields) via ruby-core @ 2024-01-30 13:34 UTC (permalink / raw
  To: ruby-core; +Cc: johnnyshields (Johnny Shields)

Issue #20226 has been updated by johnnyshields (Johnny Shields).


Whether or not Ruby's documentation made any explicit guarantees, I was under the strong impression (as per the Ruby behavior I've observed for 10 years) that Ruby did have a stable sort. Other `Array` operations such as `-` (minus), `uniq`, etc. *are* stable. I have a 500k LOC app, it's going to be difficult to review all my code for potential impacts.

If Ruby's `sort` was previously stable, it might be time to make that behavior official.

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106525

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116507] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
                   ` (7 preceding siblings ...)
  2024-01-30 13:34 ` [ruby-core:116506] " johnnyshields (Johnny Shields) via ruby-core
@ 2024-01-30 13:48 ` ufuk (Ufuk Kayserilioglu) via ruby-core
  2024-01-30 14:50 ` [ruby-core:116510] " johnnyshields (Johnny Shields) via ruby-core
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ufuk (Ufuk Kayserilioglu) via ruby-core @ 2024-01-30 13:48 UTC (permalink / raw
  To: ruby-core; +Cc: ufuk (Ufuk Kayserilioglu)

Issue #20226 has been updated by ufuk (Ufuk Kayserilioglu).


Regardless of any individual impressions, Ruby never made any stability guarantees for sorting. It was always explicit that sorting MAY BE unstable. This is because Ruby delegates the actual sorting to the efficient sorting routines provided by the platform in most cases, and those routines are free to handle sorting with whichever algorithm would be the most efficient for the input. This means that a sort that is always stable on macOS could show unstable results on Linux, for example, or vice versa.

So, regardless of what you are observing, Ruby makes no guarantees about the stability of sorting and that has always been the case.

I've linked to a conversation from 15 years ago about making the sort stable, which was rejected at the time. If you would like to propose a path forward for stable sorting guarantees in Ruby, please open a Feature ticket with the proposed implementation and performance implications.

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106526

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116510] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
                   ` (8 preceding siblings ...)
  2024-01-30 13:48 ` [ruby-core:116507] " ufuk (Ufuk Kayserilioglu) via ruby-core
@ 2024-01-30 14:50 ` johnnyshields (Johnny Shields) via ruby-core
  2024-01-30 15:21 ` [ruby-core:116511] " Eregon (Benoit Daloze) via ruby-core
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: johnnyshields (Johnny Shields) via ruby-core @ 2024-01-30 14:50 UTC (permalink / raw
  To: ruby-core; +Cc: johnnyshields (Johnny Shields)

Issue #20226 has been updated by johnnyshields (Johnny Shields).


If sort *was* stable for a long time (it does seem like it was, but I am not 100% sure), then despite Ruby's disclaimer, *many developers* will have come to rely on and expect that behavior, and consider this as "a behavior change that breaks their apps"--whether or not we strictly term it as a "regression bug".

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106529

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116511] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
                   ` (9 preceding siblings ...)
  2024-01-30 14:50 ` [ruby-core:116510] " johnnyshields (Johnny Shields) via ruby-core
@ 2024-01-30 15:21 ` Eregon (Benoit Daloze) via ruby-core
  2024-01-31 23:26 ` [ruby-core:116528] " alanwu (Alan Wu) via ruby-core
  2024-02-01  1:05 ` [ruby-core:116532] " alanwu (Alan Wu) via ruby-core
  12 siblings, 0 replies; 14+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-01-30 15:21 UTC (permalink / raw
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20226 has been updated by Eregon (Benoit Daloze).


> many developers will have come to rely on and expect that behavior,

I think that's a fair point, similar to e.g Set insertion ordering, even though that's not documented I'm pretty sure some Ruby code relies on it because it holds since a very long time.

The key seems to be whether sort was in practice stable on some platform/OS (I don't know for sure).

It'd be interesting to find out what exactly changed for 3.3.0.

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106530

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116528] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
                   ` (10 preceding siblings ...)
  2024-01-30 15:21 ` [ruby-core:116511] " Eregon (Benoit Daloze) via ruby-core
@ 2024-01-31 23:26 ` alanwu (Alan Wu) via ruby-core
  2024-02-01  1:05 ` [ruby-core:116532] " alanwu (Alan Wu) via ruby-core
  12 siblings, 0 replies; 14+ messages in thread
From: alanwu (Alan Wu) via ruby-core @ 2024-01-31 23:26 UTC (permalink / raw
  To: ruby-core; +Cc: alanwu (Alan Wu)

Issue #20226 has been updated by alanwu (Alan Wu).


Seems like maybe it was changed by #19643 for `sort_by`. But in any case, it was never stable since it was using the platform's unstable `qsort_r`. On macOS, I get `[0.0, 0.0, 0.02, 0.02, -0.02, ...]` even on 3.2.2. Glibc has [warned about](https://sourceware.org/glibc/manual/2.22/html_node/Array-Sort-Function.html) qsort being unstable for a long time, too.

----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106546

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [ruby-core:116532] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions
  2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
                   ` (11 preceding siblings ...)
  2024-01-31 23:26 ` [ruby-core:116528] " alanwu (Alan Wu) via ruby-core
@ 2024-02-01  1:05 ` alanwu (Alan Wu) via ruby-core
  12 siblings, 0 replies; 14+ messages in thread
From: alanwu (Alan Wu) via ruby-core @ 2024-02-01  1:05 UTC (permalink / raw
  To: ruby-core; +Cc: alanwu (Alan Wu)

Issue #20226 has been updated by alanwu (Alan Wu).


> The key seems to be whether sort was in practice stable on some platform/OS

Ah, until recently, glibc's `qsort` was actually a stable merge sort most of the time, which is why Ruby's sort felt stable on Linux.

https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=03bf8357e8291857a435afcc3048e0b697b6cc04
https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/thread/GBTHE3V6AYHCMBI5BI2BVTCAYOA4HFFX/



----------------------------------------
Bug #20226: Inconsistent Sort results on 3.3.0 compared to previous versions
https://bugs.ruby-lang.org/issues/20226#change-106550

* Author: omerby (Omer Ben Yosef)
* Status: Open
* Priority: Normal
* ruby -v: 3.3.0
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Try this code block:
```
[-0.9, -0.88, -0.85, -0.83, -0.81, -0.79, -0.77, -0.75, -0.73, -0.71, -0.69, -0.67, -0.65, -0.63, -0.6, -0.58, -0.56, -0.54, -0.52,
 -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.35, -0.33, -0.31, -0.29, -0.27, -0.25, -0.23, -0.21, -0.19, -0.17, -0.15, -0.13,
 -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)
```
The end result should be the numbers absolute sorted, look at the last 5 numbers of this, the end result of them should be `[0.0, 0.0, -0.02, 0.02, 0.02...]` maintaining the original order, and this behavior is what we see on ruby 3.2.0, however on ruby 3.3.0 the end result will be `[0.0, 0.0, 0.02, 0.02, -0.02...]`
This is also inconsistent, as `[-0.02, 0.0, 0.02, 0.0, 0.02].sort_by(&:abs)` will actually provide the expected result.

Again, the main issue for us is the difference between 3.3.0 and previous versions of ruby.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-02-01  1:05 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-30  3:26 [ruby-core:116493] [Ruby master Bug#20226] Inconsistent Sort results on 3.3.0 compared to previous versions omerby (Omer Ben Yosef) via ruby-core
2024-01-30  3:39 ` [ruby-core:116495] " nobu (Nobuyoshi Nakada) via ruby-core
2024-01-30 10:15 ` [ruby-core:116496] " bkDJ (Djilani Kebaili) via ruby-core
2024-01-30 10:17 ` [ruby-core:116497] " Eregon (Benoit Daloze) via ruby-core
2024-01-30 10:22 ` [ruby-core:116498] " ufuk (Ufuk Kayserilioglu) via ruby-core
2024-01-30 10:25 ` [ruby-core:116499] " ufuk (Ufuk Kayserilioglu) via ruby-core
2024-01-30 10:44 ` [ruby-core:116500] " Eregon (Benoit Daloze) via ruby-core
2024-01-30 10:58 ` [ruby-core:116501] " ufuk (Ufuk Kayserilioglu) via ruby-core
2024-01-30 13:34 ` [ruby-core:116506] " johnnyshields (Johnny Shields) via ruby-core
2024-01-30 13:48 ` [ruby-core:116507] " ufuk (Ufuk Kayserilioglu) via ruby-core
2024-01-30 14:50 ` [ruby-core:116510] " johnnyshields (Johnny Shields) via ruby-core
2024-01-30 15:21 ` [ruby-core:116511] " Eregon (Benoit Daloze) via ruby-core
2024-01-31 23:26 ` [ruby-core:116528] " alanwu (Alan Wu) via ruby-core
2024-02-01  1:05 ` [ruby-core:116532] " alanwu (Alan Wu) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).