Skip to content

Use disjoint-set-union for ClassLayout::AreCompatible. #42801

Open
@sandreenko

Description

@sandreenko

In this function:

bool ClassLayout::AreCompatible(const ClassLayout* layout1, const ClassLayout* layout2)

we are iteration over all slots if two layouts have same size and both have GC pointers, it could work slowly (number of fields * number of calls to this function) on some exotic tests, the task is to construct such a test and use DSU to fix the time complexity of the algorithm.

Steps:

  • Create a test where there are 2 structs with the same size, both with GC pointers, with huge number of fields (N = 1000+) and use many operations of copies between them (M = 1000+);
Test template
public struct Str1
{
    public int i1;
    public Object o; // at least one gc field.
    // add many more bytes of fields here.
}

public struct Str2
{
    public int j1;
    public Object o; // at least one gc field.
    // add the same number of bytes here.
}

public static int Test1()
{
    i = 0;

    Str1 str1 = new Str1();
    // add many copies from one struct to another, check that 
    // C# compiler to IL (Roslyn) does not delete them.
    Str2 str2 = Unsafe.As<Str1, Str2>(ref str1); 
    
}

  • measure jit compilation time (the time will include N*M operations in AreCompatible), confirm that it has quadratic time complexity;

  • Add ClassLayout* m_dsuParent to ClassLayout and implement DSU, when you create a new layout using the existing naive algorithm of comparing slots to find if the new layout should be united with any preexisting layout (note: do not compare with a layout a if its m_dsuParent is not equal to a);

  • Change AreCompatible to return layout1->m_dsuParent == layout2->m_dsuParent;

  • Make an optimization to create DSU only when we have calls to AreCompatible (extract InitDSU, add static bool s_DSUinitialized, check it in AreCompatible).

  • Measure JitCompilationTime on System.Private.CoreLib and other tests to understand if the optimization should be merged and used by default

Feel free to ask questions if something is not clear.

Right now we don't have examples where this issue affects JitCompilationTime but I am planning to add more usages of AreCompatible in the future.

category:implementation
theme:throughput
skill-level:beginner
cost:medium
impact:small

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIgood first issueIssue should be easy to implement, good for first-time contributorshelp wanted[up-for-grabs] Good issue for external contributorsoptimization

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions