<commit_before>﻿using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Jobs;
using Light.GuardClauses.Exceptions;

namespace Light.GuardClauses.Performance.MustHaveValue
{
    [ClrJob, CoreJob]
    [MemoryDiagnoser]
    [DisassemblyDiagnoser]
    public class MustHaveValueBenchmarks
    {
        public static readonly int? Nullable = 42;

        [Benchmark(Baseline = true)]
        public int? BaseVersion()
        {
            if (Nullable.HasValue == false) throw new NullableHasNoValueException(nameof(Nullable));
            return Nullable;
        }

        [Benchmark]
        public int? OldVersionWithParameterName() => Nullable.OldMustHaveValue(nameof(Nullable));

        [Benchmark]
        public int? LightGuardClausesWithParameterName() => Nullable.MustHaveValue(nameof(Nullable));

        [Benchmark]
        public int? LightGuardClausesWithCustomException() => Nullable.MustHaveValue(() => new Exception());
    }

    public static class MustHaveValueExtensionMethods
    {
        public static T? OldMustHaveValue<T>(this T? parameter, string parameterName = null, string message = null, Func<Exception> exception = null)
            where T : struct
        {
            if (parameter.HasValue)
                return parameter;

            throw exception?.Invoke() ?? (message != null ? new NullableHasNoValueException(message, parameterName) : new NullableHasNoValueException(parameterName));
        }
    }
}<commit_msg>added new case to MustHaveValueBenchmarks<commit_after>﻿using System;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Jobs;
using Light.GuardClauses.Exceptions;

namespace Light.GuardClauses.Performance.MustHaveValue
{
    [ClrJob, CoreJob]
    [MemoryDiagnoser]
    [DisassemblyDiagnoser]
    public class MustHaveValueBenchmarks
    {
        public static readonly int? Nullable = 42;

        [Benchmark(Baseline = true)]
        public int? BaseVersion()
        {
            if (Nullable.HasValue == false) throw new NullableHasNoValueException(nameof(Nullable));
            return Nullable;
        }

        [Benchmark]
        public int? OldVersionWithParameterName() => Nullable.OldMustHaveValue(nameof(Nullable));

        [Benchmark]
        public int? LightGuardClausesWithParameterName() => Nullable.MustHaveValue(nameof(Nullable));

        [Benchmark]
        public int? NewVersionReturnFast() => Nullable.NewMustHaveValueReturnFast(nameof(Nullable));

        [Benchmark]
        public int? LightGuardClausesWithCustomException() => Nullable.MustHaveValue(() => new Exception());
    }

    public static class MustHaveValueExtensionMethods
    {
        public static T? OldMustHaveValue<T>(this T? parameter, string parameterName = null, string message = null, Func<Exception> exception = null)
            where T : struct
        {
            if (parameter.HasValue)
                return parameter;

            throw exception?.Invoke() ?? (message != null ? new NullableHasNoValueException(message, parameterName) : new NullableHasNoValueException(parameterName));
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static T? NewMustHaveValueReturnFast<T>(this T? parameter, string parameterName = null, string message = null) where T : struct
        {
            if (parameter.HasValue) return parameter;
            Throw.NullableHasNoValue(parameterName, message);
            return null;
        }
    }
}
{'fraction_non_alphanumeric': 0.08917921045157626, 'fraction_numerical': 0.001136040897472309, 'mean_word_length': 3.1533018867924527, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 17, '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': '1893133', 'n_tokens_mistral': 946, 'n_tokens_neox': 909, 'n_words': 211}