-
-
Notifications
You must be signed in to change notification settings - Fork 942
Description
Is there a reason that JRuby's RubyArray uses it own hand-rolled sort (in Qsort.java) instead of using Java's builtin Arrays.sort?
I'm looking at these files:
- https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/RubyArray.java#L3278
- https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/RubyArray.java#L3313
- https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/util/Qsort.java
The reason I'm asking: I encountered a bug in a piece of code. A class was overriding the <=> method, but due to a typo, it was doing this comparision:
def <=>(other); self.a <=> other.b; end
instead of
def <=>(other); self.a <=> other.a; end.
Obviously, that is a bug in the application. I didn't notice the bug because a == b most of the time. However, recently, there were cases where a != b and that's when the bug manifisted. And JRuby's sort got stuck in an infinite loop.
When I converted JRuby to use Arrays.sort instead of Qsort and tried it with the bad data, it threw an exception: "Comparison method violates its general contract!".
Also, there are other benefits of using Arrays.sort:
https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/TimSort.java#L29
Is there interest in converting RubyArray to use Arrays.sort?
Thanks!
-Sriram