Skip to content

Commit

Permalink
Free SerializationContext and DeserializationContext between calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Mar 25, 2020
1 parent d9eee30 commit 56e75a7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@ public class SerializationProviderOptions
/// Serializer used if no serializer is found for a type.
/// </summary>
public Type FallbackSerializationProvider { get; set; }

/// <summary>
/// The maximum retained size for serialization and deserialization contexts.
/// </summary>
/// <remarks>
/// This should reflect the expected object graph size for messages.
/// </remarks>
public int MaxSustainedSerializationContextCapacity { get; set; } = 64;
}
}
18 changes: 12 additions & 6 deletions src/Orleans.Core/Serialization/DeserializationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ public static IDeserializationContext CreateNestedContext(

public sealed class DeserializationContext : SerializationContextBase, IDeserializationContext
{
private readonly Dictionary<int, object> taggedObjects;
private Dictionary<int, object> taggedObjects;

public DeserializationContext(SerializationManager serializationManager)
: base(serializationManager)
public DeserializationContext(SerializationManager serializationManager) : base(serializationManager)
{
this.taggedObjects = new Dictionary<int, object>();
this.Reset();
}

/// <inheritdoc />
/// <inheritdoc />
public IBinaryTokenStreamReader StreamReader { get; set; }

Expand Down Expand Up @@ -66,7 +64,15 @@ public object FetchReferencedObject(int offset)

internal void Reset()
{
this.taggedObjects.Clear();
if (this.taggedObjects is null || this.taggedObjects.Count > this.MaxSustainedSerializationContextCapacity)
{
this.taggedObjects = new Dictionary<int, object>();
}
else
{
this.taggedObjects.Clear();
}

this.CurrentObjectOffset = 0;
}

Expand Down
16 changes: 11 additions & 5 deletions src/Orleans.Core/Serialization/SerializationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,23 @@ public Record(int offset)
/// </summary>
public IBinaryTokenStreamWriter StreamWriter { get; set; }

private readonly Dictionary<object, Record> processedObjects;
private Dictionary<object, Record> processedObjects;

public SerializationContext(SerializationManager serializationManager) :
base(serializationManager)
public SerializationContext(SerializationManager serializationManager) : base(serializationManager)
{
processedObjects = new Dictionary<object, Record>(ReferenceEqualsComparer.Instance);
this.Reset();
}

internal void Reset()
{
processedObjects.Clear();
if (this.processedObjects is null || this.processedObjects.Count > this.MaxSustainedSerializationContextCapacity)
{
processedObjects = new Dictionary<object, Record>(ReferenceEqualsComparer.Instance);
}
else
{
processedObjects.Clear();
}
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Orleans.Core/Serialization/SerializationContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ public abstract class SerializationContextBase : ISerializerContext
public IServiceProvider ServiceProvider => this.SerializationManager.ServiceProvider;
public abstract object AdditionalContext { get; }

protected int MaxSustainedSerializationContextCapacity { get; }

protected SerializationContextBase(SerializationManager serializationManager)
{
this.SerializationManager = serializationManager;
this.MaxSustainedSerializationContextCapacity = serializationManager.SerializationProviderOptions.MaxSustainedSerializationContextCapacity;
}
}
}
8 changes: 5 additions & 3 deletions src/Orleans.Core/Serialization/SerializationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ public SerializationManager(
deserializers = new Dictionary<Type, Deserializer>();
grainRefConstructorDictionary = new ConcurrentDictionary<Type, Func<GrainReference, GrainReference>>();

var options = serializationProviderOptions.Value;
this.SerializationProviderOptions = serializationProviderOptions.Value;

fallbackSerializer = GetFallbackSerializer(serviceProvider, options.FallbackSerializationProvider);
fallbackSerializer = GetFallbackSerializer(serviceProvider, SerializationProviderOptions.FallbackSerializationProvider);

RegisterSerializationProviders(options.SerializationProviders);
RegisterSerializationProviders(SerializationProviderOptions.SerializationProviders);
}

internal SerializationProviderOptions SerializationProviderOptions { get; }

public void RegisterSerializers(IApplicationPartManager applicationPartManager)
{
var serializerFeature = applicationPartManager.CreateAndPopulateFeature<SerializerFeature>();
Expand Down

0 comments on commit 56e75a7

Please sign in to comment.