From e5147cee711ba86a07099008e429381538060305 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Sat, 30 Aug 2025 12:31:48 +0200 Subject: [PATCH] Add benchmarks for asdict and astuple In preparation for #1463 Co-authored-by: Pieter Eendebak <883786+eendebakpt@users.noreply.github.com> --- bench/test_benchmarks.py | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/bench/test_benchmarks.py b/bench/test_benchmarks.py index 17916caf3..a3ea8a0fa 100644 --- a/bench/test_benchmarks.py +++ b/bench/test_benchmarks.py @@ -119,3 +119,56 @@ def test_hash(): for _ in range(ROUNDS): hash(c) + + +def test_asdict_complicated(): + """ + Benchmark instances with non-shortcut fields. + """ + c = C() + ad = attrs.asdict + + for _ in range(ROUNDS): + ad(c) + + +def test_astuple_complicated(): + """ + Benchmark instances with non-shortcut fields. + """ + c = C() + at = attrs.astuple + + for _ in range(ROUNDS): + at(c) + + +@attrs.define +class AtomicFields: + a: int = 0 + b: Ellipsis = ... + c: str = "foo" + d: tuple[str] = "bar" + e: complex = complex() + + +def test_asdict_atomic(): + """ + Benchmark atomic-only instances. + """ + c = AtomicFields() + ad = attrs.asdict + + for _ in range(ROUNDS): + ad(c) + + +def test_astuple_atomic(): + """ + Benchmark atomic-only instances. + """ + c = AtomicFields() + at = attrs.astuple + + for _ in range(ROUNDS): + at(c)