Add a small speed up to PathDecanonicalized#2718
Open
elliotgoodrich wants to merge 1 commit intoninja-build:masterfrom
Open
Add a small speed up to PathDecanonicalized#2718elliotgoodrich wants to merge 1 commit intoninja-build:masterfrom
PathDecanonicalized#2718elliotgoodrich wants to merge 1 commit intoninja-build:masterfrom
Conversation
digit-google
approved these changes
Jan 25, 2026
Contributor
digit-google
left a comment
There was a problem hiding this comment.
Thanks. great PR. @jhasse please take a look
a2eaf75 to
0028139
Compare
elliotgoodrich
commented
Feb 8, 2026
src/graph.cc
Outdated
| c = static_cast<char*>(memchr(c, '/', end - c)); | ||
|
|
||
| // If `memchr` returns null then there are too many bits in `slash_bits`, | ||
| // which happens when we pass `~0` in `build.cc` instead of calculating it. |
Contributor
Author
There was a problem hiding this comment.
Found
Lines 997 to 1001 in cc60300
slash_bits is calculated correctly here.
Avoid a mandatory allocation in `PathDecanonicalized` by taking an output
`std::string*` to append to. Improve its performance by swapping
`strchr` with `std::string::find`, which can take advantage of knowing
the length of the string.
We can also return as soon as we run out of forwardslashes that need to be
turned into backslashes, instead of iterating through all
forwardslashes. If backslashes are the common path separator in ninja
build files on Windows, it may be worthwhile canonicalizing paths into
backslashes-only, which would now allow us to skip the loop entirely in
`PathDecanonicalized` in the case of paths with no forwardslashes.
Rename this function to `AppendPathDecanonicalized` to reflect its new
functionality.
Running `manifest_parser_perftest.exe` on Windows gives initial timings
of:
min 549ms max 613ms avg 573.1ms
and timings after this change of:
min 534ms max 597ms avg 559.9ms
which is a 2-3% improvement.
0028139 to
dbd8e7e
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Avoid a mandatory allocation in
PathDecanonicalizedby taking an outputstd::string*to append to. Improve its performance by swappingstrchrwithstd::string::find, which can take advantage of knowing the length of the string.We can also return as soon as we run out of forwardslashes that need to be turned into backslashes, instead of iterating through all forwardslashes.
If backslashes are the common path separator in ninja build files on Windows, it may be worthwhile canonicalizing paths into backslashes-only, which would now allow us to skip the loop entirely in
PathDecanonicalizedin the case of paths with no forwardslashes.Rename this function to
AppendPathDecanonicalizedto reflect its new functionality.Running
manifest_parser_perftest.exeon Windows gives initial timings of:and timings after this change of:
which is a 2-3% improvement.