dolphinsmalltalk / Dolphin

Dolphin Smalltalk Core Image
MIT License
301 stars 58 forks source link

Improve ScaledDecimal comparison performance #1119

Closed rko281 closed 3 years ago

rko281 commented 3 years ago

Comparison of ScaledDecimals is slow comparative to other numeric types, e.g.

"Integer = Integer"
Time microsecondsToRun: [100000 timesRepeat: [1=1]]. "1,859"
Time microsecondsToRun: [100000 timesRepeat: [1=2]]. "1,831"

"Float = Float"
Time microsecondsToRun: [100000 timesRepeat: [1.0=1.0]]. "3,060"
Time microsecondsToRun: [100000 timesRepeat: [1.0=2.0]]. "3,121"

"ScaledDecimal = ScaledDecimal"
Time microsecondsToRun: [100000 timesRepeat: [1.0s2=1.0s2]]. "80,338"
Time microsecondsToRun: [100000 timesRepeat: [1.0s2=2.0s2]]. "76,505"

"Integer = Float"
Time microsecondsToRun: [100000 timesRepeat: [1=1.0]]. "31,170"
Time microsecondsToRun: [100000 timesRepeat: [1=2.0]]. "31,772"

"Integer = ScaledDecimal"
Time microsecondsToRun: [100000 timesRepeat: [1=1.0s2]]. "165,616"
Time microsecondsToRun: [100000 timesRepeat: [1=2.0s2]]. "176,070"

The slowness looks to be largely down to the creation of an intermediate ScaledDecimal in the subtraction operation in ArithmeticValue>>#= .

Performance can be improved (5x or greater) by implementing equalToScaledDecimal: in the ArithmeticValue hierarchy (similar to equalToInteger: etc.) and comparing the ScaledDecimal by its Fraction.

I can submit a pull request for this if acceptable.

blairmcg commented 3 years ago

Sounds like a good idea to me John. Not terribly hard to test either, just a number of combinations/cases.