Numberick icon indicating copy to clipboard operation
Numberick copied to clipboard

Slow division on iOS 15.0 vs 16.4

Open oscbyspro opened this issue 2 years ago • 4 comments

I think this is a low-priority issue, but still (#58):

The difference appears to be between iOS 15 and iOS 16.4:

// Swift 5.8, iPhone 13 Pro, iOS 16.4 (0.027 seconds, 0.019 seconds)

Test Case '-[NBKDoubleWidthKitBenchmarks.NBKDoubleWidthBenchmarksOnDivisionAsUInt256 testQuotientAndRemainder]' passed (0.027 seconds).
Test Case '-[NBKDoubleWidthKitBenchmarks.NBKDoubleWidthBenchmarksOnDivisionAsUInt256 testQuotientAndRemainderDividingByDigit]' passed (0.019 seconds).

// Swift 5.8, iPhone 13 Pro, iOS 15.0 (0.233 seconds, 0.547 seconds)

Test Case '-[NBKDoubleWidthKitBenchmarks.NBKDoubleWidthBenchmarksOnDivisionAsUInt256 testQuotientAndRemainder]' passed (0.233 seconds).
Test Case '-[NBKDoubleWidthKitBenchmarks.NBKDoubleWidthBenchmarksOnDivisionAsUInt256 testQuotientAndRemainderDividingByDigit]' passed (0.547 seconds).

oscbyspro avatar Aug 14 '23 12:08 oscbyspro

All of the time is spend calling __udivmodti4, according to Instruments. So, basically, UInt.dividingFullWidth(_:) is slow as snails on iOS 15. Is my interpretation correct? And, if so, why? It seems so random.

oscbyspro avatar Aug 14 '23 18:08 oscbyspro

It seems everything is slower on Xcode 14.2 (iPhone 14 Pro), but not as slow as above.

oscbyspro avatar Aug 22 '23 16:08 oscbyspro

Probably iOS 16 was the first iOS release to pick up this optimisation of __udivmodti4 by @danlark1. Seems a bit late - that patch was integrated in 2020, iOS 16 was released in 2022 - but maybe that's the speed of Swift/LLVM in this regard.

It's pretty funny that the implementation prior to that patch was based on a C translation of PowerPC assembly for a 64-bit divmod function for 32-bit architectures. 😆 (The PowerPC Compiler Writer’s Guide)

wadetregaskis avatar Nov 13 '23 01:11 wadetregaskis

Those were interesting reads. Thanks! I appreciate that you found the source of the problem. Since it has been solved, in a way, I suppose I'll close this issue when iOS 15 gets phased out.

oscbyspro avatar Nov 13 '23 10:11 oscbyspro