`String::from_utf16` would be faster for ascii inputs with a dedicated ascii check
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
- 35/100
- Loại issue
- Tái cấu trúc
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- performance
Hướng nghiên cứu
The slow path is the decode_utf16 loop in from_utf16_units in library/alloc/src/string.rs, and the encode side is EncodeUtf16 layered over Chars in the str iterator code. Start by running the reproduction in the issue to confirm the timings, then read how those paths handle input that is all ASCII. Done means a measured speedup on the ASCII benchmark with the existing UTF-16 tests still passing.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
String::from_utf16 on 1M ASCII code units takes 590 to 690 us. A version that first checks that every unit is ASCII and then narrows them to bytes takes about 135 us (roughly 5x).
The other direction looks the same: str::encode_utf16().collect() takes about 530 us, checking is_ascii() and widening the bytes about 34 us (15x).
Both go through one char at a time. For decoding that is the decode_utf16 loop in from_utf16_units (library/alloc/src/string.rs), for encoding it is EncodeUtf16 on top of Chars. Neither has a path for input that is all ASCII.
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 from_utf16_narrowing(v: &[u16]) -> String {
if v.iter().all(|&u| u < 0x80) {
let bytes: Vec<u8> = v.iter().map(|&u| u as u8).collect();
// ASCII is valid UTF-8
return unsafe { String::from_utf8_unchecked(bytes) };
}
String::from_utf16(v).unwrap()
}
fn encode_utf16_widening(s: &str) -> Vec<u16> {
if s.is_ascii() {
return s.bytes().map(u16::from).collect();
}
s.encode_utf16().collect()
}
fn main() {
let text = "lorem ipsum dolor sit amet ".repeat((1 << 20) / 27);
let units: Vec<u16> = text.encode_utf16().collect();
assert_eq!(from_utf16_narrowing(&units), text);
assert_eq!(encode_utf16_widening(&text), units);
println!("{} ASCII code units", units.len());
let std_dec = micros(|| String::from_utf16(black_box(&units)).unwrap().len());
let own_dec = micros(|| from_utf16_narrowing(black_box(&units)).len());
println!("String::from_utf16 {std_dec:>8.1} us");
println!(" check + narrow {own_dec:>8.1} us ({:.1}x)", std_dec / own_dec);
let std_enc = micros(|| black_box(text.as_str()).encode_utf16().collect::<Vec<u16>>().len());
let own_enc = micros(|| encode_utf16_widening(black_box(&text)).len());
println!("encode_utf16().collect() {std_enc:>8.1} us");
println!(" check + widen {own_enc:>8.1} us ({:.1}x)", std_enc / own_enc);
}
- 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