Skip to content

Commit 0a2ce0c

Browse files
authored
Read the GC's thread context from a __thread pointer on Linux (#429)
On Linux the fast thread-local slot, the GC's stack context, was read with pthread_getspecific: a call through the PLT on each allocation that leaves the inline path, each time a String or a number is boxed, and wherever HX_CTX_GET is used. In an OpenFL game it was 3 to 4% of the main thread's samples. With GCC and Clang on Linux it is now a __thread pointer, read with one load from the thread pointer. __thread rather than thread_local: for an extern thread_local, C++ calls a wrapper in case another unit initializes it dynamically. Defining HXCPP_PTHREAD_FAST_TLS keeps pthread_getspecific.
1 parent 810ced2 commit 0a2ce0c

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎include/hx/Tls.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ struct TLSData
144144
#define EXTERN_FAST_TLS_DATA(TYPE,NAME) \
145145
__declspec(thread) extern TYPE * NAME;
146146

147+
#elif defined(HX_LINUX) && defined(__GNUC__) && !defined(HXCPP_PTHREAD_FAST_TLS)
148+
149+
// The fast slot (the GC's stack context, read on each allocation that leaves
150+
// the inline path) is a plain thread-local pointer: one load from the thread
151+
// pointer instead of a call to pthread_getspecific. __thread rather than
152+
// thread_local, so the extern declaration reads it directly instead of
153+
// calling the wrapper C++ emits in case of a dynamic initializer.
154+
#define DECLARE_TLS_DATA(TYPE,NAME) \
155+
::hx::TLSData<TYPE> NAME;
156+
#define DECLARE_FAST_TLS_DATA(TYPE,NAME) \
157+
__thread TYPE * NAME = nullptr;
158+
#define EXTERN_TLS_DATA(TYPE,NAME) \
159+
extern ::hx::TLSData<TYPE> NAME;
160+
#define EXTERN_FAST_TLS_DATA(TYPE,NAME) \
161+
extern __thread TYPE * NAME;
162+
147163
#else
148164

149165
#define DECLARE_TLS_DATA(TYPE,NAME) \

0 commit comments

Comments
 (0)