Skip to content

Commit d0b5b90

Browse files
committed
[2.0>master] [MERGE chakra-core#2777 @obastemur] xplat: Improve GetCurrent*Thread performance
Merge pull request chakra-core#2777 from thread_wait_fix This PR targets node-chakracore server load testing scenarios. Perf hungry JS application triggers GetCurrent*Thread more than 10K times per second. Use front thread_local caching if possible.
2 parents cd41569 + 259910a commit d0b5b90

3 files changed

Lines changed: 47 additions & 14 deletions

File tree

lib/Runtime/PlatformAgnostic/Platform/Common/HiResTimer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace DateTime
7777
// in case the system time wasn't updated backwards, and cache is still beyond...
7878
if (currentTime >= data.cacheSysTime && currentTime < data.cacheSysTime + INTERVAL_FOR_TICK_BACKUP)
7979
{
80-
return data.cacheSysTime + INTERVAL_FOR_TICK_BACKUP - 1; // wait for real time
80+
return data.cacheSysTime + INTERVAL_FOR_TICK_BACKUP; // wait for real time
8181
}
8282

8383
data.cacheSysTime = currentTime;

pal/src/include/pal/thread.hpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -737,20 +737,9 @@ namespace CorUnix
737737
extern "C" CPalThread *CreateCurrentThreadData();
738738
#endif // FEATURE_PAL_SXS
739739

740-
inline CPalThread *GetCurrentPalThread()
741-
{
742-
return reinterpret_cast<CPalThread*>(pthread_getspecific(thObjKey));
743-
}
740+
CPalThread *GetCurrentPalThread(bool force = false);
744741

745-
inline CPalThread *InternalGetCurrentThread()
746-
{
747-
CPalThread *pThread = GetCurrentPalThread();
748-
#if defined(FEATURE_PAL_SXS)
749-
if (pThread == nullptr)
750-
pThread = CreateCurrentThreadData();
751-
#endif // FEATURE_PAL_SXS
752-
return pThread;
753-
}
742+
CPalThread *InternalGetCurrentThread();
754743

755744
/***
756745

pal/src/thread/pal_thread.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,3 +2911,47 @@ bool IsAddressOnStack(ULONG_PTR address)
29112911

29122912
return false;
29132913
}
2914+
2915+
#ifndef __IOS__
2916+
// why _Thread_local ? Because it is faster(.)
2917+
// why not replace PAL to use _Thread_local instead of front caching? _Thread_local is not cross platform
2918+
2919+
// Why ULONG_PTR? keeping type for localThread `simple` may affect implementation hence the perf. positively.
2920+
THREAD_LOCAL ULONG_PTR localThread = 0;
2921+
CPalThread *CorUnix::GetCurrentPalThread(bool force)
2922+
{
2923+
ULONG_PTR pThread = localThread;
2924+
if (pThread == 0)
2925+
{
2926+
pThread = (ULONG_PTR) reinterpret_cast<CPalThread*>(pthread_getspecific(thObjKey));
2927+
#ifdef FEATURE_PAL_SXS
2928+
if (pThread == 0 && force)
2929+
{
2930+
pThread = (ULONG_PTR) CreateCurrentThreadData();
2931+
}
2932+
#endif
2933+
localThread = pThread;
2934+
}
2935+
2936+
return (CPalThread*)pThread;
2937+
}
2938+
#else // !__IOS__
2939+
CPalThread *CorUnix::GetCurrentPalThread(bool force)
2940+
{
2941+
CPalThread *pThread = reinterpret_cast<CPalThread*>(pthread_getspecific(thObjKey));
2942+
2943+
#ifdef FEATURE_PAL_SXS
2944+
if (pThread == nullptr && force)
2945+
{
2946+
pThread = CreateCurrentThreadData();
2947+
}
2948+
#endif
2949+
2950+
return pThread;
2951+
}
2952+
#endif
2953+
2954+
CPalThread *CorUnix::InternalGetCurrentThread()
2955+
{
2956+
return GetCurrentPalThread(true);
2957+
}

0 commit comments

Comments
 (0)