|
4 | 4 |
|
5 | 5 | from abc import abstractmethod |
6 | 6 | from typing import Callable, Final |
7 | | -import sys |
8 | 7 |
|
9 | 8 | from mypy.nodes import ( |
10 | 9 | EXCLUDED_ENUM_ATTRIBUTES, |
|
29 | 28 | is_class_var, |
30 | 29 | ) |
31 | 30 | from mypy.types import Instance, UnboundType, get_proper_type |
32 | | -from mypyc.common import PROPSET_PREFIX, IS_FREE_THREADED |
| 31 | +from mypyc.common import PROPSET_PREFIX |
33 | 32 | from mypyc.ir.class_ir import ClassIR, NonExtClassInfo |
34 | 33 | from mypyc.ir.func_ir import FuncDecl, FuncSignature |
35 | 34 | from mypyc.ir.ops import ( |
|
82 | 81 | py_calc_meta_op, |
83 | 82 | pytype_from_template_op, |
84 | 83 | type_object_op, |
85 | | - set_immortal_op, |
86 | 84 | ) |
87 | 85 | from mypyc.subtype import is_subtype |
88 | 86 |
|
@@ -264,6 +262,9 @@ def finalize(self, ir: ClassIR) -> None: |
264 | 262 | non_ext_class = load_non_ext_class(self.builder, ir, self.non_ext, self.cdef.line) |
265 | 263 | non_ext_class = load_decorated_class(self.builder, self.cdef, non_ext_class) |
266 | 264 |
|
| 265 | + # Try to avoid contention when using free threading. |
| 266 | + self.builder.set_immortal_if_free_threaded(non_ext_class, self.cdef.line) |
| 267 | + |
267 | 268 | # Save the decorated class |
268 | 269 | self.builder.add( |
269 | 270 | InitStatic(non_ext_class, self.cdef.name, self.builder.module_name, NAMESPACE_TYPE) |
@@ -451,10 +452,11 @@ def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value: |
451 | 452 | ) |
452 | 453 | # Create the class |
453 | 454 | tp = builder.call_c(pytype_from_template_op, [template, tp_bases, modname], cdef.line) |
454 | | - if IS_FREE_THREADED and sys.version_info >= (3, 14): |
455 | | - # Set type object to be immortal, as otherwise reference count contention |
456 | | - # can cause a massive performance hit in the worst case. |
457 | | - builder.call_c(set_immortal_op, [tp], cdef.line) |
| 455 | + |
| 456 | + # Set type object to be immortal if free threaded, as otherwise reference count contention |
| 457 | + # can cause a big performance hit. |
| 458 | + builder.set_immortal_if_free_threaded(tp, cdef.line) |
| 459 | + |
458 | 460 | # Immediately fix up the trait vtables, before doing anything with the class. |
459 | 461 | ir = builder.mapper.type_to_ir[cdef.info] |
460 | 462 | if not ir.is_trait and not ir.builtin_base: |
|
0 commit comments