<commit_before><commit_msg>Add weaklist benchmarks (#3166)<commit_after>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Linq;
using BenchmarkDotNet.Attributes;
using NUnit.Framework;
using osu.Framework.Lists;

namespace osu.Framework.Benchmarks
{
    public class BenchmarkWeakList : BenchmarkTest
    {
        private readonly object[] objects = new object[1000];
        private WeakList<object> weakList;

        public override void SetUp()
        {
            for (int i = 0; i < 1000; i++)
                objects[i] = new object();
        }

        [IterationSetup]
        [SetUp]
        public void IterationSetup()
        {
            weakList = new WeakList<object>();

            foreach (var obj in objects)
                weakList.Add(obj);
        }

        [Benchmark]
        public void Add() => weakList.Add(new object());

        [Benchmark]
        public void Remove() => weakList.Remove(objects[0]);

        [Benchmark]
        public void RemoveEach()
        {
            foreach (var obj in objects)
                weakList.Remove(obj);
        }

        [Benchmark]
        public void Clear() => weakList.Clear();

        [Benchmark]
        public bool Contains() => weakList.Contains(objects[0]);

        [Benchmark]
        public object[] Enumerate() => weakList.ToArray();

        [Benchmark]
        public object[] ClearAndEnumerate()
        {
            weakList.Clear();
            return weakList.ToArray();
        }
    }
}
{'fraction_non_alphanumeric': 0.10721003134796238, 'fraction_numerical': 0.009404388714733543, 'mean_word_length': 1.9776119402985075, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '924960', 'n_tokens_mistral': 466, 'n_tokens_neox': 432, 'n_words': 121}