feat(std): Stabilize 'os_str_bytes' feature by epage · Pull Request #115443 · rust-lang/rust (original) (raw)
sunshowers pushed a commit to camino-rs/camino that referenced this pull request
Background:
When I was migrating PathBuf
to Utf8PathBuf
, etc, I found out some regression in our benchmarks. Then I found out as_str
is actually not cost-free as in the older version of rustc there's no way to get the underlying bytes out of an OsStr
until 1.74.0.
In this PR, with the help of OsStr::as_encoded_bytes
was stabilized in 1.74.0, We can perform a cost-free conversion from &OsStr
to &str
with constraint of it's underlying bytes are UTF-8
encoded.
Benchmark:
With the benchmark included in the PR, the time cost is a constant now.
Result:
// String length of 10
osstr to_str/10
time: [5.9769 ns 5.9913 ns 6.0060 ns]
osstr as_encoded_bytes/10
time: [554.90 ps 558.32 ps 562.19 ps]
// String length of 100
osstr to_str/100
time: [6.6113 ns 6.6250 ns 6.6404 ns]
osstr as_encoded_bytes/100
time: [553.18 ps 557.33 ps 561.68 ps]
// String length of 1000
osstr to_str/1000
time: [26.990 ns 27.033 ns 27.086 ns]
osstr as_encoded_bytes/1000
time: [553.66 ps 560.67 ps 570.42 ps]
// String length of 10000
osstr to_str/10000
time: [310.17 ns 310.77 ns 311.32 ns]
osstr as_encoded_bytes/10000
time: [550.98 ps 555.16 ps 559.53 ps]