mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] gh-139103: fix free-threading dataclass.__init__ perf issue (gh-141596) (gh-141750)
The dataclasses `__init__` function is generated dynamically by a call to `exec()` and so doesn't have deferred reference counting enabled. Enable deferred reference counting on functions when assigned as an attribute to type objects to avoid reference count contention when creating dataclass instances.
(cherry picked from commit ce79154176)
Co-authored-by: Edward Xu <xuxiangad@gmail.com>
This commit is contained in:
parent
8c796889ff
commit
b7c25eabd6
3 changed files with 25 additions and 0 deletions
|
|
@ -27,6 +27,7 @@
|
|||
import sys
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
|
||||
# The iterations in individual benchmarks are scaled by this factor.
|
||||
WORK_SCALE = 100
|
||||
|
|
@ -189,6 +190,17 @@ def thread_local_read():
|
|||
_ = tmp.x
|
||||
|
||||
|
||||
@dataclass
|
||||
class MyDataClass:
|
||||
x: int
|
||||
y: int
|
||||
z: int
|
||||
|
||||
@register_benchmark
|
||||
def instantiate_dataclass():
|
||||
for _ in range(1000 * WORK_SCALE):
|
||||
obj = MyDataClass(x=1, y=2, z=3)
|
||||
|
||||
def bench_one_thread(func):
|
||||
t0 = time.perf_counter_ns()
|
||||
func()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue