Merge pull request #10861 from nzdev/v9/bugfix/trimstart-arrays

Reduce allocations by using existing CharArrays for TrimStart()
This commit is contained in:
Bjarke Berg
2021-08-17 08:07:14 +02:00
committed by GitHub
4 changed files with 12 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
namespace Umbraco.Cms.Core
namespace Umbraco.Cms.Core
{
public static partial class Constants
{
@@ -78,6 +78,12 @@
/// </summary>
public static readonly char[] TildeForwardSlash = new char[] { '~', '/' };
/// <summary>
/// Char array containing ~ / \
/// </summary>
public static readonly char[] TildeForwardSlashBackSlash = new char[] { '~', '/', '\\' };
/// <summary>
/// Char array containing only ?
/// </summary>

View File

@@ -86,7 +86,7 @@ namespace Umbraco.Cms.Core.Routing
// and then the comparisons in IsMatch can be way faster - and allocate way less strings
const string propertyAlias = Constants.Conventions.Content.UrlAlias;
var test1 = alias.TrimStart('/') + ",";
var test1 = alias.TrimStart(Constants.CharArrays.ForwardSlash) + ",";
var test2 = ",/" + test1; // test2 is ",/alias,"
test1 = "," + test1; // test1 is ",alias,"

View File

@@ -31,7 +31,7 @@ namespace Umbraco.Cms.Core.Routing
public UmbracoRequestPaths(IOptions<GlobalSettings> globalSettings, IHostingEnvironment hostingEnvironment)
{
var applicationPath = hostingEnvironment.ApplicationVirtualPath;
_appPath = applicationPath.TrimStart('/');
_appPath = applicationPath.TrimStart(Constants.CharArrays.ForwardSlash);
_backOfficePath = globalSettings.Value.GetBackOfficePath(hostingEnvironment)
.EnsureStartsWith('/').TrimStart(_appPath.EnsureStartsWith('/')).EnsureStartsWith('/');
@@ -71,7 +71,7 @@ namespace Umbraco.Cms.Core.Routing
/// </remarks>
public bool IsBackOfficeRequest(string absPath)
{
var fullUrlPath = absPath.TrimStart('/');
var fullUrlPath = absPath.TrimStart(Constants.CharArrays.ForwardSlash);
var urlPath = fullUrlPath.TrimStart(_appPath).EnsureStartsWith('/');
// check if this is in the umbraco back office
@@ -108,7 +108,7 @@ namespace Umbraco.Cms.Core.Routing
// Umbraco/MYPLUGINAREA/MYCONTROLLERNAME/{action}/{id}
// so if the path contains at a minimum 3 parts: Umbraco + MYPLUGINAREA + MYCONTROLLERNAME then we will have to assume it is a
// plugin controller for the front-end.
if (urlPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Length >= 3)
if (urlPath.Split(Constants.CharArrays.ForwardSlash, StringSplitOptions.RemoveEmptyEntries).Length >= 3)
{
return false;
}

View File

@@ -141,7 +141,7 @@ namespace Umbraco.Cms.Web.Common.AspNetCore
throw new ArgumentException("The path appears to already be fully qualified. Please remove the call to MapPath");
}
return Path.Combine(root, newPath.TrimStart('~', '/', '\\'));
return Path.Combine(root, newPath.TrimStart(Core.Constants.CharArrays.TildeForwardSlashBackSlash));
}
/// <inheritdoc/>