Skip to content
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
7 changes: 3 additions & 4 deletions Orm/Xtensive.Orm/IoC/ServiceContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.Collections.Concurrent;
using System.Reflection;
using System.Runtime.InteropServices;
using Xtensive.Collections;
using Xtensive.Core;
using Xtensive.Reflection;
Expand Down Expand Up @@ -120,10 +121,8 @@ private object GetOrCreateInstance(ServiceRegistration registration) =>
private static void Register(Dictionary<Key, List<ServiceRegistration>> types, ServiceRegistration serviceRegistration)
{
var key = GetKey(serviceRegistration.Type, serviceRegistration.Name);
if (!types.TryGetValue(key, out var list)) {
types[key] = list = new List<ServiceRegistration>(1);
}
list.Add(serviceRegistration);
ref var list = ref CollectionsMarshal.GetValueRefOrAddDefault(types, key, out var exists);
(exists ? list : (list = new(1))).Add(serviceRegistration);
}

#endregion
Expand Down
14 changes: 4 additions & 10 deletions Orm/Xtensive.Orm/Modelling/Comparison/Hints/HintSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
// Created by: Alex Yakunin
// Created: 2009.03.26

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Xtensive.Collections;
using System.Linq;
using System.Runtime.InteropServices;
using Xtensive.Core;

namespace Xtensive.Modelling.Comparison.Hints
Expand Down Expand Up @@ -165,10 +161,8 @@ private Dictionary<Type, object> GetNodeHints(Node node)
{
ArgumentNullException.ThrowIfNull(node);

if (!hintMap.TryGetValue(node, out var nodeHintMap)) {
hintMap.Add(node, nodeHintMap = new Dictionary<Type, object>());
}
return nodeHintMap;
ref var nodeHintMap = ref CollectionsMarshal.GetValueRefOrAddDefault(hintMap, node, out var exists);
return exists ? nodeHintMap : (nodeHintMap = new());
}

#region ILockable methods
Expand Down Expand Up @@ -214,4 +208,4 @@ private HintSet()
{
}
}
}
}
35 changes: 19 additions & 16 deletions Orm/Xtensive.Orm/Orm/Building/Builders/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
// Created by: Dmitri Maximov
// Created: 2007.09.26

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xtensive.Core;
using Xtensive.Orm.Building.Definitions;
using Xtensive.Orm.Building.DependencyGraph;
Expand Down Expand Up @@ -242,12 +241,8 @@ private void PreprocessAssociations()
if (paired.Item1.TargetType.IsInterface || typesWithProcessedInheritedAssociations.Contains(paired.Item1.TargetType))
AssociationBuilder.BuildReversedAssociation(context, paired.Item1, paired.Item2);
else {
List<(AssociationInfo, string)> pairs;
if (!pairedAssociationsToReverse.TryGetValue(paired.Item1.TargetType, out pairs)) {
pairs = new List<(AssociationInfo, string)>();
pairedAssociationsToReverse.Add(paired.Item1.TargetType, pairs);
}
pairs.Add(paired);
ref var pairs = ref CollectionsMarshal.GetValueRefOrAddDefault(pairedAssociationsToReverse, paired.Item1.TargetType, out var exists);
(exists ? pairs : (pairs = new(1))).Add(paired);
}
}
continue;
Expand Down Expand Up @@ -483,17 +478,25 @@ private void RegiserReferences(Dictionary<TypeInfo, int> referenceRegistrator, p
var typeImplementors = type.DirectImplementors;
var descendantTypes = type.AllDescendants;
if (typeImplementors.Any()) {
foreach (var implementor in typeImplementors)
if (referenceRegistrator.TryGetValue(implementor, out var refCount))
referenceRegistrator[implementor] = refCount + 1;
foreach (var implementor in typeImplementors) {
ref var refCount = ref CollectionsMarshal.GetValueRefOrNullRef(referenceRegistrator, implementor);
if (!Unsafe.IsNullRef(ref refCount)) {
++refCount;
}
}
}
else {
if (referenceRegistrator.TryGetValue(type, out var refCount))
referenceRegistrator[type] = refCount + 1;
ref var refCount = ref CollectionsMarshal.GetValueRefOrNullRef(referenceRegistrator, type);
if (!Unsafe.IsNullRef(ref refCount)) {
++refCount;
}

if (descendantTypes.Any()) {
foreach (var descendant in descendantTypes) {
if (referenceRegistrator.TryGetValue(descendant, out var refCount1))
referenceRegistrator[descendant] = refCount1 + 1;
ref var refCount1 = ref CollectionsMarshal.GetValueRefOrNullRef(referenceRegistrator, descendant);
if (!Unsafe.IsNullRef(ref refCount1)) {
++refCount1;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
// Created by: Alexey Kulakov
// Created: 2016.06.21

using System;
using System.Collections.Generic;
using System.Linq;
using Xtensive.Core;
using System.Runtime.InteropServices;
using Xtensive.Orm.Model;

namespace Xtensive.Orm.Internals
Expand Down Expand Up @@ -43,8 +40,8 @@ public Identifier(EntityState entityState, AssociationInfo association)
}
}

private readonly IDictionary<Identifier, HashSet<EntityState>> removedReferences = new Dictionary<Identifier, HashSet<EntityState>>();
private readonly IDictionary<Identifier, HashSet<EntityState>> addedReferences = new Dictionary<Identifier, HashSet<EntityState>>();
private readonly Dictionary<Identifier, HashSet<EntityState>> removedReferences = new();
private readonly Dictionary<Identifier, HashSet<EntityState>> addedReferences = new();
private readonly object accessGuard = new();

internal Session Session { get; }
Expand Down Expand Up @@ -125,14 +122,17 @@ private void RegisterRemoveInternal(Identifier oldKey, EntityState referencingSt
return;
}
}
if (removedReferences.TryGetValue(oldKey, out var renivedRefs)) {

ref var renivedRefs = ref CollectionsMarshal.GetValueRefOrAddDefault(removedReferences, oldKey, out var exists);
if (exists) {
if (!renivedRefs.Add(referencingState)) {
throw new InvalidOperationException(Strings.ExReferenceRregistrationErrorReferenceRemovalIsAlreadyRegistered);
}
return;
}
EnsureRegistrationsAllowed();
removedReferences.Add(oldKey, new HashSet<EntityState>{referencingState});
else {
EnsureRegistrationsAllowed();
renivedRefs = [referencingState];
}
}

private void RegisterAddInternal(Identifier newKey, EntityState referencingState)
Expand All @@ -146,14 +146,16 @@ private void RegisterAddInternal(Identifier newKey, EntityState referencingState
}
return;
}
if (addedReferences.TryGetValue(newKey, out var addedRefs)) {
ref var addedRefs = ref CollectionsMarshal.GetValueRefOrAddDefault(addedReferences, newKey, out var exists);
if (exists) {
if (!addedRefs.Add(referencingState)) {
throw new InvalidOperationException(Strings.ExReferenceRegistrationErrorReferenceAdditionIsAlreadyRegistered);
}
return;
}
EnsureRegistrationsAllowed();
addedReferences.Add(newKey, new HashSet<EntityState>{referencingState});
else {
EnsureRegistrationsAllowed();
addedRefs = [referencingState];
}
}

private void Initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
// Created by: Denis Krjuchkov
// Created: 2012.05.17

using System;
using System.Collections.Generic;
using Xtensive.Core;
using System.Runtime.InteropServices;
using Xtensive.Orm.Model;
using Xtensive.Orm.Providers;

Expand All @@ -20,11 +18,8 @@ private sealed class CachingSequenceCollection

public CachingSequence<TValue> GetSequence(SequenceInfo sequenceInfo, IStorageSequenceAccessor accessor)
{
if (!sequences.TryGetValue(sequenceInfo, out var result)) {
result = new CachingSequence<TValue>(accessor, false);
sequences.Add(sequenceInfo, result);
}
return result;
ref var result = ref CollectionsMarshal.GetValueRefOrAddDefault(sequences, sequenceInfo, out var exists);
return exists ? result : (result = new(accessor, false));
}

private void OnTransactionRollbacked(object sender, TransactionEventArgs e)
Expand Down Expand Up @@ -62,4 +57,4 @@ public SessionCachingSequenceProvider(IStorageSequenceAccessor accessor)
this.accessor = accessor;
}
}
}
}
17 changes: 8 additions & 9 deletions Orm/Xtensive.Orm/Orm/Internals/Prefetch/Nodes/ExpressionMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
// Created by: Denis Krjuchkov
// Created: 2012.02.24

using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Xtensive.Collections;
using System.Runtime.InteropServices;

namespace Xtensive.Orm.Internals.Prefetch
{
Expand All @@ -26,12 +24,13 @@ public IEnumerable<Expression> GetChildren(Expression parent)

public void RegisterChild(Expression parent, Expression child)
{
HashSet<Expression> children;
if (!childrenMap.TryGetValue(parent, out children)) {
children = new HashSet<Expression>();
childrenMap.Add(parent, children);
ref var children = ref CollectionsMarshal.GetValueRefOrAddDefault(childrenMap, parent, out var exists);
if (exists) {
children.Add(child);
}
else {
children = [child];
}
children.Add(child);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
// Created by: Dmitri Maximov
// Created: 2008.07.02

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Xtensive.Core;
using Xtensive.Orm.Model;
using System.Linq;

namespace Xtensive.Orm.ReferentialIntegrity
{
Expand Down Expand Up @@ -68,12 +66,12 @@ public void Enqueue(Entity entity, EntityRemoveReason reason)
else {
types.Enqueue(type);
}
if (queue.TryGetValue(type, out var set)) {
ref var set = ref CollectionsMarshal.GetValueRefOrAddDefault(queue, type, out var exists);
if (exists) {
_ = set.Add(entity);
}
else {
set = new HashSet<Entity> { entity };
queue.Add(type, set);
set = [entity];
}

removeReasons[entity] = reason;
Expand All @@ -92,10 +90,8 @@ public void Enqueue(IEnumerable<Entity> entities, EntityRemoveReason reason)
types.Enqueue(type);
}

if (!queue.TryGetValue(type, out var set1)) {
set1 = new HashSet<Entity>();
queue.Add(type, set1);
}
ref var set1Ref = ref CollectionsMarshal.GetValueRefOrAddDefault(queue, type, out var exists);
var set1 = exists ? set1Ref : (set1Ref = []);
foreach (var entity in group) {
removeReasons[entity] = reason;
_ = set1.Add(entity);
Expand Down
24 changes: 11 additions & 13 deletions Orm/Xtensive.Orm/Orm/Linq/Expressions/LocalCollectionExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
// Created by: Alexey Gamzov
// Created: 2009.09.09

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Xtensive.Collections;
using Xtensive.Core;
using System.Runtime.InteropServices;
using Xtensive.Orm.Linq.Expressions.Visitors;

namespace Xtensive.Orm.Linq.Expressions
Expand Down Expand Up @@ -56,22 +52,25 @@ public override Expression Remap(ColumnMap map, Dictionary<Expression, Expressio

public override ParameterizedExpression BindParameter(ParameterExpression parameter, Dictionary<Expression, Expression> processedExpressions)
{
if (processedExpressions.TryGetValue(this, out var value))
return (ParameterizedExpression) value;

ref var resultRef = ref CollectionsMarshal.GetValueRefOrAddDefault(processedExpressions, this, out var exists);
if (exists) {
return (ParameterizedExpression) resultRef;
}
var result = new LocalCollectionExpression(Type, MemberInfo, expressionAsString);
processedExpressions.Add(this, result);
resultRef = result;
result.Fields = Fields.ToDictionary(f=>f.Key, f=>(IMappedExpression)f.Value.BindParameter(parameter, processedExpressions));
return result;
}

public override Expression RemoveOuterParameter(Dictionary<Expression, Expression> processedExpressions)
{
if (processedExpressions.TryGetValue(this, out var value))
return value;
ref var resultRef = ref CollectionsMarshal.GetValueRefOrAddDefault(processedExpressions, this, out var exists);
if (exists) {
return resultRef;
}

var result = new LocalCollectionExpression(Type, MemberInfo, expressionAsString);
processedExpressions.Add(this, result);
resultRef = result;
result.Fields = Fields.ToDictionary(f=>f.Key, f=>(IMappedExpression)f.Value.RemoveOuterParameter(processedExpressions));
return result;
}
Expand All @@ -84,7 +83,6 @@ public LocalCollectionExpression(Type type, MemberInfo memberInfo, Expression so
Fields = new Dictionary<MemberInfo, IMappedExpression>();
MemberInfo = memberInfo;
expressionAsString = sourceExpression.ToString();
;
}

internal override Expression Accept(ExtendedExpressionVisitor visitor) => visitor.VisitLocalCollectionExpression(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created: 2009.05.05

using System.Linq.Expressions;
using System.Runtime.InteropServices;
using Xtensive.Core;
using Xtensive.Orm.Model;
using Xtensive.Orm.Linq.Expressions.Visitors;
Expand Down Expand Up @@ -95,12 +96,13 @@ public override StructureFieldExpression Remap(ColumnMap map, Dictionary<Express

public override StructureFieldExpression BindParameter(ParameterExpression parameter, Dictionary<Expression, Expression> processedExpressions)
{
if (processedExpressions.TryGetValue(this, out var value)) {
return (StructureFieldExpression)value;
ref var resultRef = ref CollectionsMarshal.GetValueRefOrAddDefault(processedExpressions, this, out var exists);
if (exists) {
return (StructureFieldExpression) resultRef;
}

var result = new StructureFieldExpression(PersistentType, Field, Mapping, OuterParameter, DefaultIfEmpty);
processedExpressions.Add(this, result);
resultRef = result;
var processedFields = new PersistentFieldExpression[fields.Count];
int i = 0;
foreach (var field in fields) {
Expand All @@ -120,12 +122,13 @@ public override StructureFieldExpression BindParameter(ParameterExpression param

public override StructureFieldExpression RemoveOuterParameter(Dictionary<Expression, Expression> processedExpressions)
{
if (processedExpressions.TryGetValue(this, out var value)) {
return (StructureFieldExpression) value;
ref var resultRef = ref CollectionsMarshal.GetValueRefOrAddDefault(processedExpressions, this, out var exists);
if (exists) {
return (StructureFieldExpression) resultRef;
}

var result = new StructureFieldExpression(PersistentType, Field, Mapping, OuterParameter, DefaultIfEmpty);
processedExpressions.Add(this, result);
resultRef = result;
var processedFields = new PersistentFieldExpression[fields.Count];
int i = 0;
foreach (var field in fields) {
Expand Down
Loading
Loading