Skip to content
Merged
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 @@ -6,6 +6,7 @@

using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using Xtensive.Core;
using Xtensive.Linq;
using Xtensive.Orm.Internals;
Expand Down Expand Up @@ -384,10 +385,8 @@ internal protected override Expression VisitEntityExpression(EntityExpression ex
/// <exception cref="InvalidOperationException">Unable to materialize Entity.</exception>
private Expression CreateEntity(IEntityExpression expression, Expression tupleExpression)
{
if (!entityRegistry.TryGetValue(expression, out var index)) {
index = entityRegistry.Count;
entityRegistry.Add(expression, index);
}
ref var indexRef = ref CollectionsMarshal.GetValueRefOrAddDefault(entityRegistry, expression, out var exists);
Copy link
Copy Markdown

@snaumenko-st snaumenko-st May 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO too dangerous. And my previous benchmarks haven't indicated the significant performance boost. So probably it's not worth it

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can it be dangerous?
.NET implementation of Dictionary.TryAdd() uses it inside

And here is some benchmark showing the performance gain https://matthewcrews.com/blog/2022/03/performance-of-key-value-lookups-types/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easy to use it wrong way. That's it

var index = exists ? indexRef : (indexRef = entityRegistry.Count - 1);

if (itemMaterializationContextParameter==null)
throw new InvalidOperationException(
Expand Down
Loading