From 2a391aeb3129e89ec453e3685c9ed3e5ab4ed4f5 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Tue, 16 Nov 2021 17:28:58 -0800 Subject: [PATCH 1/3] re-enable optimization when a token is replaced by corresponding type handle --- src/coreclr/vm/clsload.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/coreclr/vm/clsload.cpp b/src/coreclr/vm/clsload.cpp index 68de4d4b718fab..8d5e22bdabba60 100644 --- a/src/coreclr/vm/clsload.cpp +++ b/src/coreclr/vm/clsload.cpp @@ -1536,19 +1536,19 @@ ClassLoader::LoadTypeHandleThrowing( pClsLdr = pFoundModule->GetClassLoader(); pLookInThisModuleOnly = NULL; } + } #ifndef DACCESS_COMPILE - // Replace AvailableClasses Module entry with found TypeHandle - if (!typeHnd.IsNull() && - typeHnd.IsRestored() && - foundEntry.GetEntryType() == HashedTypeEntry::EntryType::IsHashedClassEntry && - (foundEntry.GetClassHashBasedEntryValue() != NULL) && - (foundEntry.GetClassHashBasedEntryValue()->GetData() != typeHnd.AsPtr())) - { - foundEntry.GetClassHashBasedEntryValue()->SetData(typeHnd.AsPtr()); - } -#endif // !DACCESS_COMPILE + // Replace AvailableClasses Module entry with found TypeHandle + if (!typeHnd.IsNull() && + typeHnd.IsRestored() && + foundEntry.GetEntryType() == HashedTypeEntry::EntryType::IsHashedClassEntry && + (foundEntry.GetClassHashBasedEntryValue() != NULL) && + (foundEntry.GetClassHashBasedEntryValue()->GetData() != typeHnd.AsPtr())) + { + foundEntry.GetClassHashBasedEntryValue()->SetData(typeHnd.AsPtr()); } +#endif // !DACCESS_COMPILE RETURN typeHnd; } // ClassLoader::LoadTypeHandleThrowing From aebd63bda134d2567aeabf0bc6aaaac6caf37342 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Tue, 16 Nov 2021 17:40:15 -0800 Subject: [PATCH 2/3] compute "isNested" ony when we need it. --- src/coreclr/vm/clsload.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coreclr/vm/clsload.cpp b/src/coreclr/vm/clsload.cpp index 8d5e22bdabba60..1980c70200c4a3 100644 --- a/src/coreclr/vm/clsload.cpp +++ b/src/coreclr/vm/clsload.cpp @@ -627,7 +627,6 @@ void ClassLoader::GetClassValue(NameHandleTable nhTable, CONTRACTL_END - mdToken mdEncloser; EEClassHashEntry_t *pBucket = NULL; needsToBuildHashtable = FALSE; @@ -643,8 +642,6 @@ void ClassLoader::GetClassValue(NameHandleTable nhTable, } #endif - BOOL isNested = IsNested(pName, &mdEncloser); - PTR_Assembly assembly = GetAssembly(); PREFIX_ASSUME(assembly != NULL); ModuleIterator i = assembly->IterateModules(); @@ -719,6 +716,9 @@ void ClassLoader::GetClassValue(NameHandleTable nhTable, } _ASSERTE(pTable); + mdToken mdEncloser; + BOOL isNested = IsNested(pName, &mdEncloser); + if (isNested) { Module *pNameModule = pName->GetTypeModule(); From 4407a1eafac6f0499c885524c8a5cbea7a0a2459 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Tue, 16 Nov 2021 21:18:50 -0800 Subject: [PATCH 3/3] no need to make more than 2 attempts at reading --- src/coreclr/vm/clsload.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/coreclr/vm/clsload.cpp b/src/coreclr/vm/clsload.cpp index 1980c70200c4a3..0578cc20b54698 100644 --- a/src/coreclr/vm/clsload.cpp +++ b/src/coreclr/vm/clsload.cpp @@ -834,6 +834,9 @@ void ClassLoader::LazyPopulateCaseSensitiveHashTables() } CONTRACTL_END; + + _ASSERT(m_cUnhashedModules > 0); + AllocMemTracker amTracker; ModuleIterator i = GetAssembly()->IterateModules(); @@ -909,6 +912,8 @@ void ClassLoader::LazyPopulateCaseInsensitiveHashTables() amTracker.SuppressRelease(); pModule->SetAvailableClassCaseInsHash(pNewClassCaseInsHash); FastInterlockDecrement((LONG*)&m_cUnhashedModules); + + _ASSERT(m_cUnhashedModules >= 0); } } } @@ -1238,18 +1243,18 @@ BOOL ClassLoader::FindClassModuleThrowing( if (pBucket == NULL) { + // Take the lock. To make sure the table is not being built by another thread. AvailableClasses_LockHolder lh(this); - // Try again with the lock. This will protect against another thread reallocating - // the hash table underneath us - GetClassValue(nhTable, pName, &Data, &pTable, pLookInThisModuleOnly, &foundEntry, loadFlag, needsToBuildHashtable); - pBucket = foundEntry.GetClassHashBasedEntryValue(); - + if (!needsToBuildHashtable || (m_cUnhashedModules == 0)) + { + // the table should be finished now, try again + GetClassValue(nhTable, pName, &Data, &pTable, pLookInThisModuleOnly, &foundEntry, loadFlag, needsToBuildHashtable); + pBucket = foundEntry.GetClassHashBasedEntryValue(); + } #ifndef DACCESS_COMPILE - if (needsToBuildHashtable && (pBucket == NULL) && (m_cUnhashedModules > 0)) + else { - _ASSERT(needsToBuildHashtable); - if (nhTable == nhCaseInsensitive) { LazyPopulateCaseInsensitiveHashTables(); @@ -1268,7 +1273,7 @@ BOOL ClassLoader::FindClassModuleThrowing( #endif } - // Same check as above, but this time we've checked with the lock so the table will be populated + // Same check as above, but this time we've ensured that the tables are populated if (pBucket == NULL) { #if defined(_DEBUG_IMPL) && !defined(DACCESS_COMPILE)