-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[libc++] Avoid string reallocation in std::filesystem::path::lexically_relative
#152964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc++] Avoid string reallocation in std::filesystem::path::lexically_relative
#152964
Conversation
…ly_relative` ``` Benchmark Time CPU Time Old Time New CPU Old CPU New ------------------------------------------------------------------------------------------------------------------------------------------------ BM_LexicallyRelative/small_path/2 -0.2111 -0.2082 229 181 228 180 BM_LexicallyRelative/small_path/4 -0.2579 -0.2550 455 338 452 337 BM_LexicallyRelative/small_path/8 -0.2643 -0.2616 844 621 838 619 BM_LexicallyRelative/small_path/16 -0.2582 -0.2556 1562 1158 1551 1155 BM_LexicallyRelative/small_path/32 -0.2518 -0.2496 3023 2262 3004 2254 BM_LexicallyRelative/small_path/64 -0.2806 -0.2775 6344 4564 6295 4549 BM_LexicallyRelative/small_path/128 -0.2165 -0.2137 11762 9216 11683 9186 BM_LexicallyRelative/small_path/256 -0.2672 -0.2645 24499 17953 24324 17891 BM_LexicallyRelative/large_path/2 -0.3268 -0.3236 426 287 422 285 BM_LexicallyRelative/large_path/4 -0.3274 -0.3248 734 494 729 492 BM_LexicallyRelative/large_path/8 -0.3586 -0.3560 1409 904 1399 901 BM_LexicallyRelative/large_path/16 -0.3978 -0.3951 2764 1665 2743 1659 BM_LexicallyRelative/large_path/32 -0.3934 -0.3908 5323 3229 5283 3218 BM_LexicallyRelative/large_path/64 -0.3629 -0.3605 10340 6587 10265 6564 BM_LexicallyRelative/large_path/128 -0.3450 -0.3423 19379 12694 19233 12649 BM_LexicallyRelative/large_path/256 -0.3097 -0.3054 36293 25052 35943 24965 ```
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-libcxx Author: Timothy Choi (tinnamchoi) ChangesImproves runtime by around 20 to 40%. Full diff: https://github.com/llvm/llvm-project/pull/152964.diff 2 Files Affected:
diff --git a/libcxx/src/filesystem/path.cpp b/libcxx/src/filesystem/path.cpp
index 9f7dc54fdf156..624cf785577eb 100644
--- a/libcxx/src/filesystem/path.cpp
+++ b/libcxx/src/filesystem/path.cpp
@@ -292,7 +292,9 @@ path path::lexically_relative(const path& base) const {
// return a path constructed with 'n' dot-dot elements, followed by the
// elements of '*this' after the mismatch.
path Result;
- // FIXME: Reserve enough room in Result that it won't have to re-allocate.
+ constexpr size_t ElemSize = 2; // ".."
+ constexpr size_t SeparatorSize = 1; // separator is always a single char
+ Result.__reserve(ElemCount * (ElemSize + SeparatorSize) + SeparatorSize + PP.Path.size());
while (ElemCount--)
Result /= PATHSTR("..");
for (; PP; ++PP)
diff --git a/libcxx/test/benchmarks/filesystem.bench.cpp b/libcxx/test/benchmarks/filesystem.bench.cpp
index dc6b0ac537f7e..f2e596238a922 100644
--- a/libcxx/test/benchmarks/filesystem.bench.cpp
+++ b/libcxx/test/benchmarks/filesystem.bench.cpp
@@ -171,4 +171,25 @@ BENCHMARK_CAPTURE(BM_LexicallyNormal, large_path, getRandomPaths, /*PathLen*/ 32
->Range(2, 256)
->Complexity();
+template <class GenInput>
+void BM_LexicallyRelative(benchmark::State& st, GenInput gen, size_t PathLen) {
+ using fs::path;
+ auto BasePath = gen(st.range(0), PathLen);
+ auto TargetPath = gen(st.range(0), PathLen);
+ benchmark::DoNotOptimize(&BasePath);
+ benchmark::DoNotOptimize(&TargetPath);
+ while (st.KeepRunning()) {
+ benchmark::DoNotOptimize(TargetPath.lexically_relative(BasePath));
+ }
+ st.SetComplexityN(st.range(0));
+}
+BENCHMARK_CAPTURE(BM_LexicallyRelative, small_path, getRandomPaths, /*PathLen*/ 5)
+ ->RangeMultiplier(2)
+ ->Range(2, 256)
+ ->Complexity();
+BENCHMARK_CAPTURE(BM_LexicallyRelative, large_path, getRandomPaths, /*PathLen*/ 32)
+ ->RangeMultiplier(2)
+ ->Range(2, 256)
+ ->Complexity();
+
BENCHMARK_MAIN();
|
philnik777
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically LGTM. Just a few nits.
Forgot to run `clang-format` before.
philnik777
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with the CI green.
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Should've ran `git clang-format HEAD~2` instead.
|
@philnik777 Workflow requires approval again 😅 |
|
Is it "normal" for the CIs to time out? |
Yeah, there are currently some infrastructure issues. |
philnik777
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I completely forgot: Could you add a release note to libcxx/docs/ReleaseNotes/22.rst?
|
@philnik777 Is there anything I can do to move this forward? I recall seeing on Discord that a workaround to the infrastructure problem was deployed around the same time the CI was last run so I'm not sure if that's still the problem here. |
philnik777
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the branch and then this is good to go.
|
@philnik777 This looks fine I think? At first I thought maybe I had some errors caused by upstream changes but seems like it's just infrastructure problems again. I don't have the permissions so I need help merging. |
|
@tinnamchoi Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Improves runtime by around 20 to 40%. (1.3x to 1.7x)