Parser (parse, incremental reparse from lutra_text edits), outline with UTF-16 ranges and short signatures, folding ranges, 8 MiB parse cap. Snapshot tests for five PHP 8.3 fixtures, proptests (random bytes, reparse equals fresh parse, undo round trip), ignored release timing test. Docs: architecture Syntax section, parity evidence, G4 note. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
3 KiB
Rust
112 lines
3 KiB
Rust
//! Parse timing for a generated 1 MB PHP file (budget: under 50 ms).
|
|
//!
|
|
//! Ignored by default because timings in debug builds mean nothing. Run in
|
|
//! release on an idle machine:
|
|
//! `cargo test --release -p lutra-syntax --test perf -- --ignored --nocapture`.
|
|
|
|
#![allow(
|
|
clippy::unwrap_used,
|
|
clippy::indexing_slicing,
|
|
clippy::string_slice,
|
|
reason = "tests may panic"
|
|
)]
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use lutra_syntax::{Parser, folding, outline};
|
|
use lutra_text::Edit;
|
|
|
|
/// Generates a PHP file of at least `bytes` bytes made of namespaced
|
|
/// classes with properties, a promoted constructor, methods, arrays and
|
|
/// doc comments.
|
|
fn generate(bytes: usize) -> String {
|
|
let mut text = String::from(
|
|
"<?php\n\ndeclare(strict_types=1);\n\nnamespace Bench\\Generated;\n\nuse Bench\\Support\\Collection;\n\n",
|
|
);
|
|
let mut index = 0usize;
|
|
while text.len() < bytes {
|
|
text.push_str(&format!(
|
|
r#"/**
|
|
* Generated class number {index}.
|
|
*/
|
|
final class Service{index} implements \Countable
|
|
{{
|
|
public const int LIMIT = {index};
|
|
|
|
private array $items = ['a' => 1, 'b' => [2, 3], 'c' => "four {index}"];
|
|
|
|
public function __construct(private readonly Collection $collection, protected ?string $name = null)
|
|
{{
|
|
}}
|
|
|
|
public function find(int $id, string ...$tags): ?array
|
|
{{
|
|
foreach ($this->items as $key => $value) {{
|
|
if ($key === $id && \in_array($value, $tags, true)) {{
|
|
return ['id' => $id, 'value' => $value];
|
|
}}
|
|
}}
|
|
return match ($id) {{
|
|
0 => null,
|
|
default => $this->collection->map(fn (int $x): int => $x * {index})->all(),
|
|
}};
|
|
}}
|
|
|
|
public function count(): int
|
|
{{
|
|
return \count($this->items) + self::LIMIT;
|
|
}}
|
|
}}
|
|
|
|
"#
|
|
));
|
|
index += 1;
|
|
}
|
|
text
|
|
}
|
|
|
|
/// The fastest of `runs` timings of `work`.
|
|
fn best_of(runs: usize, mut work: impl FnMut()) -> Duration {
|
|
(0..runs)
|
|
.map(|_| {
|
|
let started = Instant::now();
|
|
work();
|
|
started.elapsed()
|
|
})
|
|
.min()
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "timing; run in release"]
|
|
fn parse_one_megabyte() {
|
|
let text = generate(1024 * 1024);
|
|
let mut parser = Parser::new().unwrap();
|
|
let tree = parser.parse(&text);
|
|
assert!(!tree.has_errors());
|
|
let parse = best_of(5, || {
|
|
parser.parse(&text);
|
|
});
|
|
let outline_time = best_of(5, || {
|
|
outline(&tree);
|
|
});
|
|
let folding_time = best_of(5, || {
|
|
folding(&tree);
|
|
});
|
|
let middle = text.len() / 2;
|
|
let middle = (middle..text.len())
|
|
.find(|offset| text.is_char_boundary(*offset))
|
|
.unwrap();
|
|
let reparse = best_of(5, || {
|
|
parser.reparse(&tree, &[Edit::insert(middle, " ")]).unwrap();
|
|
});
|
|
println!(
|
|
"1 MB PHP ({} bytes, {} top-level items): parse {:?}, reparse after one keystroke {:?}, outline {:?}, folding {:?}",
|
|
text.len(),
|
|
outline(&tree).len(),
|
|
parse,
|
|
reparse,
|
|
outline_time,
|
|
folding_time
|
|
);
|
|
}
|