Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using YamlDotNet.Core;
Expand All @@ -41,6 +42,11 @@ public class FullObjectGraphTraversalStrategy : IObjectGraphTraversalStrategy
private readonly INamingConvention namingConvention;
private readonly IObjectFactory objectFactory;

// The key node emitted for a property is fully determined by the property's (immutable) name,
// so cache it per descriptor instead of allocating a fresh ObjectDescriptor for every property
// of every object during serialization.
private readonly ConcurrentDictionary<IPropertyDescriptor, ObjectDescriptor> keyNodeDescriptors = new ConcurrentDictionary<IPropertyDescriptor, ObjectDescriptor>();

public FullObjectGraphTraversalStrategy(ITypeInspector typeDescriptor, ITypeResolver typeResolver, int maxRecursion,
INamingConvention namingConvention, IObjectFactory objectFactory)
{
Expand Down Expand Up @@ -269,7 +275,8 @@ protected virtual void TraverseProperties<TContext>(IObjectDescriptor value, IOb
var propertyValue = propertyDescriptor.Read(source);
if (visitor.EnterMapping(propertyDescriptor, propertyValue, context, serializer))
{
Traverse(null, propertyDescriptor.Name, new ObjectDescriptor(propertyDescriptor.Name, typeof(string), typeof(string), ScalarStyle.Plain), visitor, context, path, serializer);
var keyNode = keyNodeDescriptors.GetOrAdd(propertyDescriptor, static pd => new ObjectDescriptor(pd.Name, typeof(string), typeof(string), ScalarStyle.Plain));
Traverse(null, propertyDescriptor.Name, keyNode, visitor, context, path, serializer);
Traverse(propertyDescriptor, propertyDescriptor.Name, propertyValue, visitor, context, path, serializer);
}
}
Expand Down
Loading