-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace consts with static readonly #428
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another reason this is a good idea, is that const is determined at compile time when being referenced elsewhere, where as 'static readonly' will cause the value to be referenced at runtime instead.
Let's look closer at those extra allocations, I don't see where they would be coming from. |
@@ -18,8 +15,8 @@ namespace Microsoft.Extensions.DependencyInjection | |||
/// </summary> | |||
public static class ServiceCollectionExtensions | |||
{ | |||
private const string ActivitySourceName = "OmexActivitySource"; | |||
private const string ActivitySourceVersion = "1.0.0.0"; | |||
private static readonly string s_activitySourceName = "OmexActivitySource"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is already static so I expect these shouldn't be changed.
@@ -11,10 +11,10 @@ namespace Microsoft.Omex.Extensions.Logging.Internal.Replayable | |||
{ | |||
internal static class ReplayebleActivityExtensions | |||
{ | |||
private const string ReplayableLogKey = "ReplayableLogKey"; | |||
private static readonly string s_replayableLogKey = "ReplayableLogKey"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class is already static
@@ -14,15 +14,15 @@ internal static class Diagnostics | |||
/// <remarks> | |||
/// Changing this name will mean that consumers might miss telemetry events | |||
/// </remarks> | |||
internal const string DiagnosticListenerName = "Microsoft.Omex.Extensions.Services.Remoting"; | |||
internal static readonly string DiagnosticListenerName = "Microsoft.Omex.Extensions.Services.Remoting"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class is already static
@@ -29,7 +29,7 @@ public static class OmexRemotingHeadersExtensions | |||
/// W3C standard for trace context parent 'traceparent', so we've added omex to avoid conflicts if support would be added in future | |||
/// link: https://www.w3.org/TR/trace-context/ | |||
/// </remarks> | |||
private const string TraceParentHeaderName = "omex-traceparent"; | |||
private static readonly string s_traceParentHeaderName = "omex-traceparent"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class is static
I take it that the issue is that as transient instances get created and destroyed as part of their DI lifecycle, the const values are being repeatedly allocated. |
They shouldn't be, CLI ensures that each string constant exists as a singular object (though you can trick it if you try hard). I was talking to Andrey separately, profiler detects multiple allocations of some strings but I'm sure this PR won't fix it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll block this to prevent accidental merging.
Using
const
results in a new allocation each time it's used. The most common exception would be health check markers we have that used each time we mark activity as healthcheck or check it.This PR replaces
const
withstatic readonly
to avoid additional allocations.