Skip to content
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

Investigate getting rid of locker object on LazyFormattedBuildEventArgs #6002

Closed
Tracked by #6940
KirillOsenkov opened this issue Jan 4, 2021 · 1 comment · Fixed by #7010
Closed
Tracked by #6940

Investigate getting rid of locker object on LazyFormattedBuildEventArgs #6002

KirillOsenkov opened this issue Jan 4, 2021 · 1 comment · Fixed by #7010
Assignees
Labels
Area: Logging backlog performance Performance-Scenario-Build This issue affects build performance. Priority:2 Work that is important, but not critical for the release size:1 triaged
Milestone

Comments

@KirillOsenkov
Copy link
Member

We should strongly consider getting rid of https://source.dot.net/#Microsoft.Build.Framework/LazyFormattedBuildEventArgs.cs,e983fe598844d129,references

to reduce allocations. We allocate hundreds of thousands of these and we should just throw away the old guidance and lock on the event args object itself, or see if the lock is even necessary there (maybe there's a lock up the stack already).

@KirillOsenkov KirillOsenkov added performance needs-triage Have yet to determine what bucket this goes in. Area: Logging Performance-Scenario-Build This issue affects build performance. labels Jan 4, 2021
@benvillalobos benvillalobos removed the needs-triage Have yet to determine what bucket this goes in. label Jan 6, 2021
@benvillalobos benvillalobos added this to the Backlog milestone Jan 6, 2021
@panopticoncentral panopticoncentral added the Priority:2 Work that is important, but not critical for the release label Mar 23, 2021
@ladipro ladipro added the size:1 label Oct 12, 2021
@ladipro
Copy link
Member

ladipro commented Oct 12, 2021

Labeling with size:1 as the cost of the initial investigation. Will open a follow-up issue if more work is identified.

@ladipro ladipro self-assigned this Oct 27, 2021
@ladipro ladipro removed this from the Backlog milestone Nov 2, 2021
rainersigwald pushed a commit that referenced this issue Nov 23, 2021
Fixes #6002 

### Context

As pointed out in #6002, the locker object is not necessarily needed in `LazyFormattedBuildEventArgs`. It guarantees that formatting the message and nulling out the `arguments` field happens atomically to prevent double-formatting. The rest of the code running under the lock is idempotent.

### Changes Made

- Removed the locker object.
- Simply removed locking from idempotent code. Compensated by making the `message` field volatile to guarantee thread safety. The `if (RawMessage == null) ... RawMessage =` pattern falls into this case.
- Implemented formatting atomicity by replacing `object[] arguments` with `object argumentsOrFormattedMessage`. The lazy `Message` getter reads arguments from this field and writes the formatted message into the same field. The run-time type determines if the message is already formatted or not.

### Testing

Existing unit tests, manual log and binlog smoke test.

### Notes

- The locker object is part of public surface so technically this is a breaking change.
- After `LazyFormattedBuildEventArgs.Message` runs the object may reference two strings - the original unformatted message and the newly created formatted one. This may have memory impact if the object is kept alive after the message is formatted.
@ladipro ladipro added this to the VS 17.1 milestone Dec 8, 2021
@AR-May AR-May added the triaged label Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Logging backlog performance Performance-Scenario-Build This issue affects build performance. Priority:2 Work that is important, but not critical for the release size:1 triaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants