<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Internal.Log
{
    /// <summary>
    /// helper class to aggregate some numeric value log in client side
    /// </summary>
    internal abstract class AbstractLogAggregator<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>> where TKey : notnull
    {
        private readonly ConcurrentDictionary<TKey, TValue> _map = new(concurrencyLevel: 2, capacity: 2);
        private readonly Func<TKey, TValue> _createCounter;

        protected AbstractLogAggregator()
        {
            _createCounter = _ => CreateCounter();
        }

        protected abstract TValue CreateCounter();

        public bool IsEmpty => _map.IsEmpty;

        public void Clear() => _map.Clear();

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
            => _map.GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator()
            => this.GetEnumerator();

        [PerformanceSensitive("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1279909", AllowCaptures = false)]
        protected TValue GetCounter(TKey key)
            => _map.GetOrAdd(key, _createCounter);

        protected bool TryGetCounter(TKey key, [MaybeNullWhen(false)] out TValue counter)
        {
            if (_map.TryGetValue(key, out counter))
            {
                return true;
            }

            return false;
        }
    }
}<commit_msg>Try to reduce JIT costs for AbstractLogAggregator<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Internal.Log
{
    /// <summary>
    /// helper class to aggregate some numeric value log in client side
    /// </summary>
    internal abstract class AbstractLogAggregator<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
        where TKey : notnull
        where TValue : class // TValue being constrained to a class will ensure that the ConcurrentDictionaries won't be JITted with structs
    {
        /// <remarks>
        /// The key here is an object even though we will often be putting enums into this map; the problem with the use of enums or other value
        /// types is they prevent the runtime from sharing the same JITted code for each different generic instantiation. In this case,
        /// the cost of boxing is cheaper than the cost of the extra JIT.
        /// </remarks>
        private readonly ConcurrentDictionary<object, TValue> _map = new(concurrencyLevel: 2, capacity: 2);
        private readonly Func<object, TValue> _createCounter;

        protected AbstractLogAggregator()
        {
            _createCounter = _ => CreateCounter();
        }

        protected abstract TValue CreateCounter();

        public bool IsEmpty => _map.IsEmpty;

        public void Clear() => _map.Clear();

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
            => _map.Select(static kvp => new KeyValuePair<TKey, TValue>((TKey)kvp.Key, kvp.Value)).GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator()
            => this.GetEnumerator();

        [PerformanceSensitive("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1279909", AllowCaptures = false)]
        protected TValue GetCounter(TKey key)
            => _map.GetOrAdd((object)key, _createCounter);

        protected bool TryGetCounter(TKey key, [MaybeNullWhen(false)] out TValue counter)
        {
            if (_map.TryGetValue((object)key, out counter))
            {
                return true;
            }

            return false;
        }
    }
}
{'fraction_non_alphanumeric': 0.08904739990796134, 'fraction_numerical': 0.0041417395306028535, 'mean_word_length': 2.8131578947368423, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 37, 'https://': 2, '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': '4667142', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1140, 'n_words': 368}