Skip to content

Commit

Permalink
Fixed Caching Issues for Better Performance. (#381)
Browse files Browse the repository at this point in the history
* Aaded caching to write member accessors.

* Removed type member processing if custom serialization is provided.
  • Loading branch information
Mike-EEE authored Mar 19, 2020
1 parent 1127100 commit ecb712e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
28 changes: 17 additions & 11 deletions src/ExtendedXmlSerializer/ContentModel/Members/TypeMembers.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
using ExtendedXmlSerializer.Core.Sources;
using ExtendedXmlSerializer.ExtensionModel.Xml;
using JetBrains.Annotations;
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using ExtendedXmlSerializer.Core.Sources;
using JetBrains.Annotations;

namespace ExtendedXmlSerializer.ContentModel.Members
{
sealed class TypeMembers : CacheBase<TypeInfo, ImmutableArray<IMember>>, ITypeMembers
{
readonly Func<IMember, bool> _specification;
readonly ITypeMemberSource _source;
readonly IContainsCustomSerialization _custom;
readonly Func<IMember, bool> _specification;
readonly ITypeMemberSource _source;

[UsedImplicitly]
public TypeMembers(IValidMemberSpecification specification, ITypeMemberSource source)
: this(specification.IsSatisfiedBy, source) {}
public TypeMembers(IValidMemberSpecification specification, IContainsCustomSerialization custom,
ITypeMemberSource source) : this(custom, specification.IsSatisfiedBy, source) {}

public TypeMembers(Func<IMember, bool> specification, ITypeMemberSource source)
public TypeMembers(IContainsCustomSerialization custom, Func<IMember, bool> specification,
ITypeMemberSource source)
{
_custom = custom;
_specification = specification;
_source = source;
}

protected override ImmutableArray<IMember> Create(TypeInfo parameter)
{
var result = _source.Get(parameter)
.Where(_specification)
.OrderBy(x => x.Order)
.ToImmutableArray();
var result = _custom.IsSatisfiedBy(parameter)
? ImmutableArray<IMember>.Empty
: _source.Get(parameter)
.Where(_specification)
.OrderBy(x => x.Order)
.ToImmutableArray();
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using ExtendedXmlSerializer.Core.Sources;
using ExtendedXmlSerializer.ReflectionModel;
using JetBrains.Annotations;

namespace ExtendedXmlSerializer.ContentModel.Members
{
class WritableMemberAccessors : IMemberAccessors
class WritableMemberAccessors : ReferenceCacheBase<IMember, IMemberAccess>, IMemberAccessors
{
readonly IAllowedMemberValues _emit;
readonly IGetterFactory _getter;
Expand All @@ -22,7 +23,7 @@ public WritableMemberAccessors(IAllowedMemberValues emit, IGetterFactory getter,
_setter = setter;
}

public IMemberAccess Get(IMember parameter)
protected override IMemberAccess Create(IMember parameter)
=> new MemberAccess(_emit.Get(parameter.Metadata), _getter.Get(parameter.Metadata),
_setter.Get(parameter.Metadata));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ sealed class Dto
{
Collection<string> _names;

public Dto()
{
_names = new Collection<string>();
}
public Dto() => _names = new Collection<string>();

[UsedImplicitly]
public Collection<string> Names
{
get => _names;
Expand Down
27 changes: 27 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue375Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
public sealed class Issue375Tests
{
[Fact]
public void Verify()
{
/*var instance = new Subject{ Inner = new Inner() };
var serializer = new ConfigurationContainer().Create().ForTesting();
serializer.Serialize(instance);
serializer.Serialize(instance);*/

}

/*sealed class Subject
{
public Inner Inner { get; set; }
}
sealed class Inner {}*/
}
}

0 comments on commit ecb712e

Please sign in to comment.