Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve change tracking performance #369

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions src/MongoFramework/Infrastructure/EntityEntryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ public EntityEntry GetEntry<TCollectionBase>(TCollectionBase entity)
var entityDefinition = EntityMapping.GetOrCreateDefinition(collectionType);
var entityId = entityDefinition.GetIdValue(entity);
var defaultIdValue = entityDefinition.GetDefaultId();
var isDefaultId = Equals(entityId, defaultIdValue);
foreach (var entry in entries)
{
if (Equals(entityId, defaultIdValue) && ReferenceEquals(entry.Entity, entity))
if (isDefaultId)
{
return entry;
if (ReferenceEquals(entry.Entity, entity))
{
return entry;
}
}
else
{
Expand Down
25 changes: 24 additions & 1 deletion src/MongoFramework/Infrastructure/Mapping/EntityDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;

namespace MongoFramework.Infrastructure.Mapping;
Expand All @@ -25,9 +26,31 @@ public sealed record PropertyDefinition
public PropertyInfo PropertyInfo { get; init; }
public string ElementName { get; init; }

private Func<object, object> getValueDelegate;
public object GetValue(object entity)
{
return PropertyInfo.GetValue(entity);
if (getValueDelegate is null)
{
// Effectively results in the following expression
// object t => (object)(({PropertyType})t).{PropertyName}
var parameter = Expression.Parameter(typeof(object), "t");
var lambda = Expression.Lambda<Func<object, object>>(
Expression.Convert(
Expression.MakeMemberAccess(
Expression.Convert(
parameter,
PropertyInfo.DeclaringType
),
PropertyInfo
),
typeof(object)
),
parameter
);
getValueDelegate = lambda.Compile();
}

return getValueDelegate(entity);
}

public void SetValue(object entity, object value)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using MongoFramework.Infrastructure;

namespace MongoFramework.Benchmarks;

[SimpleJob(RuntimeMoniker.Net60), MemoryDiagnoser]
public class EntityEntryContainerBenchmark
{
[Params(100, 1_000, 10_000)]
public int EntryCount;

private EntityEntryContainer Container;
private TestEntity[] Entities;

private class TestEntity
{
public string Id { get; set; }
}

[GlobalSetup]
public void Setup()
{
Container = new EntityEntryContainer();
Entities = new TestEntity[EntryCount];
for (var i = 0; i < Entities.Length; i++)
{
Entities[i] = new TestEntity();
}
}

[Benchmark]
public void SetEntityState()
{
for (var i = 0; i < Entities.Length; i++)
{
Container.SetEntityState(Entities[i], EntityEntryState.Added);
}
}
}
Loading