Skip to content
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

Avoid static members in generic types #8526

Merged
merged 1 commit into from
Jul 6, 2023
Merged

Conversation

pentp
Copy link
Contributor

@pentp pentp commented Jul 6, 2023

Static members in generic types are expensive, especially fields in singleton classes.

Microsoft Reviewers: Open in CodeFlow

@@ -220,8 +220,8 @@ public FSharpValueOption<T> DeepCopy(FSharpValueOption<T> input, CopyContext con
[RegisterSerializer]
public class FSharpChoiceCodec<T1, T2> : IFieldCodec<FSharpChoice<T1, T2>>, IDerivedTypeCodec
{
private static readonly Type ElementType1 = typeof(T1);
private static readonly Type ElementType2 = typeof(T2);
private readonly Type ElementType1 = typeof(T1);
Copy link
Member

Choose a reason for hiding this comment

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

Should we inline these instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inlining typeof(T) needs to do complex dictionary lookups (similar to static fields) in generic types with reference type parameters. An instance field access is always fast, regardless of type parameters.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, that sounds reasonable to me

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if there are places where we should lift typeof(T) to fields, then.

@ReubenBond ReubenBond merged commit 57bcf05 into dotnet:main Jul 6, 2023
19 checks passed
@pentp pentp deleted the generic-throws branch July 6, 2023 20:24
@github-actions github-actions bot locked and limited conversation to collaborators Dec 2, 2023
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.

2 participants