Leverage new Windows API TlsGetValue2 for performance

This commit is contained in:
Ben Niu
2023-12-21 20:33:41 -08:00
committed by Qi Wang
parent e29ac61987
commit 9e123a833c
3 changed files with 32 additions and 4 deletions

View File

@@ -24,7 +24,7 @@
# ifdef __arm__ # ifdef __arm__
# define LG_QUANTUM 3 # define LG_QUANTUM 3
# endif # endif
# ifdef __aarch64__ # if defined(__aarch64__) || defined(_M_ARM64)
# define LG_QUANTUM 4 # define LG_QUANTUM 4
# endif # endif
# ifdef __hppa__ # ifdef __hppa__

View File

@@ -15,6 +15,16 @@ typedef struct {
extern DWORD tsd_tsd; extern DWORD tsd_tsd;
extern tsd_wrapper_t tsd_boot_wrapper; extern tsd_wrapper_t tsd_boot_wrapper;
extern bool tsd_booted; extern bool tsd_booted;
#if defined(_M_ARM64EC)
#define JEMALLOC_WIN32_TLSGETVALUE2 0
#else
#define JEMALLOC_WIN32_TLSGETVALUE2 1
#endif
#if JEMALLOC_WIN32_TLSGETVALUE2
typedef LPVOID (WINAPI *TGV2)(DWORD dwTlsIndex);
extern TGV2 tls_get_value2;
extern HMODULE tgv2_mod;
#endif
/* Initialization/cleanup. */ /* Initialization/cleanup. */
JEMALLOC_ALWAYS_INLINE bool JEMALLOC_ALWAYS_INLINE bool
@@ -49,9 +59,17 @@ tsd_wrapper_set(tsd_wrapper_t *wrapper) {
JEMALLOC_ALWAYS_INLINE tsd_wrapper_t * JEMALLOC_ALWAYS_INLINE tsd_wrapper_t *
tsd_wrapper_get(bool init) { tsd_wrapper_get(bool init) {
DWORD error = GetLastError(); tsd_wrapper_t *wrapper;
tsd_wrapper_t *wrapper = (tsd_wrapper_t *) TlsGetValue(tsd_tsd); #if JEMALLOC_WIN32_TLSGETVALUE2
SetLastError(error); if (tls_get_value2 != NULL) {
wrapper = (tsd_wrapper_t *) tls_get_value2(tsd_tsd);
} else
#endif
{
DWORD error = GetLastError();
wrapper = (tsd_wrapper_t *) TlsGetValue(tsd_tsd);
SetLastError(error);
}
if (init && unlikely(wrapper == NULL)) { if (init && unlikely(wrapper == NULL)) {
wrapper = (tsd_wrapper_t *) wrapper = (tsd_wrapper_t *)
@@ -78,6 +96,12 @@ tsd_boot0(void) {
} }
_malloc_tsd_cleanup_register(&tsd_cleanup_wrapper); _malloc_tsd_cleanup_register(&tsd_cleanup_wrapper);
tsd_wrapper_set(&tsd_boot_wrapper); tsd_wrapper_set(&tsd_boot_wrapper);
#if JEMALLOC_WIN32_TLSGETVALUE2
tgv2_mod = LoadLibraryA("api-ms-win-core-processthreads-l1-1-8.dll");
if (tgv2_mod != NULL) {
tls_get_value2 = (TGV2)GetProcAddress(tgv2_mod, "TlsGetValue2");
}
#endif
tsd_booted = true; tsd_booted = true;
return false; return false;
} }

View File

@@ -25,6 +25,10 @@ bool tsd_booted = false;
DWORD tsd_tsd; DWORD tsd_tsd;
tsd_wrapper_t tsd_boot_wrapper = {false, TSD_INITIALIZER}; tsd_wrapper_t tsd_boot_wrapper = {false, TSD_INITIALIZER};
bool tsd_booted = false; bool tsd_booted = false;
#if JEMALLOC_WIN32_TLSGETVALUE2
TGV2 tls_get_value2 = NULL;
HMODULE tgv2_mod = NULL;
#endif
#else #else
/* /*