Skip to content

Commit

Permalink
Avoid/cache string allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed May 29, 2022
1 parent 8da904f commit 370c674
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions Src/FluentAssertions/Equivalency/Node.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using FluentAssertions.Common;
Expand All @@ -13,15 +14,35 @@ public class Node : INode
{
private static readonly Regex MatchFirstIndex = new(@"^\[\d+\]$");

private string path;
private string name;
private string pathAndName;

public GetSubjectId GetSubjectId { get; protected set; } = () => string.Empty;

public Type Type { get; protected set; }

public string Path { get; protected set; }
public string Path
{
get => path;
protected set
{
path = value;
pathAndName = null;
}
}

public string PathAndName => Path.Combine(Name);
public string PathAndName => pathAndName ??= Path.Combine(Name);

public string Name { get; set; }
public string Name
{
get => name;
set
{
name = value;
pathAndName = null;
}
}

public virtual string Description => $"{GetSubjectId().Combine(PathAndName)}";

Expand Down Expand Up @@ -109,14 +130,17 @@ public override bool Equals(object obj)
return Equals((Node)obj);
}

private bool Equals(Node other) => Type == other.Type && PathAndName == other.PathAndName;
private bool Equals(Node other) => (Type, Name, Path) == (other.Type, other.Name, other.Path);

public override int GetHashCode()
{
unchecked
{
#pragma warning disable CA1307
return (Type.GetHashCode() * 397) ^ PathAndName.GetHashCode();
int hashCode = Type.GetHashCode();
hashCode = (hashCode * 397) + Path.GetHashCode();
hashCode = (hashCode * 397) + Name.GetHashCode();
return hashCode;
}
}

Expand Down

0 comments on commit 370c674

Please sign in to comment.