Skip to content

Conversation

ToddGrun
Copy link
Contributor

Reduce closure allocations in TokenizerBackedParser.ConfigureSpanContext

Move the closure allocation to it's own method which should slightly reduce allocations when there isn't an existing SpanContextConfig.

Speeedometer run failed for some reason and I don't want to wait another 8 hours to get verify what I'm pretty confident in already.

Test insertion pipeline run (failed): https://dev.azure.com/dnceng/internal/_build/results?buildId=2795099&view=results

…eSpanContext

Move the closure allocation to it's own method which should slightly reduce allocations when there isn't an existing SpanContextConfig.

Will do a speedometer run with this change to make sure it effects things the way I think it does.
@ToddGrun ToddGrun requested a review from a team as a code owner September 17, 2025 18:32
Copy link
Member

@DustinCampbell DustinCampbell left a comment

Choose a reason for hiding this comment

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

Move the closure allocation to it's own method which should slightly reduce allocations when there isn't an existing SpanContextConfig.

I must be missing something here. Instead of creating a closure directly, the code now calls a method that creates a closure. I'm not seeing any new conditions where the closure would be created less often.

: GetNewSpanContextConfigAction(config, SpanContextConfig);
InitializeContext();

static SpanContextConfigAction GetNewSpanContextConfigAction(SpanContextConfigActionWithPreviousConfig config, SpanContextConfigAction? prev)
Copy link
Member

Choose a reason for hiding this comment

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

This is uncommon enough that it I think it probably warrants a comment that it needed to avoid allocations.


protected void ConfigureSpanContext(SpanContextConfigActionWithPreviousConfig? config)
{
var prev = SpanContextConfig;
Copy link
Member

Choose a reason for hiding this comment

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

Can this line be kept? It seems a bit harder to read when SpanContextConfig is set to an expression that uses the previous value of SpanContentConfig?

Copy link
Member

@DustinCampbell DustinCampbell left a comment

Choose a reason for hiding this comment

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

I see why this reduces allocations now. I wasn't seeing that the compiler unconditionally creates the closure class outside of the ternary, even if it's not used. I had assumed that we'd get more efficient code, but we don't. 😞

SharpLab

@ToddGrun ToddGrun merged commit 0aba326 into dotnet:main Sep 17, 2025
11 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Sep 17, 2025
@akhera99 akhera99 modified the milestones: Next, 18.0 P1, 18.0 P2 Sep 22, 2025
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