`String::from_utf8_lossy` is 6–19x slower than str::from_utf8 on valid ASCII
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
- 45/100
Hướng nghiên cứu
Start with Utf8Chunks::next in library/core/src/str/lossy.rs, which checks bytes one at a time, and compare it with the word-at-a-time ASCII skip in library/core/src/str/validations.rs. Run the repro in the issue to confirm the ratio on 1 MiB of ASCII versus German and Russian text. Done means the ASCII case gets close to str::from_utf8 speed while the German and Russian timings and the existing lossy tests stay unchanged.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
On 1 MiB of valid ASCII, String::from_utf8_lossy takes about 260 µs while str::from_utf8 takes about 17 µs, although both validate the whole input and return it borrowed. from_utf8 skips ASCII a word at a time (library/core/src/str/validations.rs), whereas Utf8Chunks::next in library/core/src/str/lossy.rs checks byte by byte. On German or Russian text the two are equally fast, so only the ASCII fast path is missing, and everything built on utf8_chunks() pays the same cost.
Repro:
use std::hint::black_box;
use std::time::Instant;
fn micros<R>(mut f: impl FnMut() -> R) -> f64 {
let mut best = f64::MAX;
for _ in 0..5 {
let t = Instant::now();
for _ in 0..50 {
black_box(f());
}
best = best.min(t.elapsed().as_secs_f64() / 50.0);
}
best * 1e6
}
fn main() {
const LEN: usize = 1 << 20;
let inputs = [
("ascii", "lorem ipsum dolor sit amet ".repeat(LEN / 27)),
("german", "Größe über Straße, schön für Käufer ".repeat(LEN / 42)),
("russian", "Привет мир, это проверка строки ".repeat(LEN / 58)),
];
println!("{:<10} {:>14} {:>22} {:>8}", "1 MiB", "from_utf8 (us)", "from_utf8_lossy (us)", "ratio");
for (name, text) in &inputs {
let bytes = text.as_bytes();
let strict = micros(|| std::str::from_utf8(black_box(bytes)).unwrap().len());
let lossy = micros(|| String::from_utf8_lossy(black_box(bytes)).len());
println!("{name:<10} {strict:>14.1} {lossy:>22.1} {:>7.1}x", lossy / strict);
}
}
- Ngôn ngữ chính
- Rust
- Star
- 122k
- Fork
- 16.7k
- Merge trung bình
- 2 ngày 16 giờ
- Pull request đã merge (30 ngày)
- 515
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của rust-lang/rust
-
Remove unnecessary parentheses when suggesting to remove '&' from a patternCó thể đã có người làm @BartSimpson001 đã nhận 1 ngày trước. Đang mởA-diagnostics D-invalid-suggestion T-compiler
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Windows `File::set_permissions` silently does nothing when clearing `readonly` leaves `FileAttributes == 0`Có thể đã có người làm @ChrisDenton đã nhận 11 ngày trước. Đang mởA-filesystem C-bug O-windows T-libs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
A-run-make relnotes relnotes-needs-review relnotes-tracking-issue T-lang T-libs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
A-LLVM O-Arm O-UEFI O-x86_64 relnotes relnotes-needs-review relnotes-tracking-issue T-compiler
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
rust-lang/rust#165504 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
relnotes relnotes-tracking-issue T-libs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của rust-lang/rust
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
bytecodealliance/wrpc#1785 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
2 advisory ignore entries no longer match anything (`quick-xml` already patched in `Cargo.lock`)Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
longbridge/nsh#2758 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
antithesishq/bombadil#473 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
bench/api.sh exec test fails with curl exit 52, and --ignore-failure still records the timingsĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
libracoder/kiri#669 ·
Maintainer thường phản hồi trong vòng 1 ngày