Skip to content

[API Proposal]: ImmutableArray<T>.Builder.ToImmutableAndClear() #72625

Closed

Description

@333fred

Background and motivation

In basically every project I use ImmutableArray in, I end up creating an extension for ToImmutableAndClear on the builder type, which takes advantage of MoveToImmutable() if the current builder has right size, and otherwise falls back to ToImmutable()/Clear() if not. I would really love it if we added this to the BCL itself.

API Proposal

namespace System.Collections.Immutable;

public struct ImmutableArray<T>
{
    public class Builder
    {
        public ImmutableArray<T> ToImmutableAndClear()
        {
            if (Capacity == Count) return MoveToImmutable();
            else
            {
                var result = ToImmutable();
                Clear();
                return result;
            }
       }
    }
}

API Usage

var builder = ImmutableArray.CreateBuilder<string>(/* max possible values */ 42);

foreach (var x in thingGettingMapped)
{
    if (x.Frob) builder.Add(x);
}

return builder.ToImmutableAndClear();

Alternative Designs

No response

Risks

No response

Activity

  1. added
    api-suggestion
    on Jul 21, 2022
  2. ghost added
    untriaged
    on Jul 21, 2022
  3. Sergio0694 commented on Jul 21, 2022

    @Sergio0694
    Contributor

    I also use something like this quite often in my generators and would very much welcome this API 🙂

    Nit: the proposal says ToImmutableAndClear but the example uses ToImmutableAndFree, old snippet?

  4. 333fred commented on Jul 21, 2022

    @333fred
    MemberAuthor

    Nit: the proposal says ToImmutableAndClear but the example uses ToImmutableAndFree, old snippet?

    No, force of habit. Roslyn also pools builders.

  5. eiriktsarpalis commented on Jul 22, 2022

    @eiriktsarpalis
    Member

    Sounds useful enough. If we do add such a method, we might want to explicitly document that any perf benefits are predicated on Capacity == Count.

  6. removed
    untriaged
    on Jul 22, 2022
  7. added this to the 8.0.0 milestone on Jul 22, 2022
  8. added
    api-ready-for-review
    and removed
    api-suggestion
    on Jul 22, 2022
  9. terrajobst commented on Aug 16, 2022

    @terrajobst
    Contributor

    Video

    The challenge is that MoveToImmutable() results in Capacity becoming 0, while ToImmutable(); Clear(); would preserve the capacity. This can result in cases where we need go through various resize cycles, making the Capacity == Count potentially more expensive.

    namespace System.Collections.Immutable;
    
    public partial struct ImmutableArray<T>
    {
        public partial class Builder
        {
            public ImmutableArray<T> ToImmutableAndClear();
        }
    }
  10. added
    api-needs-work
    api-ready-for-review
    and removed
    api-ready-for-review
    api-needs-work
    on Aug 16, 2022
  11. bartonjs commented on Sep 6, 2022

    @bartonjs
    Member

    Video

    • ToImmutableAndClear, as proposed, has a problem where when Capacity==Count Capacity changes to 0, and otherwise it remains.
    • We instead think the "do the cheapest thing possible" would be DrainToImmutable, always making Capacity 0 as a result of being called.
      • We debated many verbs (Drain, Extract, Empty) and prefix vs post-fix, but prefix matched the existing MoveToImmutable vs ToImmutable choice.
    namespace System.Collections.Immutable;
    
    public struct ImmutableArray<T>
    {
        public partial class Builder
        {
            public ImmutableArray<T> DrainToImmutable();
        }
    }
  12. added
    api-approved
    and removed
    api-ready-for-review
    on Sep 6, 2022
  13. added
    help wanted
    on Oct 7, 2022
  14. ghost added
    in-pr
    on Dec 8, 2022
  15. ghost removed
    in-pr
    on Dec 8, 2022
  16. ghost locked as resolved and limited conversation to collaborators on Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Collectionshelp wanted

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions