Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: optimize finilizeWearableLoadingSystem #2442

Merged
merged 13 commits into from
Oct 17, 2024
46 changes: 30 additions & 16 deletions Explorer/Assets/DCL/CommunicationData/URLHelpers/URN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ public readonly struct URN

private readonly string lowercaseUrn;
private readonly string originalUrn;
private readonly int hash;

public URN(string urn)
{
this.originalUrn = urn;
this.lowercaseUrn = this.originalUrn.ToLower();
originalUrn = urn;
lowercaseUrn = originalUrn.ToLower();
hash = originalUrn != null ? lowercaseUrn.GetHashCode() : 0;
}

public URN(int urn)
private URN(URN urn, int shortenIndex)
{
this.originalUrn = urn.ToString();
this.lowercaseUrn = this.originalUrn.ToLower();
bool hasSameLength = shortenIndex == urn.originalUrn.Length;

originalUrn = hasSameLength ? urn.originalUrn : urn.originalUrn[..shortenIndex];
lowercaseUrn = hasSameLength ? urn.lowercaseUrn : urn.lowercaseUrn[..shortenIndex];

hash = hasSameLength
? urn.hash
: originalUrn != null
? lowercaseUrn.GetHashCode()
: 0;
}

public bool IsNullOrEmpty() =>
Expand All @@ -31,13 +41,9 @@ public bool IsNullOrEmpty() =>
public bool IsValid() =>
!IsNullOrEmpty() && originalUrn.StartsWith("urn");

public bool Equals(int other) => Equals(other.ToString());

public bool Equals(URN other) =>
Equals(other.lowercaseUrn);

public bool Equals(string other) =>
string.Equals(lowercaseUrn, other);
hash == other.hash && // "fail fast" check
lowercaseUrn == other.lowercaseUrn; // check to avoid false positives from hash collisions

public override bool Equals(object obj) =>
obj is URN other && Equals(other);
Expand All @@ -47,7 +53,8 @@ public override string ToString() =>

public URLAddress ToUrlOrEmpty(URLAddress baseUrl)
{
string currentUrn = this.originalUrn;
string currentUrn = originalUrn;

ReadOnlySpan<char> CutBeforeColon(ref int endIndex, out bool success)
{
int atBeginning = endIndex;
Expand Down Expand Up @@ -115,13 +122,20 @@ void LogError()
}

public override int GetHashCode() =>
originalUrn != null ? StringComparer.OrdinalIgnoreCase.GetHashCode(originalUrn) : 0;
hash;

public URN Shorten()
{
if (string.IsNullOrEmpty(originalUrn)) return this;
if (CountParts() <= SHORTEN_URN_PARTS) return this;

int shortenIndex = GetShortenIndex();

return shortenIndex != -1 ? new URN(this, shortenIndex) : this;
}

private int GetShortenIndex()
{
int index;

if (IsThirdPartyCollection())
Expand All @@ -137,13 +151,13 @@ public URN Shorten()
if (index == -1) break;
}

return index != -1 ? originalUrn[..index] : originalUrn;
return index;
}

// TokenId is always placed in the last part for regular nfts
index = originalUrn.LastIndexOf(':');

return index != -1 ? originalUrn[..index] : this;
return index;
}

public static implicit operator URN(int urn) =>
Expand All @@ -160,7 +174,7 @@ public bool IsThirdPartyCollection() =>

private int CountParts()
{
int count = 1;
var count = 1;
int index = originalUrn.IndexOf(':');

while (index != -1)
Expand Down