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

Add optimisation step for Invariant Arrays #296

Closed
benaadams opened this issue Apr 8, 2018 · 1 comment
Closed

Add optimisation step for Invariant Arrays #296

benaadams opened this issue Apr 8, 2018 · 1 comment
Labels

Comments

@benaadams
Copy link
Member

Classes that don't pass their arrays out by reference; and only directly instantiate them (new T[n]) (rather than setting the array field to something passed by argument or result of external call, could have their element stores changed from covariant stores to invariant stores.

From https://github.com/dotnet/corefx/issues/23689#issuecomment-378283974

@Suchiman I like your idea of enabling this optimization without requiring code changes everywhere! We can start with implementing it in ILLinker to measure the benefit and prove its usefulness, and maybe later in Roslyn. We have taken the same journey for SkipLocalsInit. The implementation in ILLinker can:

  • Do escape analysis to identify all arrays that are just used as internal implementation detail (that do not escape outside the assembly - assuming there is no private reflection) and that do not use array covariance
  • Use alternative more efficient sequence for storing elements into them.

For example the store in coreclr List<T>.Add(T)

public partial class List<T>
{
    private T[] _items;

    public void Add(T item)
    {
        _version++;
        T[] array = _items;
        int size = _size;
        if ((uint)size < (uint)array.Length)
        {
            _size = size + 1;
            array[size] = item; // array element store
        }
        else
        {
            AddWithResize(item);
        }
    }
}

Could have the il changed from something like

ldloc.0
ldloc.1
ldarg.1
stelem     !T    // Covariant store

To something like

ldloc.0
ldloc.1
readonly.
ldelema    !T
ldarg.1
stobj      !T    // Invariant store

/cc @jkotas @mjp41 @VSadov @jaredpar

@marek-safar marek-safar changed the title Option: Use Invariant Arrays Add optimisation step Invariant Arrays Oct 3, 2019
@marek-safar marek-safar changed the title Add optimisation step Invariant Arrays Add optimisation step for Invariant Arrays Oct 3, 2019
@vitek-karas
Copy link
Member

So far we've not been using illink as a performance optimization tools. While some of the transforms it does might have performance effect, their main goal is pretty much always size reduction.

If the optimization can be done within a single method's body, then I would say it belongs to the compiler (Roslyn).
If the optimization needs a global program view, then that's something we don't have a tool for yet. Potential places:

  • JIT
  • NativeAOT compiler (but that's really JIT as well)
  • Global IL optimization tool - this could be added to illink, or it could be a separate new tool.

I think that if we want to pursue this, the right place to start would be to start the discussion in the context of roslyn or JIT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants