-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Reduce allocations in PSTraceSource.WriteLine(). Part 2 #18246
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
Conversation
src/System.Management.Automation/utils/StructuredTraceSource.cs
Outdated
Show resolved
Hide resolved
|
This is great! There are more places to apply this, like all the |
I suggest doing this in some follow PRs because we need to change dozens of files, which will make it difficult to review this PR. |
src/System.Management.Automation/utils/StructuredTraceSource.cs
Outdated
Show resolved
Hide resolved
|
Leveraging the interpolated string handler for The current change in this PR will reduce more allocations when tracing is enabled, but that's not a common scenario and thus not very interesting (reducing allocation is always good, it's just not as important in this case). Back to the design of the current change, it would require one handler implementation per every logging option, because the handler has built-in assumption of the tracing option to check against. This is obviously not desired. What we want is a single handler implementation that can work for all logging options. To achieve this goal, we need to first do significant refactoring to the existing |
We still do early evaluating of arguments (there is even StringBuilder!). We have reduced boxing with a lot of helpers, but now we can remove them and use a single method, which is much easier and clearer.
I am trying to keep the PR as small as possible.
|
Can you please point me to some examples to prove the bold part? Also, back to the design of the current change, I have concern about having one handler implementation per every logging option. See the 3rd paragraph in my comment #18246 (comment) for details. When |
You can look how
Options:
|
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
Having
This would be most ideal -- being consistent and modern. That will also allow the pattern to be easily copied in other places. The problem is that the saving in allocation will not be obvious when the tracing is disabled (this is the common scenario), which will make the value/cost of this work less attractive :( |
|
Again, I think when tracing is disabled, we ideally should never call to the |
Can you clarify how we could achieve this?
We seem to be in slightly different contexts. :-) I must have made a bad initial description. Now we can remove all these helper methods and use a single method that completely solves the problem. There are still limitations there, but they are not critical since the API is not public.
We still can keep using specific names like WriteLine and have one handler. For this we should have one more optional parameter in the handler and in the methods. What is pattern from the two ones you like more?
The benefit is that the new pattern eliminates allocations and errors like |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
This reverts commit 0bd49eb.
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
PR Summary
Significant refactoring
PSTraceSourceclass.Rationale:
The original code causes significant resource consumption even when tracing is turned off.
Part of the problem was reduced earlier by creating many helper methods. This reduced the allocations when tracing is turned off noticeably, but did not eliminate them all (scope tracing, early argument evaluations).
These problems also limited developers, forcing them to use simple strings instead of displaying really useful information.
Benefits gained:
PSTraceSourcecode:PSTraceWrite()method:PSTraceSourceOptionsenum.ref readonly struct PSTraceScopefor scope tracing.PSTraceSourceclass was updated with nullable annotations.PSTraceWrite()method andPSTraceScopestruct.Reviewers can use debugger and the follow command:
PR Context
History #10052.
From #10052 (review)
@daxian-dbw The time has come! 😄
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).