Skip to content

Commit e586f6d

Browse files
authored
[ty] Benchmarks for problematic implicit instance attributes cases (#20133)
## Summary Add regression benchmarks for the problematic cases in astral-sh/ty#758. I'd like to merge this before #20128 to measure the impact (local tests show that this will "solve" both cases).
1 parent 76a6b7e commit e586f6d

File tree

1 file changed

+51
-3
lines changed
  • crates/ruff_benchmark/benches

1 file changed

+51
-3
lines changed

crates/ruff_benchmark/benches/ty.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,6 @@ fn benchmark_complex_constrained_attributes_2(criterion: &mut Criterion) {
450450
r#"
451451
class C:
452452
def f(self: "C"):
453-
self.a = ""
454-
self.b = ""
455-
456453
if isinstance(self.a, str):
457454
return
458455
@@ -466,6 +463,56 @@ fn benchmark_complex_constrained_attributes_2(criterion: &mut Criterion) {
466463
return
467464
if isinstance(self.b, str):
468465
return
466+
if isinstance(self.b, str):
467+
return
468+
if isinstance(self.b, str):
469+
return
470+
471+
self.a = ""
472+
self.b = ""
473+
"#,
474+
)
475+
},
476+
|case| {
477+
let Case { db, .. } = case;
478+
let result = db.check();
479+
assert_eq!(result.len(), 0);
480+
},
481+
BatchSize::SmallInput,
482+
);
483+
});
484+
}
485+
486+
fn benchmark_complex_constrained_attributes_3(criterion: &mut Criterion) {
487+
setup_rayon();
488+
489+
criterion.bench_function("ty_micro[complex_constrained_attributes_3]", |b| {
490+
b.iter_batched_ref(
491+
|| {
492+
// This is a regression test for https://github.com/astral-sh/ty/issues/758
493+
setup_micro_case(
494+
r#"
495+
class GridOut:
496+
def __init__(self: "GridOut") -> None:
497+
self._buffer = b""
498+
499+
def _read_size_or_line(self: "GridOut", size: int = -1):
500+
if size > self._position:
501+
size = self._position
502+
pass
503+
if size == 0:
504+
return bytes()
505+
506+
while size > 0:
507+
if self._buffer:
508+
buf = self._buffer
509+
self._buffer = b""
510+
else:
511+
buf = b""
512+
513+
if len(buf) > size:
514+
self._buffer = buf
515+
self._position -= len(self._buffer)
469516
"#,
470517
)
471518
},
@@ -668,6 +715,7 @@ criterion_group!(
668715
benchmark_tuple_implicit_instance_attributes,
669716
benchmark_complex_constrained_attributes_1,
670717
benchmark_complex_constrained_attributes_2,
718+
benchmark_complex_constrained_attributes_3,
671719
benchmark_many_enum_members,
672720
);
673721
criterion_group!(project, anyio, attrs, hydra, datetype);

0 commit comments

Comments
 (0)