This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients seberg, vstinner
Date 2020-12-28.15:53:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
PR 23976 stores the currrent interpreter and the current Python thread state into a Thread Local Storage (TLS) using GCC/clang __thread keyword.

On x86-64 using LTO and GCC -O3, _PyThreadState_GET() and _PyInterpreterState_GET() become a single MOV.

Assembly code using LTO and gcc -O3.

_PyThreadState_GET() in _PySys_GetObjectId():

   0x00000000004adabe <+14>: mov rbx,QWORD PTR fs:0xfffffffffffffff8

_PyThreadState_GET() in PyThreadState_Get():

   0x000000000046b660 <+0>: mov  rax,QWORD PTR fs:0xfffffffffffffff8

_PyInterpreterState_GET() in PyTuple_New():

   0x000000000048dfcc <+12>: mov  rax,QWORD PTR fs:0xfffffffffffffff0

_PyInterpreterState_GET() in PyState_FindModule():

   0x000000000044bf20 <+16>: mov  rax,QWORD PTR fs:0xfffffffffffffff0


Note: Without LTO, sometimes there is an indirection:

_PyThreadState_GET() in _PySys_GetObjectId(), 2 MOV (PIC indirection):

   mov    rax,QWORD PTR [rip+0x1eb270]        # 0x713fe0
   # rax = 0xfffffffffffffff0 (-16)
   mov    r13,QWORD PTR fs:[rax]

_PyInterpreterState_GET() in PyTuple_New(), 2 MOV (PIC indirection):

  mov    rax,QWORD PTR [rip+0x294d95]        # 0x713ff8
  mov    rax,QWORD PTR fs:[rax]

An optimized Python should always be built with LTO.
Date User Action Args
2020-12-28 15:53:43vstinnersetrecipients: + vstinner, seberg
2020-12-28 15:53:43vstinnersetmessageid: <>
2020-12-28 15:53:43vstinnerlinkissue40522 messages
2020-12-28 15:53:43vstinnercreate