Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Reuse static delegate rather than create a new one each call - #12542

#12542
Closed
Erarndt wants to merge 1 commit into
dotnet:maindotnet/msbuild:mainfrom
Erarndt:dev/erarndt/targetFinishedTranslatorDelegateErarndt/msbuild:dev/erarndt/targetFinishedTranslatorDelegateCopy head branch name to clipboard
Closed

Reuse static delegate rather than create a new one each call#12542
Erarndt wants to merge 1 commit into
dotnet:maindotnet/msbuild:mainfrom
Erarndt:dev/erarndt/targetFinishedTranslatorDelegateErarndt/msbuild:dev/erarndt/targetFinishedTranslatorDelegateCopy head branch name to clipboard

Conversation

@Erarndt

@Erarndt Erarndt commented Sep 17, 2025

Copy link
Copy Markdown
Contributor

Fixes #

Context

The constructors for LogMessagePacket allocate a new TargetFinishedTranslator delegate 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.

image

Changes Made

Testing

Notes

Copilot AI review requested due to automatic review settings September 17, 2025 20:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 TargetFinishedTranslator delegate
  • Updated both constructors to use the cached delegate instead of creating new instances

@ccastanedaucf

Copy link
Copy Markdown
Contributor

Coincidentally #12526 fully gets rid of TargetFinishedTranslator

@teo-tsirpanis teo-tsirpanis left a comment

Copy link
Copy Markdown
Contributor

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
: base(nodeBuildEvent, targetFinishedTranslator)
: base(nodeBuildEvent, TranslateTargetFinishedEvent)

/// </summary>
private LogMessagePacket(ITranslator translator)
: base(translator, new TargetFinishedTranslator(TranslateTargetFinishedEvent))
: base(translator, targetFinishedTranslator)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
: base(translator, targetFinishedTranslator)
: base(translator, TranslateTargetFinishedEvent)

Comment on lines +22 to +23
private static readonly TargetFinishedTranslator targetFinishedTranslator = new TargetFinishedTranslator(TranslateTargetFinishedEvent);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static readonly TargetFinishedTranslator targetFinishedTranslator = new TargetFinishedTranslator(TranslateTargetFinishedEvent);

@YuliiaKovalova

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.