Skip to content

Comparing integers has unexpected overhead #11685

Open
@informarte

Description

@informarte

I am migrating a project from Scala 2.12.8 to 2.13.0. While investigating a performance degradation, I came across an issue that's not new to Scala 2.13: Comparing integers like in the following piece of code

    private final class EventPoint(val x: Int, val bbox: Rect2d, val isBBoxStart: Boolean)
    private object EventPointOrdering extends Ordering[EventPoint] {
        override def compare(p1: EventPoint, p2: EventPoint) = p1.x.compare(p2.x)
    }

has unexpected overhead. I use this ordering to sort an EventPoint array and found that the use of EventPointOrdering accounts for 10% of the runtime:

EventPointOrdering compare-2 13 0-opt

(I used VisualVM for profiling and the program was compiled with full optimization and inlining.)

This call stack is terrifying, isn't it? I think that RIchInt (and all the other boxes for primitive types) should override compare instead of relying on OrderedProxy.

I solved my performance issue by using java.lang.Integer.compare directly.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions