Skip to content

Commit

Permalink
chore: fix non-threadsafe classes
Browse files Browse the repository at this point in the history
  • Loading branch information
filzrev committed Feb 26, 2025
1 parent 2a2ed1d commit ea6269d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.Reflection;
using System.Reflection.Emit;
using Docfx.YamlSerialization.Helpers;
Expand All @@ -20,9 +21,9 @@ public class EmitGenericCollectionNodeDeserializer : INodeDeserializer
private readonly IObjectFactory _objectFactory;
private readonly INamingConvention _enumNamingConvention;
private readonly ITypeInspector _typeDescriptor;
private readonly Dictionary<Type, Type?> _gpCache =
private readonly ConcurrentDictionary<Type, Type?> _gpCache =
new();
private readonly Dictionary<Type, Action<IParser, Type, Func<IParser, Type, object?>, object?, INamingConvention, ITypeInspector>> _actionCache =
private readonly ConcurrentDictionary<Type, Action<IParser, Type, Func<IParser, Type, object?>, object?, INamingConvention, ITypeInspector>> _actionCache =
new();

public EmitGenericCollectionNodeDeserializer(IObjectFactory objectFactory, INamingConvention enumNamingConvention, ITypeInspector typeDescriptor)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.ComponentModel;
using System.Reflection;
using System.Reflection.Emit;
Expand All @@ -16,8 +17,8 @@ public class EmitGenericDictionaryNodeDeserializer : INodeDeserializer
private static readonly MethodInfo DeserializeHelperMethod =
typeof(EmitGenericDictionaryNodeDeserializer).GetMethod(nameof(DeserializeHelper))!;
private readonly IObjectFactory _objectFactory;
private readonly Dictionary<Type, Type[]?> _gpCache = [];
private readonly Dictionary<Tuple<Type, Type>, Action<IParser, Type, Func<IParser, Type, object?>, object?>> _actionCache = [];
private readonly ConcurrentDictionary<Type, Type[]?> _gpCache = [];
private readonly ConcurrentDictionary<Tuple<Type, Type>, Action<IParser, Type, Func<IParser, Type, object?>, object?>> _actionCache = [];

public EmitGenericDictionaryNodeDeserializer(IObjectFactory objectFactory)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.Reflection;
using System.Reflection.Emit;

Expand All @@ -10,7 +11,7 @@ namespace Docfx.YamlSerialization.ObjectFactories;

public class DefaultEmitObjectFactory : ObjectFactoryBase
{
private readonly Dictionary<Type, Func<object>> _cache = [];
private readonly ConcurrentDictionary<Type, Func<object>> _cache = [];
private static Type[] EmptyTypes => Type.EmptyTypes;

public override object Create(Type type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
Expand Down Expand Up @@ -32,7 +33,7 @@ public class FullObjectGraphTraversalStrategy : IObjectGraphTraversalStrategy
private readonly IObjectFactory _objectFactory;

// private readonly Dictionary<Tuple<Type, Type>, Action<IPropertyDescriptor?, IObjectDescriptor, IObjectGraphVisitor, Type, Type, IObjectGraphVisitorContext, Stack<FullObjectGraphTraversalStrategy.ObjectPathSegment>, ObjectSerializer>> _behaviorCache = new();
private readonly Dictionary<Tuple<Type, Type, Type>, Action<FullObjectGraphTraversalStrategy, object, IObjectGraphVisitor, INamingConvention, IObjectGraphVisitorContext, Stack<ObjectPathSegment>, ObjectSerializer>> _traverseGenericDictionaryCache = new();
private readonly ConcurrentDictionary<Tuple<Type, Type, Type>, Action<FullObjectGraphTraversalStrategy, object, IObjectGraphVisitor, INamingConvention, IObjectGraphVisitorContext, Stack<ObjectPathSegment>, ObjectSerializer>> _traverseGenericDictionaryCache = new();

protected YamlSerializer Serializer { get; }

Expand Down
3 changes: 2 additions & 1 deletion src/Docfx.YamlSerialization/YamlSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using Docfx.YamlSerialization.Helpers;
using Docfx.YamlSerialization.ObjectDescriptors;
using Docfx.YamlSerialization.ObjectGraphTraversalStrategies;
Expand Down Expand Up @@ -141,7 +142,7 @@ private IEventEmitter CreateEventEmitter()
{
return new TypeAssigningEventEmitter(
writer,
new Dictionary<Type, TagName>(),
new ConcurrentDictionary<Type, TagName>(),
quoteNecessaryStrings: false,
quoteYaml1_1Strings: false,
defaultScalarStyle: ScalarStyle.Any,
Expand Down

0 comments on commit ea6269d

Please sign in to comment.