Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve OidLookup.ToOid perf, in particular for X509Certificate2.Extensions #46819

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ internal static partial class OidLookup
private static readonly ConcurrentDictionary<string, string> s_lateBoundOidToFriendlyName =
new ConcurrentDictionary<string, string>();

private static readonly ConcurrentDictionary<string, string> s_lateBoundFriendlyNameToOid =
new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private static readonly ConcurrentDictionary<string, string?> s_lateBoundFriendlyNameToOid =
new ConcurrentDictionary<string, string?>(StringComparer.OrdinalIgnoreCase);

//
// Attempts to map a friendly name to an OID. Returns null if not a known name.
Expand Down Expand Up @@ -80,13 +80,19 @@ internal static partial class OidLookup

mappedOid = NativeFriendlyNameToOid(friendlyName, oidGroup, fallBackToAllGroups);

if (shouldUseCache && mappedOid != null)
if (shouldUseCache)
{
s_lateBoundFriendlyNameToOid.TryAdd(friendlyName, mappedOid);

// Don't add the reverse here. Friendly Name => OID is a case insensitive search,
// so the casing provided as input here may not be the 'correct' one. Just let
// ToFriendlyName capture the response and cache it itself.

// Also, mappedOid could be null here if the lookup failed. Allowing storing null
// means we're able to cache that a lookup failed so we don't repeat it. It's
// theoretically possible, however, the failure could have been intermittent, e.g.
// if the call was forced to follow an AD fallback path and the relevant servers
// were offline.
}

return mappedOid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public IEnumerable<X509Extension> Extensions
{
CERT_EXTENSION* pCertExtension = pCertInfo->rgExtension + i;
string oidValue = Marshal.PtrToStringAnsi(pCertExtension->pszObjId)!;
Oid oid = new Oid(oidValue);
Oid oid = new Oid(oidValue, friendlyName: null);
bool critical = pCertExtension->fCritical != 0;
byte[] rawData = pCertExtension->Value.ToByteArray();

Expand Down