-
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(enumerables): optimize array mapping by using Array.Clone() in c…
…ertain cases (#7) Array.Clone is used if the element mapping is a direct assignment and source type is an array and target an array or an IReadOnlyCollection
- Loading branch information
Showing
9 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Riok.Mapperly/Descriptors/TypeMappings/ArrayCloneMapping.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
using static Riok.Mapperly.Emit.SyntaxFactoryHelper; | ||
|
||
namespace Riok.Mapperly.Descriptors.TypeMappings; | ||
|
||
/// <summary> | ||
/// Represents a mapping from an array to an array of the same type by using Array.Clone. | ||
/// </summary> | ||
public class ArrayCloneMapping : TypeMapping | ||
{ | ||
private const string CloneMethodName = nameof(Array.Clone); | ||
|
||
public ArrayCloneMapping( | ||
ITypeSymbol sourceType, | ||
ITypeSymbol targetType) | ||
: base(sourceType, targetType) | ||
{ | ||
} | ||
|
||
public override ExpressionSyntax Build(ExpressionSyntax source) | ||
{ | ||
return CastExpression( | ||
IdentifierName(TargetType.ToDisplayString()), | ||
InvocationExpression(MemberAccess(source, CloneMethodName))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...sts/_snapshots/UserMethodTest.WithInvalidGenericSignatureShouldDiagnostic.00.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
Diagnostics: [ | ||
{ | ||
Id: RMG001, | ||
Title: Has an unsupported mapping method signature, | ||
Severity: Error, | ||
WarningLevel: 0, | ||
Location: : (10,4)-(10,33), | ||
Description: , | ||
HelpLink: , | ||
MessageFormat: {0} has an unsupported mapping method signature, | ||
Message: ToString has an unsupported mapping method signature, | ||
Category: Mapper, | ||
CustomTags: [] | ||
} | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
...ests/_snapshots/UserMethodTest.WithInvalidGenericSignatureShouldDiagnostic.01.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//HintName: Mapper.g.cs | ||
#nullable enable | ||
public sealed class Mapper : IMapper | ||
{ | ||
public static readonly IMapper Instance = new Mapper(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters