Skip to content

Conversation

@Erarndt
Copy link
Contributor

@Erarndt Erarndt commented Sep 17, 2025

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
Copy link
Contributor

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

@ccastanedaucf
Copy link
Contributor

Coincidentally #12526 fully gets rid of TargetFinishedTranslator

Copy link
Contributor

@teo-tsirpanis teo-tsirpanis left a 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)
Copy link
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
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
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);

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.

3 participants