Skip to content

Commit db43799

Browse files
Henr1k80nul800sebastiaan
authored andcommitted
Avoid some heap allocations
1 parent 2723e4f commit db43799

File tree

10 files changed

+33
-38
lines changed

10 files changed

+33
-38
lines changed

src/Umbraco.Cms.Api.Delivery/Configuration/ConfigureUmbracoMemberAuthenticationDeliveryApiSwaggerGenOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
4848
Id = AuthSchemeName,
4949
}
5050
},
51-
new string[] { }
51+
[]
5252
}
5353
}
5454
};

src/Umbraco.Cms.Api.Management/OpenApi/BackOfficeSecurityRequirementsOperationFilterBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
3838
Type = ReferenceType.SecurityScheme,
3939
Id = ManagementApiConfiguration.ApiSecurityName
4040
}
41-
}, new string[] { }
41+
}, []
4242
}
4343
}
4444
};

src/Umbraco.Core/Composing/TypeFinder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,7 @@ private IEnumerable<Assembly> GetFilteredAssemblies(
233233
excludeFromResults = new HashSet<Assembly>();
234234
}
235235

236-
if (exclusionFilter == null)
237-
{
238-
exclusionFilter = new string[] { };
239-
}
236+
exclusionFilter ??= [];
240237

241238
return GetAllAssemblies()
242239
.Where(x => excludeFromResults.Contains(x) == false

src/Umbraco.Core/Exceptions/InvalidCompositionException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public InvalidCompositionException(string contentTypeAlias, string[] propertyTyp
3535
/// <param name="addedCompositionAlias">The added composition alias.</param>
3636
/// <param name="propertyTypeAliases">The property type aliases.</param>
3737
public InvalidCompositionException(string contentTypeAlias, string? addedCompositionAlias, string[] propertyTypeAliases)
38-
: this(contentTypeAlias, addedCompositionAlias, propertyTypeAliases, new string[0])
38+
: this(contentTypeAlias, addedCompositionAlias, propertyTypeAliases, [])
3939
{
4040
}
4141

src/Umbraco.Core/Extensions/IntExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public static void Times(this int n, Action<int> action)
2727
/// </returns>
2828
public static Guid ToGuid(this int value)
2929
{
30-
var bytes = new byte[16];
31-
BitConverter.GetBytes(value).CopyTo(bytes, 0);
30+
Span<byte> bytes = stackalloc byte[16];
31+
BitConverter.GetBytes(value).CopyTo(bytes);
3232
return new Guid(bytes);
3333
}
3434
}

src/Umbraco.Core/Extensions/StringExtensions.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,16 @@ public static string ReplaceNonAlphanumericChars(this string input, string repla
152152

153153
public static string ReplaceNonAlphanumericChars(this string input, char replacement)
154154
{
155-
var inputArray = input.ToCharArray();
156-
var outputArray = new char[input.Length];
157-
for (var i = 0; i < inputArray.Length; i++)
155+
var chars = input.ToCharArray();
156+
for (var i = 0; i < chars.Length; i++)
158157
{
159-
outputArray[i] = char.IsLetterOrDigit(inputArray[i]) ? inputArray[i] : replacement;
158+
if (!char.IsLetterOrDigit(chars[i]))
159+
{
160+
chars[i] = replacement;
161+
}
160162
}
161163

162-
return new string(outputArray);
164+
return new string(chars);
163165
}
164166

165167
/// <summary>
@@ -209,7 +211,7 @@ public static string AppendQueryStringToUrl(this string url, params string[] que
209211

210212
var nonEmpty = queryStrings.Where(x => !x.IsNullOrWhiteSpace()).ToArray();
211213

212-
if (url.Contains("?"))
214+
if (url.Contains('?'))
213215
{
214216
return url + string.Join("&", nonEmpty).EnsureStartsWith('&');
215217
}
@@ -692,7 +694,7 @@ public static byte[] UrlTokenDecode(this string input)
692694

693695
if (input.Length == 0)
694696
{
695-
return Array.Empty<byte>();
697+
return [];
696698
}
697699

698700
// calc array size - must be groups of 4
@@ -807,7 +809,7 @@ public static string UrlTokenEncode(this byte[] input)
807809
}
808810

809811
// replace chars that would cause problems in URLs
810-
var chArray = new char[pos];
812+
Span<char> chArray = pos <= 1024 ? stackalloc char[pos] : new char[pos];
811813
for (var i = 0; i < pos; i++)
812814
{
813815
var ch = str[i];
@@ -1293,8 +1295,7 @@ internal static Guid CreateGuidFromHash(Guid namespaceId, string name, int versi
12931295
}
12941296

12951297
// most bytes from the hash are copied straight to the bytes of the new GUID (steps 5-7, 9, 11-12)
1296-
var newGuid = new byte[16];
1297-
Array.Copy(hash, 0, newGuid, 0, 16);
1298+
Span<byte> newGuid = hash.AsSpan()[..16];
12981299

12991300
// set the four most significant bits (bits 12 through 15) of the time_hi_and_version field to the appropriate 4-bit version number from Section 4.1.3 (step 8)
13001301
newGuid[6] = (byte)((newGuid[6] & 0x0F) | (version << 4));
@@ -1308,20 +1309,15 @@ internal static Guid CreateGuidFromHash(Guid namespaceId, string name, int versi
13081309
}
13091310

13101311
// Converts a GUID (expressed as a byte array) to/from network order (MSB-first).
1311-
internal static void SwapByteOrder(byte[] guid)
1312+
internal static void SwapByteOrder(Span<byte> guid)
13121313
{
13131314
SwapBytes(guid, 0, 3);
13141315
SwapBytes(guid, 1, 2);
13151316
SwapBytes(guid, 4, 5);
13161317
SwapBytes(guid, 6, 7);
13171318
}
13181319

1319-
private static void SwapBytes(byte[] guid, int left, int right)
1320-
{
1321-
var temp = guid[left];
1322-
guid[left] = guid[right];
1323-
guid[right] = temp;
1324-
}
1320+
private static void SwapBytes(Span<byte> guid, int left, int right) => (guid[left], guid[right]) = (guid[right], guid[left]);
13251321

13261322
/// <summary>
13271323
/// Checks if a given path is a full path including drive letter

src/Umbraco.Core/GuidUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static string ToBase32String(Guid guid, int length = 26)
6363
// a Guid is 3 blocks + 8 bits
6464

6565
// so it turns into a 3*8+2 = 26 chars string
66-
var chars = new char[length];
66+
Span<char> chars = stackalloc char[length];
6767

6868
var i = 0;
6969
var j = 0;

src/Umbraco.Core/HexEncoder.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static class HexEncoder
2828
public static string Encode(byte[] bytes)
2929
{
3030
var length = bytes.Length;
31-
var chars = new char[length * 2];
31+
int charsLength = length * 2;
32+
Span<char> chars = charsLength <= 1024 ? stackalloc char[charsLength] : new char[charsLength];
3233

3334
var index = 0;
3435
for (var i = 0; i < length; i++)
@@ -38,7 +39,7 @@ public static string Encode(byte[] bytes)
3839
chars[index++] = HexLutLo[byteIndex];
3940
}
4041

41-
return new string(chars, 0, chars.Length);
42+
return new string(chars);
4243
}
4344

4445
/// <summary>
@@ -54,7 +55,8 @@ public static string Encode(byte[] bytes)
5455
public static string Encode(byte[] bytes, char separator, int blockSize, int blockCount)
5556
{
5657
var length = bytes.Length;
57-
var chars = new char[(length * 2) + blockCount];
58+
int charsLength = (length * 2) + blockCount;
59+
Span<char> chars = charsLength <= 1024 ? stackalloc char[charsLength] : new char[charsLength];
5860
var count = 0;
5961
var size = 0;
6062
var index = 0;
@@ -80,6 +82,6 @@ public static string Encode(byte[] bytes, char separator, int blockSize, int blo
8082
count++;
8183
}
8284

83-
return new string(chars, 0, chars.Length);
85+
return new string(chars);
8486
}
8587
}

src/Umbraco.Core/Strings/DefaultShortStringHelper.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ protected virtual string CleanString(string text, CleanStringType stringType, st
306306
return text;
307307
}
308308

309-
private string RemoveSurrogatePairs(string text)
309+
private static string RemoveSurrogatePairs(string text)
310310
{
311311
var input = text.AsSpan();
312312
Span<char> output = input.Length <= 1024 ? stackalloc char[input.Length] : new char[text.Length];
@@ -622,7 +622,8 @@ public virtual string SplitPascalCasing(string text, char separator)
622622
}
623623

624624
var input = text.ToCharArray();
625-
var output = new char[input.Length * 2];
625+
int outputLength = input.Length * 2;
626+
Span<char> output = outputLength <= 1024 ? stackalloc char[outputLength] : new char[outputLength];
626627
var opos = 0;
627628
var a = input.Length > 0 ? input[0] : char.MinValue;
628629
var upos = char.IsUpper(a) ? 1 : 0;
@@ -666,7 +667,7 @@ public virtual string SplitPascalCasing(string text, char separator)
666667
output[opos++] = a;
667668
}
668669

669-
return new string(output, 0, opos);
670+
return new string(output[..opos]);
670671
}
671672

672673
#endregion

src/Umbraco.Core/Strings/Utf8ToAsciiConverter.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ public static char[] ToAsciiCharArray(string text, char fail = '?')
5050

5151
// this is faster although it uses more memory
5252
// but... we should be filtering short strings only...
53-
var output = new char[input.Length * 3]; // *3 because of things such as OE
53+
int outputLength = input.Length * 3; // *3 because of things such as OE
54+
Span<char> output = outputLength <= 1024 ? stackalloc char[outputLength] : new char[outputLength];
5455
var len = ToAscii(input, output, fail);
55-
var array = new char[len];
56-
Array.Copy(output, array, len);
57-
return array;
56+
return output[..len].ToArray();
5857

5958
// var temp = new StringBuilder(input.Length + 16); // default is 16, start with at least input length + little extra
6059
// ToAscii(input, temp);

0 commit comments

Comments
 (0)