Reuse static delegate rather than create a new one each call - #12542
#12542Reuse static delegate rather than create a new one each call#12542Erarndt wants to merge 1 commit intodotnet:maindotnet/msbuild:mainfrom Erarndt:dev/erarndt/targetFinishedTranslatorDelegateErarndt/msbuild:dev/erarndt/targetFinishedTranslatorDelegateCopy head branch name to clipboard
Conversation
There was a problem hiding this comment.
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 |
teo-tsirpanis
left a comment
There was a problem hiding this comment.
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.
| : 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.
| : base(translator, targetFinishedTranslator) | |
| : base(translator, TranslateTargetFinishedEvent) |
| private static readonly TargetFinishedTranslator targetFinishedTranslator = new TargetFinishedTranslator(TranslateTargetFinishedEvent); | ||
|
|
There was a problem hiding this comment.
| private static readonly TargetFinishedTranslator targetFinishedTranslator = new TargetFinishedTranslator(TranslateTargetFinishedEvent); |
|
was addressed in scope of 84e6f07#diff-e90122e9ff329eb52633d20ba6d5d9d3bfebc5020a76eca5d6f32f95d6074be0 |
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