From 41db9dad8ba4e69af37ab7780811fc0cd3857cfb Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Wed, 21 Aug 2024 02:03:15 +0900 Subject: [PATCH] chore: use HashCode struct --- Editor/AnimatorParserV2/PropModNode.cs | 4 +- Editor/ObjectMapping/ObjectMapping.cs | 12 +----- Editor/Processors/MergeBoneProcessor.cs | 6 +-- .../SkinnedMeshes/EditSkinnedMeshProcessor.cs | 2 +- .../MergeSkinnedMeshProcessor.cs | 4 +- .../TraceAndOptimize/AutoMergeSkinnedMesh.cs | 40 +++++++++---------- Editor/Utils/AnimationLocation.cs | 21 +++++----- Editor/Utils/ComponentOrGameObject.cs | 2 +- Internal/Utils/EqualsHashSet.cs | 9 +---- Internal/Utils/Matrix3x3.cs | 24 +++++------ Internal/Utils/Utils.GetHashCode2.cs | 8 ++-- 11 files changed, 51 insertions(+), 81 deletions(-) diff --git a/Editor/AnimatorParserV2/PropModNode.cs b/Editor/AnimatorParserV2/PropModNode.cs index 181c51eba..6ebdaa85d 100644 --- a/Editor/AnimatorParserV2/PropModNode.cs +++ b/Editor/AnimatorParserV2/PropModNode.cs @@ -94,9 +94,7 @@ public bool Equals(ValueInfo other) public override bool Equals(object obj) => obj is ValueInfo other && Equals(other); - public override int GetHashCode() => _possibleValues == null - ? 0 - : _possibleValues.Aggregate(0, (current, value) => current ^ value.GetHashCode()); + public override int GetHashCode() => _possibleValues == null ? 0 : _possibleValues.GetSetHashCode(); public override string ToString() => _possibleValues == null diff --git a/Editor/ObjectMapping/ObjectMapping.cs b/Editor/ObjectMapping/ObjectMapping.cs index a20be72b6..9327087ea 100644 --- a/Editor/ObjectMapping/ObjectMapping.cs +++ b/Editor/ObjectMapping/ObjectMapping.cs @@ -174,17 +174,7 @@ public PropertyDescriptor(int instanceId, Type type, string name) Name = name; } - public override int GetHashCode() - { - unchecked - { - var hashCode = InstanceId; - hashCode = (hashCode * 397) ^ Type.GetHashCode(); - hashCode = (hashCode * 397) ^ Name.GetHashCode(); - return hashCode; - } - } - + public override int GetHashCode() => HashCode.Combine(InstanceId, Type, Name); public bool Equals(PropertyDescriptor other) => InstanceId == other.InstanceId && Type == other.Type && Name == other.Name; public override bool Equals(object? obj) => obj is PropertyDescriptor other && Equals(other); diff --git a/Editor/Processors/MergeBoneProcessor.cs b/Editor/Processors/MergeBoneProcessor.cs index cf476d5d9..1cb197316 100644 --- a/Editor/Processors/MergeBoneProcessor.cs +++ b/Editor/Processors/MergeBoneProcessor.cs @@ -293,11 +293,9 @@ public bool Equals(BoneUniqKey other) => Equals(Transform, other.Transform) && _bindPoseInfo == other._bindPoseInfo; public override bool Equals(object? obj) => obj is BoneUniqKey other && Equals(other); - - public override int GetHashCode() => - unchecked(_bindPoseInfo.GetHashCode() * 397) ^ (Transform != null ? Transform.GetHashCode() : 0); + public override int GetHashCode() => HashCode.Combine(_bindPoseInfo, Transform); } - + public struct MergeBoneTransParentInfo { diff --git a/Editor/Processors/SkinnedMeshes/EditSkinnedMeshProcessor.cs b/Editor/Processors/SkinnedMeshes/EditSkinnedMeshProcessor.cs index 0e34b589a..94792e45f 100644 --- a/Editor/Processors/SkinnedMeshes/EditSkinnedMeshProcessor.cs +++ b/Editor/Processors/SkinnedMeshes/EditSkinnedMeshProcessor.cs @@ -28,7 +28,7 @@ protected EditSkinnedMeshProcessor(TComponent component) protected bool Equals(EditSkinnedMeshProcessor other) => Component == other.Component; - public override bool Equals(object obj) => + public override bool Equals(object? obj) => obj != null && (ReferenceEquals(this, obj) || obj.GetType() == this.GetType() && Equals((EditSkinnedMeshProcessor)obj)); diff --git a/Editor/Processors/SkinnedMeshes/MergeSkinnedMeshProcessor.cs b/Editor/Processors/SkinnedMeshes/MergeSkinnedMeshProcessor.cs index 8dc4cfbd1..340c4d0ec 100644 --- a/Editor/Processors/SkinnedMeshes/MergeSkinnedMeshProcessor.cs +++ b/Editor/Processors/SkinnedMeshes/MergeSkinnedMeshProcessor.cs @@ -521,7 +521,7 @@ class MeshInfoComputer : IMeshInfoComputer private class BlendShapeNameComparator : IEqualityComparer<(string name, float weight)> { - public static readonly BlendShapeNameComparator Instance = new BlendShapeNameComparator(); + public static readonly BlendShapeNameComparator Instance = new(); public bool Equals((string name, float weight) x, (string name, float weight) y) { @@ -530,7 +530,7 @@ public bool Equals((string name, float weight) x, (string name, float weight) y) public int GetHashCode((string name, float weight) obj) { - return obj.name?.GetHashCode() ?? 0; + return obj.name.GetHashCode(); } } } diff --git a/Editor/Processors/TraceAndOptimize/AutoMergeSkinnedMesh.cs b/Editor/Processors/TraceAndOptimize/AutoMergeSkinnedMesh.cs index 6e6dc1fec..ec55c85f9 100644 --- a/Editor/Processors/TraceAndOptimize/AutoMergeSkinnedMesh.cs +++ b/Editor/Processors/TraceAndOptimize/AutoMergeSkinnedMesh.cs @@ -720,28 +720,24 @@ public override bool Equals(object? obj) public override int GetHashCode() { - unchecked - { - var hashCode = HasNormals.GetHashCode(); - hashCode = (hashCode * 397) ^ ActivenessAnimationLocations.GetHashCode(); - hashCode = (hashCode * 397) ^ RendererAnimationLocations.GetHashCode(); - hashCode = (hashCode * 397) ^ Activeness.GetHashCode(); - hashCode = (hashCode * 397) ^ Bounds.GetHashCode(); - hashCode = (hashCode * 397) ^ (int)ShadowCastingMode; - hashCode = (hashCode * 397) ^ ReceiveShadows.GetHashCode(); - hashCode = (hashCode * 397) ^ (int)LightProbeUsage; - hashCode = (hashCode * 397) ^ (int)ReflectionProbeUsage; - hashCode = (hashCode * 397) ^ AllowOcclusionWhenDynamic.GetHashCode(); - hashCode = (hashCode * 397) ^ (LightProbeProxyVolumeOverride != null - ? LightProbeProxyVolumeOverride.GetHashCode() - : 0); - hashCode = (hashCode * 397) ^ (ProbeAnchor != null ? ProbeAnchor.GetHashCode() : 0); - hashCode = (hashCode * 397) ^ (int)Quality; - hashCode = (hashCode * 397) ^ UpdateWhenOffscreen.GetHashCode(); - hashCode = (hashCode * 397) ^ (RootBone != null ? RootBone.GetHashCode() : 0); - hashCode = (hashCode * 397) ^ SkinnedMotionVectors.GetHashCode(); - return hashCode; - } + var hashCode = new HashCode(); + hashCode.Add(HasNormals); + hashCode.Add(ActivenessAnimationLocations); + hashCode.Add(RendererAnimationLocations); + hashCode.Add(Activeness); + hashCode.Add(Bounds); + hashCode.Add(ShadowCastingMode); + hashCode.Add(ReceiveShadows); + hashCode.Add(LightProbeUsage); + hashCode.Add(ReflectionProbeUsage); + hashCode.Add(AllowOcclusionWhenDynamic); + hashCode.Add(LightProbeProxyVolumeOverride); + hashCode.Add(ProbeAnchor); + hashCode.Add(Quality); + hashCode.Add(UpdateWhenOffscreen); + hashCode.Add(RootBone); + hashCode.Add(SkinnedMotionVectors); + return hashCode.ToHashCode(); } } } diff --git a/Editor/Utils/AnimationLocation.cs b/Editor/Utils/AnimationLocation.cs index 800de9e34..b3d2fcb78 100644 --- a/Editor/Utils/AnimationLocation.cs +++ b/Editor/Utils/AnimationLocation.cs @@ -105,18 +105,15 @@ public override bool Equals(object? obj) => public override int GetHashCode() { - unchecked - { - var hashCode = Component.GetHashCode(); - hashCode = (hashCode * 397) ^ PlayableLayerIndex; - hashCode = (hashCode * 397) ^ AnimationLayerIndex; - hashCode = (hashCode * 397) ^ AnimatorState.GetHashCode(); - hashCode = (hashCode * 397) ^ BlendTreeLocation.Length; - foreach (var location in BlendTreeLocation) - hashCode = (hashCode * 397) ^ location; - hashCode = (hashCode * 397) ^ Curve.GetHashCode2(); - return hashCode; - } + var hashCode = new HashCode(); + hashCode.Add(Component); + hashCode.Add(PlayableLayerIndex); + hashCode.Add(AnimationLayerIndex); + hashCode.Add(AnimatorState); + foreach (var location in BlendTreeLocation) + hashCode.Add(location); + hashCode.Add(Curve); + return hashCode.ToHashCode(); } public IEnumerable ContextReferences => new[] diff --git a/Editor/Utils/ComponentOrGameObject.cs b/Editor/Utils/ComponentOrGameObject.cs index 985314775..0b4c49d5d 100644 --- a/Editor/Utils/ComponentOrGameObject.cs +++ b/Editor/Utils/ComponentOrGameObject.cs @@ -49,7 +49,7 @@ public bool TryAs(out T gameObject) where T : Object public bool Equals(ComponentOrGameObject other) => Equals(_object, other._object); public override bool Equals(object obj) => obj is ComponentOrGameObject other && Equals(other); - public override int GetHashCode() => _object != null ? _object.GetHashCode() : 0; + public override int GetHashCode() => HashCode.Combine(_object); public override string ToString() => _object != null ? _object.ToString() : string.Empty; } } diff --git a/Internal/Utils/EqualsHashSet.cs b/Internal/Utils/EqualsHashSet.cs index 9ec56e099..9a1cb9a15 100644 --- a/Internal/Utils/EqualsHashSet.cs +++ b/Internal/Utils/EqualsHashSet.cs @@ -20,14 +20,7 @@ public EqualsHashSet(IEnumerable collection) : this(new HashSet(collection public int Count => backedSet.Count; - public override int GetHashCode() - { - // we use XOR to make the hashcode order-independent - var hash = backedSet.Count; - foreach (var item in backedSet) - hash ^= item == null ? 0 : item.GetHashCode(); - return hash; - } + public override int GetHashCode() => backedSet.GetSetHashCode(); public bool Equals(EqualsHashSet other) => !ReferenceEquals(null, other) && (ReferenceEquals(this, other) || backedSet.SetEquals(other.backedSet)); diff --git a/Internal/Utils/Matrix3x3.cs b/Internal/Utils/Matrix3x3.cs index d79140f61..ac48ce395 100644 --- a/Internal/Utils/Matrix3x3.cs +++ b/Internal/Utils/Matrix3x3.cs @@ -187,19 +187,17 @@ public Matrix3x3( public override int GetHashCode() { - unchecked - { - var hashCode = m00.GetHashCode(); - hashCode = (hashCode * 397) ^ m10.GetHashCode(); - hashCode = (hashCode * 397) ^ m20.GetHashCode(); - hashCode = (hashCode * 397) ^ m01.GetHashCode(); - hashCode = (hashCode * 397) ^ m11.GetHashCode(); - hashCode = (hashCode * 397) ^ m21.GetHashCode(); - hashCode = (hashCode * 397) ^ m02.GetHashCode(); - hashCode = (hashCode * 397) ^ m12.GetHashCode(); - hashCode = (hashCode * 397) ^ m22.GetHashCode(); - return hashCode; - } + var hashCode = new HashCode(); + hashCode.Add(m00); + hashCode.Add(m10); + hashCode.Add(m20); + hashCode.Add(m01); + hashCode.Add(m11); + hashCode.Add(m21); + hashCode.Add(m02); + hashCode.Add(m12); + hashCode.Add(m22); + return hashCode.ToHashCode(); } public bool Equals(Matrix3x3 other) => diff --git a/Internal/Utils/Utils.GetHashCode2.cs b/Internal/Utils/Utils.GetHashCode2.cs index 9b5594191..c1f7f3f80 100644 --- a/Internal/Utils/Utils.GetHashCode2.cs +++ b/Internal/Utils/Utils.GetHashCode2.cs @@ -36,12 +36,12 @@ public static int GetHashCode2(this Keyframe curve) return code.ToHashCode(); } - // The HashSet doesn't implement HashCode - public static int GetHashCode2(this HashSet set) + // Create a hashcode for a order-independent set + public static int GetSetHashCode(this ICollection collection) { // we use XOR to make the hashcode order-independent - var hash = set.Count; - foreach (var item in set) + var hash = collection.Count; + foreach (var item in collection) hash ^= HashCode.Combine(item); return hash; }