Fix perf issues with GetCurrentThreadHomeHeapNumber (#21150)
authorDavid Mason <davmason@microsoft.com>
Fri, 30 Nov 2018 09:25:57 +0000 (01:25 -0800)
committerGitHub <noreply@github.com>
Fri, 30 Nov 2018 09:25:57 +0000 (01:25 -0800)
src/gc/gc.cpp
src/gc/objecthandle.cpp
src/vm/gcenv.ee.cpp

index 285200b..3ff1aa8 100644 (file)
@@ -35215,17 +35215,14 @@ int GCHeap::GetNumberOfHeaps ()
 int GCHeap::GetHomeHeapNumber ()
 {
 #ifdef MULTIPLE_HEAPS
-    Thread *pThread = GCToEEInterface::GetThread();
-    for (int i = 0; i < gc_heap::n_heaps; i++)
+    gc_alloc_context* ctx = GCToEEInterface::GetAllocContext();
+    if (!ctx)
     {
-        if (pThread)
-        {
-            gc_alloc_context* ctx = GCToEEInterface::GetAllocContext();
-            GCHeap *hp = static_cast<alloc_context*>(ctx)->get_home_heap();
-            if (hp == gc_heap::g_heaps[i]->vm_heap) return i;
-        }
+        return 0;
     }
-    return 0;
+
+    GCHeap *hp = static_cast<alloc_context*>(ctx)->get_home_heap();
+    return (hp ? hp->pGenGCHeap->heap_number : 0);
 #else
     return 0;
 #endif //MULTIPLE_HEAPS
index b3d93b4..3045c67 100644 (file)
@@ -1819,8 +1819,7 @@ int GetCurrentThreadHomeHeapNumber()
 {
     WRAPPER_NO_CONTRACT;
 
-    if (g_theGCHeap == nullptr)
-        return 0;
+    assert(g_theGCHeap != nullptr);
     return g_theGCHeap->GetHomeHeapNumber();
 }
 
index d99b12c..ff8f109 100644 (file)
@@ -320,7 +320,11 @@ gc_alloc_context * GCToEEInterface::GetAllocContext()
     WRAPPER_NO_CONTRACT;
 
     Thread* pThread = ::GetThread();
-    assert(pThread != nullptr);
+    if (!pThread)
+    {
+        return nullptr;
+    }
+
     return pThread->GetAllocContext();
 }