Skip to content

Commit

Permalink
Use Dictionary.TryAdd for minor perf improvements in entityset add
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Svensson committed Oct 26, 2023
1 parent e32f5ca commit 6be87ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/OpenRiaServices.Client/Framework/EntitySet.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using OpenRiaServices.Client.Internal;

#if HAS_COLLECTIONVIEW
Expand All @@ -30,7 +28,7 @@ public abstract class EntitySet : IEnumerable, ICollection, INotifyCollectionCha
private IList _list;
// set of entities, for fast lookup
private HashSet<Entity> _set;
private IDictionary<object, Entity> _identityCache;
private Dictionary<object, Entity> _identityCache;
private readonly HashSet<Entity> _interestingEntities;
private NotifyCollectionChangedEventHandler _collectionChangedEventHandler;

Expand Down Expand Up @@ -738,7 +736,7 @@ internal Entity LoadEntity(Entity entity, LoadBehavior loadBehavior)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.EntityKey_NullIdentity, entity));
}

Entity cachedEntity = null;
Entity cachedEntity;
this._identityCache.TryGetValue(identity, out cachedEntity);
if (cachedEntity == null)
{
Expand Down Expand Up @@ -813,13 +811,11 @@ internal void AddToCache(Entity entity)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.EntityKey_NullIdentity, entity));
}

// Throw if we already have an entity cached with the same identity
if (this._identityCache.ContainsKey(identity))
if (!this._identityCache.TryAdd(identity, entity))
{
// Throw if we already have an entity cached with the same identity
throw new InvalidOperationException(Resource.EntitySet_DuplicateIdentity);
}

this._identityCache[identity] = entity;
}

/// <summary>
Expand Down
29 changes: 29 additions & 0 deletions src/OpenRiaServices.Client/Framework/Polyfills.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;

namespace OpenRiaServices.Client
{
#if !NET
/// <summary>
/// Helper methods to allow "newer"
/// </summary>
static class Polyfills
{
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
// This is expected to be used in scenarios where the add vill almost always succeed, so we pay the cost of an exception
// on duplicates instead of checking if the Key exists first
try
{
dictionary.Add(key, value);
return true;
}
// Duplicate key
catch (ArgumentException)
{
return false;
}
}
}
#endif
}

0 comments on commit 6be87ba

Please # to comment.