Skip to content

Commit f4e805d

Browse files
committed
less allocations
1 parent 5f0cebd commit f4e805d

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

src/AutoMapper/Configuration/INamingConvention.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Text.RegularExpressions;
23
namespace AutoMapper
34
{
@@ -15,23 +16,31 @@ public interface INamingConvention
1516
}
1617
public class ExactMatchNamingConvention : INamingConvention
1718
{
18-
public static readonly ExactMatchNamingConvention Instance = new ExactMatchNamingConvention();
19+
public static readonly ExactMatchNamingConvention Instance = new();
1920
public Regex SplittingExpression { get; }
2021
public string SeparatorCharacter => "";
2122
public string ReplaceValue(Match match) => match.Value;
2223
}
2324
public class PascalCaseNamingConvention : INamingConvention
2425
{
25-
private static readonly Regex PascalCase = new Regex(@"(\p{Lu}+(?=$|\p{Lu}[\p{Ll}0-9])|\p{Lu}?[\p{Ll}0-9]+)");
26-
public static readonly PascalCaseNamingConvention Instance = new PascalCaseNamingConvention();
26+
private static readonly Regex PascalCase = new(@"(\p{Lu}+(?=$|\p{Lu}[\p{Ll}0-9])|\p{Lu}?[\p{Ll}0-9]+)");
27+
public static readonly PascalCaseNamingConvention Instance = new();
2728
public Regex SplittingExpression { get; } = PascalCase;
2829
public string SeparatorCharacter => string.Empty;
29-
public string ReplaceValue(Match match) => match.Value[0].ToString().ToUpper() + match.Value.Substring(1);
30+
public string ReplaceValue(Match match)
31+
{
32+
var source = match.Value;
33+
return string.Create(source.Length, source, static (buffer, state) =>
34+
{
35+
buffer[0] = char.ToUpper(state[0]);
36+
((ReadOnlySpan<char>)state)[1..].CopyTo(buffer[1..]);
37+
});
38+
}
3039
}
3140
public class LowerUnderscoreNamingConvention : INamingConvention
3241
{
33-
private static readonly Regex LowerUnderscore = new Regex(@"[\p{Ll}\p{Lu}0-9]+(?=_?)");
34-
public static readonly LowerUnderscoreNamingConvention Instance = new LowerUnderscoreNamingConvention();
42+
private static readonly Regex LowerUnderscore = new(@"[\p{Ll}\p{Lu}0-9]+(?=_?)");
43+
public static readonly LowerUnderscoreNamingConvention Instance = new();
3544
public Regex SplittingExpression { get; } = LowerUnderscore;
3645
public string SeparatorCharacter => "_";
3746
public string ReplaceValue(Match match) => match.Value.ToLower();

src/AutoMapper/TypeMap.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace AutoMapper
1212
using static Execution.ExpressionBuilder;
1313
using Configuration;
1414
using Features;
15-
using QueryableExtensions.Impl;
1615
using Internal;
1716
/// <summary>
1817
/// Main configuration object holding all mapping configuration for a source and destination type

src/Benchmark/Program.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using Benchmark.Flattening;
34

45
namespace Benchmark
@@ -14,12 +15,16 @@ public static void Main(string[] args)
1415
{ "Complex", new IObjectToObjectMapper[] { new ComplexTypeMapper(), new ManualComplexTypeMapper() } },
1516
{ "Deep", new IObjectToObjectMapper[] { new DeepTypeMapper(), new ManualDeepTypeMapper() } }
1617
};
17-
foreach(var pair in mappers)
18+
while (true)
1819
{
19-
foreach(var mapper in pair.Value)
20+
foreach (var pair in mappers)
2021
{
21-
new BenchEngine(mapper, pair.Key).Start();
22+
foreach (var mapper in pair.Value)
23+
{
24+
new BenchEngine(mapper, pair.Key).Start();
25+
}
2226
}
27+
Console.ReadLine();
2328
}
2429
}
2530
}

0 commit comments

Comments
 (0)