Improve OidLookup.ToLookup perf, in particular for X509Certificate2.Extensions (...
authorStephen Toub <stoub@microsoft.com>
Mon, 11 Jan 2021 21:00:26 +0000 (16:00 -0500)
committerGitHub <noreply@github.com>
Mon, 11 Jan 2021 21:00:26 +0000 (16:00 -0500)
commit323e7a329c0d5a118cfcbff6b202dc4172b21f4a
treece9f64f27411550fe27c7c049ada213d086f4d39
parent5282582d79c9cdc1a57c3a58e95485cb329efec7
Improve OidLookup.ToLookup perf, in particular for X509Certificate2.Extensions (#46819)

* Avoid OidLookup.ToOid call per extension in X509Certificate2.Extensions

`X509Certificate2.Extensions` invokes `CertificatePal.Extensions`, which in turn creates the extensions collection, invoking `new Oid(string)` for each.  This in turn calls `OidLookup.ToOid` in order to gather the friendly name, even though in many situations, no one actually cares about the friendly name.  We can instead call `new Oid(string, null)`, which makes the friendly name lazily initialized on first use, saving the `OidLookup.ToOid` call when it's not needed, and significantly reducing the time to call Extensions (in particular when the predefined OID lookup tables don't contain the OID for an extension and when it can't be found on lookup).

* Allow OidLookup.ToOid to cache failure

ToOid has a cache, but it only caches successful results.  If ToOid fails to find the relevant OID, nothing is cached, which makes the failure path very expensive, as every ToOid call for that OID takes the slow path.  This lets it be cached.
src/libraries/System.Security.Cryptography.Encoding/src/Internal/Cryptography/OidLookup.cs
src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/CertificatePal.cs