// Copyright (c) 2022 bradson // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. namespace PerformanceFish.Cache; public static class ByReference where T_first : notnull { private static Dictionary _get = new(); [ThreadStatic] private static Dictionary? _getThreadStatic; public static Dictionary Get { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => /*UnityData.IsInMainThread ? _get :*/ _getThreadStatic ??= Utility.AddNew>(); } public static Dictionary GetDirectly => _get; static ByReference() => Utility.All.Add(_get); public static unsafe void Initialize() { try { _ = FisheryLib.FunctionPointers.Equals.Default; _ = FisheryLib.FunctionPointers.GetHashCode.Default; } catch (Exception ex) { Log.Error($"Performance Fish encountered an exception while trying to initialize {typeof(ByReference).FullDescription()}\n{ex}"); } } } public struct ByReference : IEquatable> where T_first : notnull where T_second : notnull { private static Dictionary, T_result> _get = new(); [ThreadStatic] private static Dictionary, T_result>? _getThreadStatic; public static Dictionary, T_result> Get { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => /*UnityData.IsInMainThread ? _get :*/ _getThreadStatic ??= Utility.AddNew, T_result>>(); } public static Dictionary, T_result> GetDirectly => _get; static ByReference() => Utility.All.Add(_get); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ByReference(T_first first, T_second second) { First = first; Second = second; } public static unsafe void Initialize() { try { _ = FisheryLib.FunctionPointers.Equals.Default; _ = FisheryLib.FunctionPointers.Equals.Default; _ = FisheryLib.FunctionPointers.Equals>.Default; _ = FisheryLib.FunctionPointers.GetHashCode.Default; _ = FisheryLib.FunctionPointers.GetHashCode.Default; _ = FisheryLib.FunctionPointers.GetHashCode>.Default; } catch (Exception ex) { Log.Error($"Performance Fish encountered an exception while trying to initialize {typeof(ByReference).FullDescription()}\n{ex}"); } } public T_first First; public T_second Second; [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(ByReference other) => First.Equals(other.First) && Second.Equals(other.Second); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() => HashCode.Combine(First, Second); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override bool Equals(object obj) => obj is ByReference cache && Equals(cache); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(ByReference left, ByReference right) => left.Equals(right); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(ByReference left, ByReference right) => !(left == right); } public struct ByReference : IEquatable> where T_first : notnull where T_second : notnull where T_third : notnull { private static Dictionary, T_result> _get = new(); [ThreadStatic] private static Dictionary, T_result>? _getThreadStatic; public static Dictionary, T_result> Get { [MethodImpl(MethodImplOptions.AggressiveInlining)] get /*UnityData.IsInMainThread ? _get :*/ => _getThreadStatic ??= Utility.AddNew, T_result>>(); } public static Dictionary, T_result> GetDirectly => _get; static ByReference() => Utility.All.Add(_get); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ByReference(T_first first, T_second second, T_third third) { First = first; Second = second; Third = third; _hashCode = HashCode.Combine(first, second, third); } public static unsafe void Initialize() { try { _ = FisheryLib.FunctionPointers.Equals.Default; _ = FisheryLib.FunctionPointers.Equals.Default; _ = FisheryLib.FunctionPointers.Equals.Default; _ = FisheryLib.FunctionPointers.Equals>.Default; _ = FisheryLib.FunctionPointers.GetHashCode.Default; _ = FisheryLib.FunctionPointers.GetHashCode.Default; _ = FisheryLib.FunctionPointers.GetHashCode.Default; _ = FisheryLib.FunctionPointers.GetHashCode>.Default; } catch (Exception ex) { Log.Error($"Performance Fish encountered an exception while trying to initialize {typeof(ByReference).FullDescription()}\n{ex}"); } } public T_first First; public T_second Second; public T_third Third; private int _hashCode; [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(ByReference other) => First.Equals(other.First) && Second.Equals(other.Second) && Third.Equals(other.Third); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() => _hashCode; [MethodImpl(MethodImplOptions.AggressiveInlining)] public override bool Equals(object obj) => obj is ByReference cache && Equals(cache); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(ByReference left, ByReference right) => left.Equals(right); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(ByReference left, ByReference right) => !(left == right); } public struct ByReference : IEquatable> where T_first : notnull where T_second : notnull where T_third : notnull where T_fourth : notnull { private static Dictionary, T_result> _get = new(); [ThreadStatic] private static Dictionary, T_result>? _getThreadStatic; public static Dictionary, T_result> Get { [MethodImpl(MethodImplOptions.AggressiveInlining)] get /*UnityData.IsInMainThread ? _get :*/ => _getThreadStatic ??= Utility.AddNew, T_result>>(); } public static Dictionary, T_result> GetDirectly => _get; static ByReference() => Utility.All.Add(_get); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ByReference(T_first first, T_second second, T_third third, T_fourth fourth) { First = first; Second = second; Third = third; Fourth = fourth; _hashCode = HashCode.Combine(first, second, third, fourth); } public static unsafe void Initialize() { try { _ = FisheryLib.FunctionPointers.Equals.Default; _ = FisheryLib.FunctionPointers.Equals.Default; _ = FisheryLib.FunctionPointers.Equals.Default; _ = FisheryLib.FunctionPointers.Equals.Default; _ = FisheryLib.FunctionPointers.Equals>.Default; _ = FisheryLib.FunctionPointers.GetHashCode.Default; _ = FisheryLib.FunctionPointers.GetHashCode.Default; _ = FisheryLib.FunctionPointers.GetHashCode.Default; _ = FisheryLib.FunctionPointers.GetHashCode.Default; _ = FisheryLib.FunctionPointers.GetHashCode>.Default; } catch (Exception ex) { Log.Error($"Performance Fish encountered an exception while trying to initialize {typeof(ByReference).FullDescription()}\n{ex}"); } } public T_first First; public T_second Second; public T_third Third; public T_fourth Fourth; private int _hashCode; [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(ByReference other) => First.Equals(other.First) && Second.Equals(other.Second) && Third.Equals(other.Third) && Fourth.Equals(other.Fourth); [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() => _hashCode; [MethodImpl(MethodImplOptions.AggressiveInlining)] public override bool Equals(object obj) => obj is ByReference cache && Equals(cache); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(ByReference left, ByReference right) => left.Equals(right); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(ByReference left, ByReference right) => !(left == right); }