runtime icon indicating copy to clipboard operation
runtime copied to clipboard

CopyWithPrivateKey appears to have very different performance on Linux, Windows and macOS

Open qmfrederik opened this issue 8 years ago β€’ 5 comments

The performance of X509Certificate.CopyWithPrivateKey for 2048-bit RSA keys appears to be very different on Windows, Linux and macOS:

Platform Time Relative
Windows 2.323 ms x 74
Linux 0.031 ms 1
macOS 82.036 ms x 2636

Comparing the perf on Linux, macOS and Windows is to some extent comparing apples and oranges, especially when using AppVeyor and Travis, but the differences (x 74 on Windows, x 2636 on macOS) seem to point to something deeper (perhaps an issue with my benchmark code, who knows πŸ˜„).

Benchmarks for our own tests, where we use similar hardware for Windows, Linux and macOS, show the same pattern - x 40 on Windows and x 108 on macOS.

  • Benchmark code: https://github.com/qmfrederik/dotnet-cert-perf
  • Windows run (AppVeyor): https://ci.appveyor.com/project/qmfrederik/dotnet-cert-perf/build/1.0.2
  • macOS run (Travis): https://travis-ci.org/qmfrederik/dotnet-cert-perf/jobs/395371518
  • Linux run (Travis): https://travis-ci.org/qmfrederik/dotnet-cert-perf/jobs/395371519

Windows

Intel Xeon CPU E5-2697 v3 2.60GHz, 1 CPU, 2 logical and 2 physical cores

Method Mean Error StdDev
CopyWithPrivateKey 3,878,931.66 ns 34,279.5860 ns 30,387.9571 ns
CopyWithPrivateKeyReuseRSA 2,323,796.09 ns 30,222.2591 ns 28,269.9171 ns
CreateRSA 25.54 ns 0.4243 ns 0.3761 ns

macOS

Intel Xeon CPU E5-2697 v2 2.70GHz, 2 CPU, 2 logical and 2 physical cores

Method Mean Error StdDev
CopyWithPrivateKey 82,036,805.16 ns 1,618,144.1343 ns 3,585,695.6307 ns
CopyWithPrivateKeyReuseRSA 83,821,069.31 ns 1,674,732.9700 ns 3,019,888.9999 ns
CreateRSA 39.12 ns 0.7748 ns 0.7248 ns

Linux

Intel Xeon CPU E5-2680 v2 2.80GHz, 2 CPU, 32 logical and 16 physical cores

Method Mean Error StdDev Median
CopyWithPrivateKey 34,576.6 ns 899.394 ns 2,507.152 ns 34,298.2 ns
CopyWithPrivateKeyReuseRSA 31,117.7 ns 612.547 ns 897.863 ns 31,271.3 ns
CreateRSA 144.9 ns 3.026 ns 7.192 ns 142.0 ns

qmfrederik avatar Jun 22 '18 08:06 qmfrederik

/cc @bartonjs

qmfrederik avatar Jun 22 '18 08:06 qmfrederik

Everyone has to export public keys and see if they match (different cost on different systems).

  • Linux then just uprefs the native pointer, assigns a managed field.
  • Windows sets a property on the new cert context, so a P/Invoke into a big switch with some extra validation. Plus building the new cert context.
  • macOS has to build a temporary keychain (which touches the filesystem), copy the certificate into the keychain, copy the private key into the keychain, then ask the keychain to build a SecIdentityRef.

You seem to have an error in scale, or transcription, though. 82 million nanoseconds is 82 milliseconds, not 82 thousand milliseconds. Unless that's using "ms" as "microseconds" (mu-s).

It's possible you'll normalize your time a bit if you use each key, because it could be in a delayed processing state which is paid at the time we call ExportParameters. Calling ExportParameters(true) and/or VerifyHash(new byte[20], new byte[key.KeySize >> 3], HashAlgorithmNames.SHA1, RSASignaturePadding.Pkcs1) in the initialize phase may ensure the key is "ready" on all platforms, making sure that the same relative work is being measured.

bartonjs avatar Jun 22 '18 14:06 bartonjs

@bartonjs Thanks, I fixed the conversion to milliseconds πŸ˜„ .

I updated the benchmarks to trigger ExportParameters but it does not appear to have a major impact:

  • https://ci.appveyor.com/project/qmfrederik/dotnet-cert-perf/build/1.0.3
  • https://travis-ci.org/qmfrederik/dotnet-cert-perf/jobs/395588159
  • https://travis-ci.org/qmfrederik/dotnet-cert-perf/jobs/395588160

Since you mentioned macOS builds a temporary keychain, I added another benchmark which uses BouncyCastle to create a PKCS12 store, add the private and public key, and create a X509Certificate2 form that PKCS12 store.

It turns out that Bouncy Castle actually outperforms the current .NET Core implementation on macOS:

Method Mean Error StdDev
CopyWithPrivateKey 84.84 ms 1.694 ms 3.499 ms
CopyWithPrivateKeyReuseRSA 88.98 ms 1.728 ms 2.365 ms
CopyWithPrivateKeyBouncy 79.69 ms 1.581 ms 2.811 ms

qmfrederik avatar Jun 22 '18 19:06 qmfrederik

Thanks I’m trying to learn but it’s harder than I thought.

Sent from my iPhone

On Jun 22, 2018, at 3:17 PM, Frederik Carlier [email protected] wrote:

@bartonjs Thanks, I fixed the conversion to milliseconds πŸ˜„ .

I updated the benchmarks to trigger ExportParameters but it does not appear to have a major impact:

https://ci.appveyor.com/project/qmfrederik/dotnet-cert-perf/build/1.0.3 https://travis-ci.org/qmfrederik/dotnet-cert-perf/jobs/395588159 https://travis-ci.org/qmfrederik/dotnet-cert-perf/jobs/395588160 Since you mentioned macOS builds a temporary keychain, I added another benchmark which uses BouncyCastle to create a PKCS12 store, add the private and public key, and create a X509Certificate2 form that PKCS12 store.

It turns out that Bouncy Castle actually outperforms the current .NET Core implementation on macOS:

Method Mean Error StdDev CopyWithPrivateKey 84.84 ms 1.694 ms 3.499 ms CopyWithPrivateKeyReuseRSA 88.98 ms 1.728 ms 2.365 ms CopyWithPrivateKeyBouncy 79.69 ms 1.581 ms 2.811 ms β€” You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

Hillbillywild avatar Jun 22 '18 20:06 Hillbillywild

Ultimately, the problem here is that the only way Apple supports getting a SecIdentityRef (which is required for using any of their API with a cert+key) is via a keychain; and once we have dynamic cert+key pairings we have to make temporary keychains, and that has high overhead.

BouncyCastle avoids this problem by not making use of the system cryptography layer (since they're an entire re-implementation). .NET, conversely, considers it important to utilize platform cryptography components.

bartonjs avatar Feb 25 '19 21:02 bartonjs