oscbyspro / AwesomeNumbersKit

Large number arithmetic in Swift
Apache License 2.0
7 stars 1 forks source link

Stdlib encoding performance #60

Open oscbyspro opened 1 year ago

oscbyspro commented 1 year ago

Given that the package provides alternative coding methods via the ANKBigEndianTextCodable protocol, the following is more vexing than problematic. Having said that, if somebody wants to take a deep dive into Protocol Witness Hell :tm: and figure out how to make the stdlib encoding method perform - that'd be cool. As you can see in the list below, there's some funky business going on in String.init(some BinaryInteger, radix: Int, uppercase: Bool):

Test Case '-[ANKFullWidthKitBenchmarks.UInt256BenchmarksOnText testDecodingRadix10]' passed (0.017 seconds).
Test Case '-[ANKFullWidthKitBenchmarks.UInt256BenchmarksOnText testDecodingRadix16]' passed (0.016 seconds).
Test Case '-[ANKFullWidthKitBenchmarks.UInt256BenchmarksOnText testEncodingRadix10]' passed (0.015 seconds).
Test Case '-[ANKFullWidthKitBenchmarks.UInt256BenchmarksOnText testEncodingRadix16]' passed (0.005 seconds).

Test Case '-[ANKFullWidthKitBenchmarks.UInt256BenchmarksOnText testDecodingUsingSwiftStdlibRadix10]' passed (0.037 seconds).
Test Case '-[ANKFullWidthKitBenchmarks.UInt256BenchmarksOnText testDecodingUsingSwiftStdlibRadix16]' passed (0.034 seconds).
Test Case '-[ANKFullWidthKitBenchmarks.UInt256BenchmarksOnText testEncodingUsingSwiftStdlibRadix10]' passed (5.007 seconds).
Test Case '-[ANKFullWidthKitBenchmarks.UInt256BenchmarksOnText testEncodingUsingSwiftStdlibRadix16]' passed (4.038 seconds).
oscbyspro commented 1 year ago

Adding the top-secret™ _lowWord accessor makes the StdlibRadix10 case 30% faster.

oscbyspro commented 1 year ago

Just look at that 2600x base 16 encoding ratio...

Test Case '-[ANKFullWidthKitBenchmarks.UInt256BenchmarksOnText testEncodingRadix16]' passed (0.005 seconds).
Test Case '-[ANKFullWidthKitBenchmarks.UInt256BenchmarksOnText testEncodingUsingSwiftStdlibRadix16]' passed (12.986 seconds).
oscbyspro commented 1 year ago

The base 10 case is ironically made 2x faster by omitting the fast paths in the binary integer init methods. Well, I won’t do that as it should only matter when they are unspecialized, which for my purposes means never. Using a more efficient form of type testing (#71) might have a similar effect, however.

oscbyspro commented 1 year ago

I’ve not kept track of what changes are responsible, but:

oscbyspro commented 1 year ago

Adding custom smart-shifts reduces base 16 stdlib encoding to 4.284 seconds.

oscbyspro commented 1 year ago

I'm pretty sure (#88) sent the base 10 case to an outer ring of Tuple's Inferno™