Performance: skip the syntax-tree rewrite walk for files no rule can affect

Đã đóng
#736 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
30/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php
Lĩnh vực
performance

Hướng nghiên cứu

Start from SyntaxTreeRewriter::transform(), which walks every transformed file, and NodeRewriterDispatcher, which resolves rules per node class through a closure into getRulesFor(). The proposal adds an optional way for a NodeRewriter (e.g. MagicConstantTransformer) to declare cheap source substrings, then skips the walk when none match. Measure with composer test:performance:transformation, JIT off; done means the rewrite ratio drops while a rule declaring no substrings still always applies.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Enhancement Performance

Problem

SyntaxTreeRewriter walks the whole syntax tree of every transformed file, even when none of the enabled rules can match it. The visitor also finds the rules for each node through a closure call into SyntaxTreeRewriter::getRulesFor().

Measurement

composer test:performance:transformation (JIT off), php-parser corpus of 271 files:

ms
parse (the main cost of a cache miss) ~252
plain walk with a no-op visitor ~26
SyntaxTreeRewriter, default rules ~42 (ratio 1.54)
SyntaxTreeRewriter, all rules ~44 (ratio 1.67)
str_contains pre-check over all sources 0.4
  • Only 14 of 271 files contain __DIR__, __FILE__ or getFileName, which the default MagicConstantTransformer rule looks for. Only 16 contain include or require, and 78 contain new.
  • Caching the rule list per node class inside NodeRewriterDispatcher alone moved the ratios from 1.53 to 1.42–1.47 (default rules) and from 1.70 to 1.49–1.58 (all rules).

Proposal

  • Let a rule declare cheap source substrings that must appear for it to apply, through an optional method or interface. Keep the NodeRewriter contract compatible for custom rules: a rule without substrings always applies.
  • SyntaxTreeRewriter::transform() skips the walk when no enabled rule's substrings appear in the source, and walks with only the rules whose substrings do.
  • Inline the rule lookup per node class in NodeRewriterDispatcher.

Estimate

Cache misses only. With the default rules about 95% of files skip the walk, which saves about 13% of parse plus rewrite time on cache warm-up and debug-mode re-transformations. The rule lookup alone saves 5–8% of the rewriter time. Medium risk (the public NodeRewriter API is touched), medium effort, best done before 4.0.0.

Ngôn ngữ chính
PHP
Star
1.7k
Fork
166
Merge trung bình
1 ngày 5 giờ
Pull request đã merge (30 ngày)
30

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của goaop/framework

Tất cả issue của goaop/framework

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.