Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Performance fixes, wave 1. #864

Merged
merged 14 commits into from
Apr 18, 2019
Merged

Performance fixes, wave 1. #864

merged 14 commits into from
Apr 18, 2019

Conversation

Dmitry-Matveev
Copy link
Member

@Dmitry-Matveev Dmitry-Matveev commented Apr 11, 2019

  • Swtich from Diagnostics adapters to listeners from @lmolkova (was already merged into develop, this commit is therefore ignored by PR);
  • Switch from Initialize() to InitializeInsturmenationKey() to prevent second round of Telemetry Initializers
  • GetUri updates, 2x gain (this bug is still an issue, though)
  • SetHeaderKeyValue updates, 10x gain
  • Use deprecated Properties for a while again to avoid second concurrent dictionary initialization for GlobalProperties.
  • Test updates, including awesome contribution from @lmolkova to fix W3C correlation the aforementioned changes broke

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated with one line description of the fix, and a link to the original issue.

{
if (HeaderMatchesKey(currentHeaders[index], key))
{
currentHeaders[index] = string.Concat(key, "=", value);
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: Tried making it a variable, IL adds an extra allocation for that, so kept in three places on purpose.

Copy link
Member

Choose a reason for hiding this comment

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

That sounds suspicious. Are you counting locals (stack slots) from IL? Those aren't the kind of allocations (GC heap) to be worried about.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. ldloc.1 and similar. Memory hit here is fine, i'd like to avoid extra instructions if possible :)
Not to the point of one allocation of course, but no allocation simply sounded better than "an allocation" upon review of IL here. Not a problem, can move to a variable if you feel it's absolutely necessary.

Copy link
Member

Choose a reason for hiding this comment

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

IL instruction count isn't really that important. Sure, fewer is generally better (you can fit more on a cache line) but, of course, it's the generated machine code that really matters. An IL "slot" is required for local variables (and args) and they'll usually map to a register or a BP-relative memory location. We might call the latter "stack allocation", but it's nowhere near the same as a GC allocation. In this case, I'd expect that the total IL instruction count for this method when hoisting the expression to a local variable (common subexpression elimination) will be less overall -- because of the saved duplicate code. I doubt the C# compiler will do the hoisting itself -- and if it did, it'd use a stack slot.

This is all a long explanation for why you shouldn't worry about IL slots for perf.

For this case, I don't mind how you write it. I just wanted to explain why "IL adds an extra allocation" raised my Spock-like eyebrow and, hopefully, educate the team :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool! Great to know!

{
telemetry.Context.GlobalProperties.Add(AspNetCoreEnvironmentPropertyName, environment.EnvironmentName);
telemetry.Context.Properties.Add(AspNetCoreEnvironmentPropertyName, environment.EnvironmentName);
Copy link
Contributor

Choose a reason for hiding this comment

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

If we can avoid touching properties at all in default scenarios, then that'll also save the perf hit from instantiating the Properties Conc.Dictionary.

Can you try if changes similar to this(https://github.com/Microsoft/ApplicationInsights-dotnet/pull/929/files) can be applied he as well? We'll need to modify all telemetry types to have this special internal field.

https://github.com/Microsoft/ApplicationInsights-dotnet/pull/929/files

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, don't we stamp each request with Pre-Aggregation's "_MS.AggregatedByMetricExtractors" or similar? Which dictionary does it use - we should just use that one since it's initialized for each request anyway.

Copy link
Contributor

@cijothomas cijothomas left a comment

Choose a reason for hiding this comment

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

Have left a comment about avoiding .Properties completely

@Dmitry-Matveev Dmitry-Matveev merged commit 4e05f2a into develop Apr 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants