Skip to content

SortedList allocates memory due to boxing of TKey #100406

Closed

Description

@PetrAltman

Description

SortedList<TKey, TValue> with struct TKey allocates memory due to boxing of TKey.

Configuration

Net 8,0

Analysis

Several methods in SortedList (e.g., Add, Remove, IndexOf) verify that the key is not null by invoking ArgumentNullException.ThrowIfNull(key);. ThrowIfNull expects an object parameter, causing TKey to be boxed when it is a struct type. This behavior should be avoided for struct types. It might be beneficial to add a generic overload to ArgumentNullException that does not allocate memory for struct types. This change could be useful as similar scenarios might occur in other generic classes within .NET.

Activity

  1. added
    tenet-performance
    on Mar 28, 2024
  2. ghost added
    needs-area-label
    on Mar 28, 2024
  3. added
    untriaged
    on Mar 28, 2024
  4. added and removed
    needs-area-label
    on Mar 28, 2024
  5. stephentoub commented on Mar 28, 2024

    @stephentoub
    Member

    Are you actually seeing that boxing happening in release with optimized code? The boxing associated with that ThrowIfNull gets optimized away by the JIT.

  6. PetrAltman commented on Mar 28, 2024

    @PetrAltman
    Author

    Oh, I didn't think of JIT could optimize that.

    I had a naive benchmark:

    var l = new SortedList<int, int>();
    
    for (int it = 0; it < 100; it++)
    {
        var a0 = GC.GetAllocatedBytesForCurrentThread();
    
        for (int i = 0; i < 1000; i++)
        {
            l.Remove(i);
        }
    
        var a1 = GC.GetAllocatedBytesForCurrentThread();
    
        Console.WriteLine(a1 - a0);
    }

    Where the number of iterations matters... This version returns on my machine:
    24000
    24000
    24000
    24000
    24000
    24000
    24000
    24000
    24000
    23520
    0
    0
    0
    0
    0
    0
    0

    It probably depends on when tiered JIT generates optimal code.
    After switching TieredCompilation to false, it always returns 0.

  7. stephentoub commented on Mar 28, 2024

    @stephentoub
    Member

    Yes, in tier 0, almost all optimization is disabled (including but definitely not limited to boxing elimination). For ThrowIfNull, inlining needs to be enabled for the JIT to be able to see that the boxed object isn't actually needed, and inlining is one of the many optimizations disabled in tier 0, too.

  8. removed
    untriaged
    on Mar 28, 2024
  9. MichalPetryka commented on Mar 28, 2024

    @MichalPetryka
    Contributor

    The boxing associated with that ThrowIfNull gets optimized away by the JIT.

    Is Mono able to remove it too or is it just with CoreCLR?

  10. huoyaoyuan commented on Mar 29, 2024

    @huoyaoyuan
    Member

    I'm concerned about how this would affect relatively cold methods. The impact of one particular method is limited by the threshold of Tier 0, but invoking many different methods may be impacted more.

  11. locked and limited conversation to collaborators on Apr 28, 2024
  12. EgorBo commented on Jul 15, 2024

    @EgorBo
    Member

    #104815 removed this boxing from Tier0 as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions