-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: cost-free conversion from paths to
&str
(#93)
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`](rust-lang/rust#115443) 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] ```
- Loading branch information
Showing
7 changed files
with
80 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[workspace] | ||
members = [".", "camino-examples"] | ||
members = ["."] | ||
|
||
[package] | ||
name = "camino" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) The camino Contributors | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
// This benchmark is here because criterion has a higher MSRV than camino -- camino-examples is only | ||
// tested on stable, which is good enough. | ||
|
||
use camino::Utf8PathBuf; | ||
use criterion::*; | ||
|
||
fn bench_path(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("Path"); | ||
for i in [10, 100, 1000, 10000] { | ||
let p = "i".repeat(i); | ||
let buf = Utf8PathBuf::from(black_box(p)); | ||
group.bench_with_input(BenchmarkId::new("Utf8PathBuf::as_str", i), &buf, |b, i| { | ||
b.iter(|| { | ||
let _ = black_box(&i).as_str(); | ||
}) | ||
}); | ||
} | ||
} | ||
|
||
criterion_group!(benches, bench_path); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters