google / webcrypto.dart

Cross-platform implementation of Web Cryptography APIs
https://pub.dev/packages/webcrypto
Apache License 2.0
81 stars 48 forks source link

Very slow AES-CTR performance on Mac M1 (other arm64 too? I will report back) #161

Closed cconstab closed 3 weeks ago

cconstab commented 3 weeks ago

I have been looking to replace/improve the AES-CTR streaming package that I am using at present (encrypt) which tops out at about 50mbps when using AOT.

I got the webcrypto package working and see great improvements on Windows (up to 250mbps) but when testing on Mac M1 very poor results both in AOT and JIT..

When comparing with `package:better_cryptography/better_cryptography.dart' ---> Performs best in AOT/JIT 'package:encrypt/encrypt.dart'

JIT

String length: 5120 KB
-----------------------------------------------
Start software encrypt AESCTR
..........
1987 ms
201 mbps
software encrypt end
-----------------------------------------------
Start Better AES CTR
..........
2328 ms
180 mbps
Better end
Better faster by: -15%
-----------------------------------------------
Start BoringSSL AES CTR
..........
29881 ms
14 mbps
BoringSSL end
BoringSSL faster by: -93%
-----------------------------------------------

And AOT

String length: 5120 KB
-----------------------------------------------
Start software encrypt AESCTR
..........
6826 ms
59 mbps
software encrypt end
-----------------------------------------------
Start Better AES CTR
..........
2414 ms
174 mbps
Better end
Better faster by: 183%
-----------------------------------------------
Start BoringSSL AES CTR
..........
30730 ms
14 mbps
BoringSSL end
BoringSSL faster by: -78%
-----------------------------------------------
cconstab commented 3 weeks ago

For comparison same code running on Windows..

JIT

String length: 5120 KB
-----------------------------------------------
Start software AESCTR
..........
2490 ms
161 mbps
software end
-----------------------------------------------
Start Better AES CTR
..........
1268 ms
331 mbps
Better end
Better faster by: 96%
-----------------------------------------------
Start BoringSSL AES CTR
..........
1403 ms
299 mbps
BoringSSL end
BoringSSL faster by: 77%
-----------------------------------------------

and AOT

-----------------------------------------------
String length: 5120 KB
-----------------------------------------------
Start software AESCTR
..........
7716 ms
52 mbps
software end
-----------------------------------------------
Start Better AES CTR
..........
1595 ms
263 mbps
Better end
Better faster by: 384%
-----------------------------------------------
Start BoringSSL AES CTR
..........
1680 ms
250 mbps
BoringSSL end
BoringSSL faster by: 359%
-----------------------------------------------
jonasfj commented 3 weeks ago

On all platforms other than web this package shells out to BoringSSL.

If performance is slow, it might have to do with compiler options. https://github.com/google/webcrypto.dart/blob/8ac145beef348a55f0e268f11101bc8f9a8e3fec/darwin/webcrypto.podspec#L29-L30

One could try to enable those options on Linux or Windows to see if you get similar performance degradation.

In which case it might be worth figuring out how to enable ASM on Mac/iOS. Or compile without DDOPENSSL_SMALL not targeting Android or iOS.

If you find an improvement PRs are appreciated.

cconstab commented 3 weeks ago

I closed while I work a little harder to solve and will raise PR once I have a solution rather than a problem :-)