From 301d9985d80c012aafc52c3b708fb634b9d1e56a Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 13 May 2025 22:14:30 +0200 Subject: [PATCH] [ty] Add benchmark for union of tuples (#18076) ## Summary Add a micro-benchmark for the code pattern observed in https://github.com/astral-sh/ty/issues/362. This currently takes around 1 second on my machine. ## Test Plan ```bash cargo bench -p ruff_benchmark -- 'ty_micro\[many_tuple' --sample-size 10 ``` --- crates/ruff_benchmark/benches/ty.rs | 58 ++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/crates/ruff_benchmark/benches/ty.rs b/crates/ruff_benchmark/benches/ty.rs index 83c58e5e82..c20f35e8c9 100644 --- a/crates/ruff_benchmark/benches/ty.rs +++ b/crates/ruff_benchmark/benches/ty.rs @@ -301,6 +301,62 @@ fn benchmark_many_string_assignments(criterion: &mut Criterion) { }); } +fn benchmark_many_tuple_assignments(criterion: &mut Criterion) { + setup_rayon(); + + criterion.bench_function("ty_micro[many_tuple_assignments]", |b| { + b.iter_batched_ref( + || { + // This is a micro benchmark, but it is effectively identical to a code sample + // observed in https://github.com/astral-sh/ty/issues/362 + setup_micro_case( + r#" + def flag() -> bool: + return True + + t = () + if flag(): + t += (1,) + if flag(): + t += (2,) + if flag(): + t += (3,) + if flag(): + t += (4,) + if flag(): + t += (5,) + if flag(): + t += (6,) + if flag(): + t += (7,) + if flag(): + t += (8,) + if flag(): + t += (9,) + if flag(): + t += (10,) + if flag(): + t += (11,) + + # Perform some kind of operation on the union type + print(1 in t) + "#, + ) + }, + |case| { + let Case { db, .. } = case; + let result = db.check().unwrap(); + assert_eq!(result.len(), 0); + }, + BatchSize::SmallInput, + ); + }); +} + criterion_group!(check_file, benchmark_cold, benchmark_incremental); -criterion_group!(micro, benchmark_many_string_assignments); +criterion_group!( + micro, + benchmark_many_string_assignments, + benchmark_many_tuple_assignments +); criterion_main!(check_file, micro);