Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ The source generator will look for attributes and implement interfaces that exis
Here's a quick summary about how this library performs with a quick example of marshalling and unmarshalling a simple
DTO object.

| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------------------|-----------:|---------:|---------:|-------:|-------:|----------:|
| Unmarshall_Person_DTO | 821.6 ns | 8.43 ns | 7.47 ns | 0.0553 | - | 696 B |
| Amazon_Unmarshall_Person_DTO | 6,639.1 ns | 79.64 ns | 74.50 ns | 0.9155 | - | 11609 B |
| Marshall_Person_DTO | 689.6 ns | 4.24 ns | 3.76 ns | 0.3052 | 0.0038 | 3840 B |
| Amazon_Marshall_Person_DTO | 5,611.1 ns | 20.94 ns | 17.49 ns | 0.9460 | - | 12076 B |
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------------------|-----------:|----------:|----------:|-------:|-------:|----------:|
| Unmarshall_Person_DTO | 681.6 ns | 6.95 ns | 6.50 ns | 0.0553 | - | 696 B |
| Amazon_Unmarshall_Person_DTO | 6,131.7 ns | 42.92 ns | 38.05 ns | 0.9155 | - | 11610 B |
| Marshall_Person_DTO | 694.6 ns | 13.15 ns | 12.30 ns | 0.3052 | 0.0038 | 3840 B |
| Amazon_Marshall_Person_DTO | 5,824.9 ns | 116.27 ns | 264.80 ns | 0.9460 | - | 12076 B |

## Features:

Expand Down
2 changes: 1 addition & 1 deletion src/DynamoDBGenerator.SourceGenerator/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static class AttributeValueUtilityFactory
public const string FromNullableStringSet = $"{ClassName}.FromNullableStringSet";
public const string FromNumberSet = $"{ClassName}.FromNumberSet";
public const string FromNullableNumberSet = $"{ClassName}.FromNullableNumberSet";
public const string ToNullableStringSet = $"{ClassName}.ToNullableStringSet";
public const string GetAttributeValueOrNull = $"{ClassName}.GetAttributeValueOrNull";
}
public static class ExceptionHelper
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal static IEnumerable<string> CreateClass(DynamoDBMarshallerArguments[] ar
private static CodeFactory CreateCode(TypeIdentifier typeIdentifier, Func<ITypeSymbol, DynamoDbDataMember[]> fn, MarshallerOptions options)
{
var assignments = fn(typeIdentifier.TypeSymbol)
.Select(x => (DDB: x, MethodCall: InvokeUnmarshallMethod(x.DataMember.TypeIdentifier, $"{Dict}.GetValueOrDefault(\"{x.AttributeName}\")", $"\"{x.DataMember.Name}\"", options), x.DataMember.Name))
.Select(x => (DDB: x, MethodCall: InvokeUnmarshallMethod(x.DataMember.TypeIdentifier, $"{AttributeValueUtilityFactory.GetAttributeValueOrNull}({Dict}, \"{x.AttributeName}\")", $"\"{x.DataMember.Name}\"", options), x.DataMember.Name))
.ToArray();

var blockBody =
Expand Down
13 changes: 12 additions & 1 deletion src/DynamoDBGenerator/Internal/MarshallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Amazon.DynamoDBv2.Model;

Expand All @@ -29,6 +30,16 @@ public static class MarshallHelper
: new AttributeValue { M = dict };
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static AttributeValue? GetAttributeValueOrNull(Dictionary<string, AttributeValue> dict, string key)
{
ref readonly var attributeValue = ref CollectionsMarshal.GetValueRefOrNullRef(dict, key);

return Unsafe.IsNullRef(in attributeValue)
? null
: attributeValue;
}

public static AttributeValue FromDictionary<T, TArgument>(
IEnumerable<KeyValuePair<string, T>> dictionary,
TArgument argument,
Expand Down Expand Up @@ -505,7 +516,7 @@ public static TResult[] ToArray<TResult, TArgument>(
var span = CollectionsMarshal.AsSpan(attributeValues);
if (span.Length is 0)
return [];

var elements = new TResult[span.Length];
for (var i = 0; i < span.Length; i++)
elements[i] = resultSelector(span[i], argument, $"{dataMember}[{i}]");
Expand Down
13 changes: 7 additions & 6 deletions tests/DynamoDBGenerator.SourceGenerator.Benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ Intel Core Ultra 9 185H, 1 CPU, 22 logical and 16 physical cores


```
| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|----------------------------- |-----------:|---------:|---------:|-------:|-------:|----------:|
| Unmarshall_Person_DTO | 821.6 ns | 8.43 ns | 7.47 ns | 0.0553 | - | 696 B |
| Amazon_Unmarshall_Person_DTO | 6,639.1 ns | 79.64 ns | 74.50 ns | 0.9155 | - | 11609 B |
| Marshall_Person_DTO | 689.6 ns | 4.24 ns | 3.76 ns | 0.3052 | 0.0038 | 3840 B |
| Amazon_Marshall_Person_DTO | 5,611.1 ns | 20.94 ns | 17.49 ns | 0.9460 | - | 12076 B |

| Method | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|------------------------------|-----------:|----------:|----------:|-------:|-------:|----------:|
| Unmarshall_Person_DTO | 681.6 ns | 6.95 ns | 6.50 ns | 0.0553 | - | 696 B |
| Amazon_Unmarshall_Person_DTO | 6,131.7 ns | 42.92 ns | 38.05 ns | 0.9155 | - | 11610 B |
| Marshall_Person_DTO | 694.6 ns | 13.15 ns | 12.30 ns | 0.3052 | 0.0038 | 3840 B |
| Amazon_Marshall_Person_DTO | 5,824.9 ns | 116.27 ns | 264.80 ns | 0.9460 | - | 12076 B |