Skip to content

Try optimize without allocation - #135189

Open
Advitalitum wants to merge 16 commits into
dotnet:mainfrom
Advitalitum:try_optimize_without_allocation
Open

Advitalitum wants to merge 16 commits into
dotnet:mainfrom
Advitalitum:try_optimize_without_allocation

Conversation

@Advitalitum

Copy link
Copy Markdown
Contributor

No description provided.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 4, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

@Advitalitum
Advitalitum force-pushed the try_optimize_without_allocation branch from 05380fd to d5f1375 Compare October 4, 2026 16:44
@Advitalitum

Copy link
Copy Markdown
Contributor Author

@EgorBot -linux_amd -osx_arm64

using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args);

[MemoryDiagnoser]
public class Benchmarks
{
    private FrozenSet<int> _frozenSet = null!;
    
    private IEnumerable<int> _largerDisjointFrozenSet = null!;
    private IEnumerable<int> _largerFrozenSetWithLateMatch = null!;
    
    private IEnumerable<int> _largerDisjointHashSet = null!;
    private IEnumerable<int> _largerHashSetWithLateMatch = null!;
    
    private IEnumerable<int> _listWithImmediateMatch = null!;
    private IEnumerable<int> _largerDisjointList = null!;

    [GlobalSetup]
    public void Setup()
    {
        _frozenSet = Enumerable.Range(9_999, 100).ToFrozenSet();
        
        _largerDisjointFrozenSet = Enumerable.Range(0, 9_999).ToFrozenSet();
        _largerDisjointHashSet = new HashSet<int>(Enumerable.Range(0, 9_999));

        _largerFrozenSetWithLateMatch = Enumerable.Range(0, 9_999).Concat([9_999]).ToFrozenSet();

        HashSet<int> largerHashSetWithLateMatch = new(Enumerable.Range(0, 9_999));
        largerHashSetWithLateMatch.Add(9_999);
        _largerHashSetWithLateMatch = largerHashSetWithLateMatch;

        _listWithImmediateMatch = new List<int> { 9_999 };
        _largerDisjointList = new List<int>(Enumerable.Range(0, 9_999));
    }

    [Benchmark]
    public bool LargerFrozenSet_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointFrozenSet);
    }

    [Benchmark]
    public bool LargerFrozenSet_MatchLastInOther()
    {
        return _frozenSet.Overlaps(_largerFrozenSetWithLateMatch);
    }

    [Benchmark]
    public bool LargerHashSet_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointHashSet);
    }

    [Benchmark]
    public bool LargerHashSet_MatchLastInOther()
    {
        return _frozenSet.Overlaps(_largerHashSetWithLateMatch);
    }

    [Benchmark]
    public bool List_MatchFirst()
    {
        return _frozenSet.Overlaps(_listWithImmediateMatch);
    }

    [Benchmark]
    public bool LargerList_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointList);
    }
}

@Advitalitum

Copy link
Copy Markdown
Contributor Author

@EgorBot -linux_amd -osx_arm64

using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args);

[MemoryDiagnoser]
public class Benchmarks
{
    private FrozenSet<int> _frozenSet = null!;
    
    private IEnumerable<int> _largerDisjointFrozenSet = null!;
    private IEnumerable<int> _largerFrozenSetWithLateMatch = null!;
    
    private IEnumerable<int> _largerDisjointHashSet = null!;
    private IEnumerable<int> _largerHashSetWithLateMatch = null!;
    
    private IEnumerable<int> _listWithImmediateMatch = null!;
    private IEnumerable<int> _largerDisjointList = null!;

    [GlobalSetup]
    public void Setup()
    {
        _frozenSet = Enumerable.Range(9_999, 100).ToFrozenSet();
        
        _largerDisjointFrozenSet = Enumerable.Range(0, 9_999).ToFrozenSet();
        _largerDisjointHashSet = new HashSet<int>(Enumerable.Range(0, 9_999));

        _largerFrozenSetWithLateMatch = Enumerable.Range(0, 9_999).Concat([9_999]).ToFrozenSet();

        HashSet<int> largerHashSetWithLateMatch = new(Enumerable.Range(0, 9_999));
        largerHashSetWithLateMatch.Add(9_999);
        _largerHashSetWithLateMatch = largerHashSetWithLateMatch;

        _listWithImmediateMatch = new List<int> { 9_999 };
        _largerDisjointList = new List<int>(Enumerable.Range(0, 9_999));
    }

    [Benchmark]
    public bool LargerFrozenSet_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointFrozenSet);
    }

    [Benchmark]
    public bool LargerFrozenSet_MatchLastInOther()
    {
        return _frozenSet.Overlaps(_largerFrozenSetWithLateMatch);
    }

    [Benchmark]
    public bool LargerHashSet_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointHashSet);
    }

    [Benchmark]
    public bool LargerHashSet_MatchLastInOther()
    {
        return _frozenSet.Overlaps(_largerHashSetWithLateMatch);
    }

    [Benchmark]
    public bool List_MatchFirst()
    {
        return _frozenSet.Overlaps(_listWithImmediateMatch);
    }

    [Benchmark]
    public bool LargerList_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointList);
    }
}

@Advitalitum

Copy link
Copy Markdown
Contributor Author

@EgorBot -macos_arm

using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args);

[MemoryDiagnoser]
public class Benchmarks
{
    private FrozenSet<int> _frozenSet = null!;
    
    private IEnumerable<int> _largerDisjointFrozenSet = null!;
    private IEnumerable<int> _largerFrozenSetWithLateMatch = null!;
    
    private IEnumerable<int> _largerDisjointHashSet = null!;
    private IEnumerable<int> _largerHashSetWithLateMatch = null!;
    
    private IEnumerable<int> _listWithImmediateMatch = null!;
    private IEnumerable<int> _largerDisjointList = null!;

    [GlobalSetup]
    public void Setup()
    {
        _frozenSet = Enumerable.Range(9_999, 100).ToFrozenSet();
        
        _largerDisjointFrozenSet = Enumerable.Range(0, 9_999).ToFrozenSet();
        _largerDisjointHashSet = new HashSet<int>(Enumerable.Range(0, 9_999));

        _largerFrozenSetWithLateMatch = Enumerable.Range(0, 9_999).Concat([9_999]).ToFrozenSet();

        HashSet<int> largerHashSetWithLateMatch = new(Enumerable.Range(0, 9_999));
        largerHashSetWithLateMatch.Add(9_999);
        _largerHashSetWithLateMatch = largerHashSetWithLateMatch;

        _listWithImmediateMatch = new List<int> { 9_999 };
        _largerDisjointList = new List<int>(Enumerable.Range(0, 9_999));
    }

    [Benchmark]
    public bool LargerFrozenSet_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointFrozenSet);
    }

    [Benchmark]
    public bool LargerFrozenSet_MatchLastInOther()
    {
        return _frozenSet.Overlaps(_largerFrozenSetWithLateMatch);
    }

    [Benchmark]
    public bool LargerHashSet_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointHashSet);
    }

    [Benchmark]
    public bool LargerHashSet_MatchLastInOther()
    {
        return _frozenSet.Overlaps(_largerHashSetWithLateMatch);
    }

    [Benchmark]
    public bool List_MatchFirst()
    {
        return _frozenSet.Overlaps(_listWithImmediateMatch);
    }

    [Benchmark]
    public bool LargerList_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointList);
    }
}

@Advitalitum

Copy link
Copy Markdown
Contributor Author

@EgorBot -macos_arm

using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Benchmarks).Assembly).Run(args);

[MemoryDiagnoser]
public class Benchmarks
{
    private FrozenSet<int> _frozenSet = null!;
    
    private IEnumerable<int> _largerDisjointFrozenSet = null!;
    private IEnumerable<int> _largerFrozenSetWithLateMatch = null!;
    
    private IEnumerable<int> _largerDisjointHashSet = null!;
    private IEnumerable<int> _largerHashSetWithLateMatch = null!;
    
    private IEnumerable<int> _listWithImmediateMatch = null!;
    private IEnumerable<int> _largerDisjointList = null!;

    [GlobalSetup]
    public void Setup()
    {
        _frozenSet = Enumerable.Range(9_999, 100).ToFrozenSet();
        
        _largerDisjointFrozenSet = Enumerable.Range(0, 9_999).ToFrozenSet();
        _largerDisjointHashSet = new HashSet<int>(Enumerable.Range(0, 9_999));

        _largerFrozenSetWithLateMatch = Enumerable.Range(0, 9_999).Concat([9_999]).ToFrozenSet();

        HashSet<int> largerHashSetWithLateMatch = new(Enumerable.Range(0, 9_999));
        largerHashSetWithLateMatch.Add(9_999);
        _largerHashSetWithLateMatch = largerHashSetWithLateMatch;

        _listWithImmediateMatch = new List<int> { 9_999 };
        _largerDisjointList = new List<int>(Enumerable.Range(0, 9_999));
    }

    [Benchmark]
    public bool LargerFrozenSet_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointFrozenSet);
    }

    [Benchmark]
    public bool LargerFrozenSet_MatchLastInOther()
    {
        return _frozenSet.Overlaps(_largerFrozenSetWithLateMatch);
    }

    [Benchmark]
    public bool LargerHashSet_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointHashSet);
    }

    [Benchmark]
    public bool LargerHashSet_MatchLastInOther()
    {
        return _frozenSet.Overlaps(_largerHashSetWithLateMatch);
    }

    [Benchmark]
    public bool List_MatchFirst()
    {
        return _frozenSet.Overlaps(_listWithImmediateMatch);
    }

    [Benchmark]
    public bool LargerList_Disjoint()
    {
        return _frozenSet.Overlaps(_largerDisjointList);
    }
}

This branch has not been deployed

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

Labels

area-System.Collections community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant