Use less memory when generating a GuidUdi (#13090)

* Use less memory when generating a GuidUdi

* PR feedback

Co-authored-by: Sebastiaan Janssen <sebastiaan@umbraco.com>
This commit is contained in:
patrickdemooij9
2022-10-04 01:49:59 +02:00
committed by GitHub
parent 71276b7b4d
commit 31cb328c3b
4 changed files with 111 additions and 2 deletions

View File

@@ -348,5 +348,10 @@ public static partial class Constants
// TODO: return a list of built in types so we can use that to prevent deletion in the uI
}
public static class Udi
{
public const string Prefix = "umb://";
}
}
}

View File

@@ -1,4 +1,5 @@
using System.ComponentModel;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core;
@@ -14,7 +15,7 @@ public class GuidUdi : Udi
/// <param name="entityType">The entity type part of the udi.</param>
/// <param name="guid">The guid part of the udi.</param>
public GuidUdi(string entityType, Guid guid)
: base(entityType, "umb://" + entityType + "/" + guid.ToString("N")) =>
: base(entityType, CreateStringValue(entityType, guid)) =>
Guid = guid;
/// <summary>
@@ -57,4 +58,19 @@ public class GuidUdi : Udi
EnsureNotRoot();
return this;
}
private static string CreateStringValue(ReadOnlySpan<char> entityType, Guid guid)
{
var startUdiLength = Constants.Conventions.Udi.Prefix.Length;
var outputSize = entityType.Length + startUdiLength + 32 + 1; //Based on the format umb://entityType/guid (32 = Guid N format, 1 = / between entityType and guid)
Span<char> output = stackalloc char[outputSize];
//Add all the values of the format to the output
Constants.Conventions.Udi.Prefix.CopyTo(output[..startUdiLength]);
entityType.CopyTo(output.Slice(startUdiLength, entityType.Length));
output[startUdiLength + entityType.Length] = '/';
guid.TryFormat(output.Slice(outputSize - 32, 32), out _, "N");
return new string(output);
}
}

View File

@@ -14,7 +14,7 @@ public class StringUdi : Udi
/// <param name="entityType">The entity type part of the udi.</param>
/// <param name="id">The string id part of the udi.</param>
public StringUdi(string entityType, string id)
: base(entityType, "umb://" + entityType + "/" + EscapeUriString(id)) =>
: base(entityType, Constants.Conventions.Udi.Prefix + entityType + "/" + EscapeUriString(id)) =>
Id = id;
/// <summary>