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

Speed up Import(FieldInfo) when debugging #528

Closed
wants to merge 1 commit into from

Conversation

CreateAndInject
Copy link
Contributor

I already reported this issue to Microsoft, see here

@CreateAndInject
Copy link
Contributor Author

CreateAndInject commented Oct 1, 2023

static void Main()
{
    var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
    var module = typeof(object).Module;
    Stopwatch sw = Stopwatch.StartNew();
    foreach (var type in module.GetTypes())
    {
        foreach (var field in type.GetFields(bindingFlags))
            module.ResolveField(field.MetadataToken);
    }
    Console.WriteLine(sw.Elapsed.TotalSeconds.ToString("f1"));
    Console.ReadKey();
}
- ResolveField ResolveMethod
Debug(EnableJustMyCode .NET3.5) 12.4s 0.1s
Debug(DisableJustMyCode .NET3.5) 12.4s 0.1s
Debug(EnableJustMyCode .NET4.8/5/6/7) 0.1s 0.1s
Debug(DisableJustMyCode .NET4.8/5/6/7) 82.4s 0.1s
Start Without Debugging(All.NET) 0.1s 0.1s

@@ -650,7 +650,7 @@ public Importer(ModuleDef module, ImporterOptions options, GenericParamContext g
try {
// Get the original field def in case the declaring type is a generic
// type instance and the field uses a generic type parameter.
origField = fieldInfo.Module.ResolveField(fieldInfo.MetadataToken);
origField = fieldInfo.DeclaringType is { IsGenericType: true } ? fieldInfo.Module.ResolveField(fieldInfo.MetadataToken) : fieldInfo;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this code exactly identical to the original code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so, typeof(C1<>.C2).IsGenericType is true, so if IsGenericType is false, I don't think ResolveField can return a different value

public class C1<T>
{
    public class C2
    {
        public T Field;
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I can only accept this PR if it's guaranteed to work the same as before.

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.

2 participants