gh-140281: Update free threading Python HOWTO for 3.14 (gh-140566) · python/cpython@2dc28eb

@@ -11,9 +11,7 @@ available processing power by running threads in parallel on available CPU cores

1111

While not all software will benefit from this automatically, programs

1212

designed with threading in mind will run faster on multi-core hardware.

131314-

The free-threaded mode is working and continues to be improved, but

15-

there is some additional overhead in single-threaded workloads compared

16-

to the regular build. Additionally, third-party packages, in particular ones

14+

Some third-party packages, in particular ones

1715

with an :term:`extension module`, may not be ready for use in a

1816

free-threaded build, and will re-enable the :term:`GIL`.

1917

@@ -101,63 +99,42 @@ This section describes known limitations of the free-threaded CPython build.

10199

Immortalization

102100

---------------

103101104-

The free-threaded build of the 3.13 release makes some objects :term:`immortal`.

102+

In the free-threaded build, some objects are :term:`immortal`.

105103

Immortal objects are not deallocated and have reference counts that are

106104

never modified. This is done to avoid reference count contention that would

107105

prevent efficient multi-threaded scaling.

108106109-

An object will be made immortal when a new thread is started for the first time

110-

after the main thread is running. The following objects are immortalized:

107+

As of the 3.14 release, immortalization is limited to:

111108112-

* :ref:`function <user-defined-funcs>` objects declared at the module level

113-

* :ref:`method <instance-methods>` descriptors

114-

* :ref:`code <code-objects>` objects

115-

* :term:`module` objects and their dictionaries

116-

* :ref:`classes <classes>` (type objects)

117-118-

Because immortal objects are never deallocated, applications that create many

119-

objects of these types may see increased memory usage under Python 3.13. This

120-

has been addressed in the 3.14 release, where the aforementioned objects use

121-

deferred reference counting to avoid reference count contention.

122-123-

Additionally, numeric and string literals in the code as well as strings

124-

returned by :func:`sys.intern` are also immortalized in the 3.13 release. This

125-

behavior is part of the 3.14 release as well and it is expected to remain in

126-

future free-threaded builds.

109+

* Code constants: numeric literals, string literals, and tuple literals

110+

composed of other constants.

111+

* Strings interned by :func:`sys.intern`.

127112128113129114

Frame objects

130115

-------------

131116132-

It is not safe to access :ref:`frame <frame-objects>` objects from other

133-

threads and doing so may cause your program to crash . This means that

134-

:func:`sys._current_frames` is generally not safe to use in a free-threaded

135-

build. Functions like :func:`inspect.currentframe` and :func:`sys._getframe`

136-

are generally safe as long as the resulting frame object is not passed to

137-

another thread.

117+

It is not safe to access :attr:`frame.f_locals` from a :ref:`frame <frame-objects>`

118+

object if that frame is currently executing in another thread, and doing so may

119+

crash the interpreter.

120+138121139122

Iterators

140123

---------

141124142-

Sharing the same iterator object between multiple threads is generally not

143-

safe and threads may see duplicate or missing elements when iterating or crash

144-

the interpreter.

125+

It is generally not thread-safe to access the same iterator object from

126+

multiple threads concurrently, and threads may see duplicate or missing

127+

elements.

145128146129147130

Single-threaded performance

148131

---------------------------

149132150133

The free-threaded build has additional overhead when executing Python code

151-

compared to the default GIL-enabled build. In 3.13, this overhead is about

152-

40% on the `pyperformance <https://pyperformance.readthedocs.io/>`_ suite.

153-

Programs that spend most of their time in C extensions or I/O will see

154-

less of an impact. The largest impact is because the specializing adaptive

155-

interpreter (:pep:`659`) is disabled in the free-threaded build.

156-157-

The specializing adaptive interpreter has been re-enabled in a thread-safe way

158-

in the 3.14 release. The performance penalty on single-threaded code in

159-

free-threaded mode is now roughly 5-10%, depending on the platform and C

160-

compiler used.

134+

compared to the default GIL-enabled build. The amount of overhead depends

135+

on the workload and hardware. On the pyperformance benchmark suite, the

136+

average overhead ranges from about 1% on macOS aarch64 to 8% on x86-64 Linux

137+

systems.

161138162139163140

Behavioral changes