Improve performance by avoiding overhead of func +
#58
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.
This change improves the performance of PathKit by avoiding unnecessary cpu and memory costs in
func +
.For the project I am testing on, which uses XcodeGen among others, the cpu and memory costs of
func +
are reduced fairly significantly. Memory use is down from ~15mb to 1.5mb, and these values are consistent across Instruments runes. Here are some before and after screenshots:Memory Use Before
Memory Use After
For CPU, the times vary a little across Instruments runs, but the performance is also improved by an order of magnitude, from ~300ms to ~40ms. Here are some before and after screenshots:
CPU Use Before
CPU Use After
Explanation
func +
runs in O(N) time and space, where N is the number of elements in the path. This means it performs worse for longer paths than shorter paths, which is not an ideal performance trait. For both path arguments, it creates an array of path components, then filters out"."
elements, which is often just a copy, resolves some (but not all)..
references, then joins the results into a new string. For any string with no..
or.
elements, this is all completely unnecessary. This change adds the concept ofdirect
paths, paths with no..
or.
. Now,func +
can avoid all the unnecessary work when the paths are direct.