Serializer: `write_escaped` is O(n²) when text doesn't contain `<`, `>` or `"`

Đang mở
#810 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ó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
performance

Hướng nghiên cứu

Start in html5ever/src/serialize/mod.rs at HtmlSerializer::write_escaped, then build and run the html2html example with the reproduction in the issue. Compare timings as the input grows for &amp; and &nbsp; versus &amp;<; done means the problematic inputs no longer show the reported quadratic scaling.

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

Mô tả

HtmlSerializer::write_escaped finds the next byte to escape with two memchr searches (introduced by #740):

https://github.com/servo/html5ever/blob/6fcf956f59f54f0455464e6cd48fa4a69b9e8706/html5ever/src/serialize/mod.rs#L111-L114

If the text doesn't have an <, > or ", every & or non-breaking space will cause a full scan of the remaining text.

It's pretty common for text to contain &amp; and &nbsp; without <s or >s.

Repro using the html2html example:

cargo build --release -p markup5ever_rcdom --example html2html

TIMEFORMAT='%R s'

for text in '&amp;' '&amp;<' '&nbsp;'; do
  for count in 100000 200000 400000; do
    yes "$text" | head -n "$count" | tr -d '\n' > input.html
    echo -n "$text x $count: "
    time target/release/examples/html2html < input.html > /dev/null
  done
done

On a Linode G6 Dedicated machine with 2 vCPUs and 4 GB RAM, I get:

&amp; x 100000: 0.148 s
&amp; x 200000: 0.462 s
&amp; x 400000: 1.665 s
&amp;< x 100000: 0.096 s
&amp;< x 200000: 0.197 s
&amp;< x 400000: 0.376 s
&nbsp; x 100000: 0.241 s
&nbsp; x 200000: 0.839 s
&nbsp; x 400000: 3.492 s
Ngôn ngữ chính
Rust
Star
2.7k
Fork
303
Merge trung bình
17 giờ 29 phút
Pull request đã merge (30 ngày)
4

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 servo/html5ever

Tất cả issue của servo/html5ever

Issue tương tự

Thêm issue về Rust

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.