-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Reuse static delegate rather than create a new one each call #12542
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
base: main
Are you sure you want to change the base?
Reuse static delegate rather than create a new one each call #12542
Conversation
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.
Pull Request Overview
This PR optimizes memory allocations in the LogMessagePacket class by replacing repeated delegate instantiations with a single static cached delegate. The change reduces ~22MB of allocations by reusing the same TargetFinishedTranslator delegate instance across all constructor calls instead of creating a new one each time.
Key Changes
- Added a static readonly field to cache the
TargetFinishedTranslatordelegate - Updated both constructors to use the cached delegate instead of creating new instances
|
Coincidentally #12526 fully gets rid of |
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.
I think that Roslyn since some time, caches static method group delegates by itself.
| /// </summary> | ||
| internal LogMessagePacket(KeyValuePair<int, BuildEventArgs>? nodeBuildEvent) | ||
| : base(nodeBuildEvent, new TargetFinishedTranslator(TranslateTargetFinishedEvent)) | ||
| : base(nodeBuildEvent, targetFinishedTranslator) |
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.
| : base(nodeBuildEvent, targetFinishedTranslator) | |
| : base(nodeBuildEvent, TranslateTargetFinishedEvent) |
| /// </summary> | ||
| private LogMessagePacket(ITranslator translator) | ||
| : base(translator, new TargetFinishedTranslator(TranslateTargetFinishedEvent)) | ||
| : base(translator, targetFinishedTranslator) |
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.
| : base(translator, targetFinishedTranslator) | |
| : base(translator, TranslateTargetFinishedEvent) |
| private static readonly TargetFinishedTranslator targetFinishedTranslator = new TargetFinishedTranslator(TranslateTargetFinishedEvent); | ||
|
|
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.
| private static readonly TargetFinishedTranslator targetFinishedTranslator = new TargetFinishedTranslator(TranslateTargetFinishedEvent); |
Fixes #
Context
The constructors for
LogMessagePacketallocate a newTargetFinishedTranslatordelegate for each call. The delegate function is static already, so this can be cached to avoid the repeated allocations. There are ~22MB of allocations total from two separate paths.Changes Made
Testing
Notes