-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Optimization/grouped extensions #3260
Changes from 10 commits
afa5047
e8cd4f7
cbc80ef
4079a75
f0b5bf0
6129c69
fc7624f
4e05301
d129d74
c017e48
1a88b0b
a8e4d67
6400993
630ffaf
3d4b0d6
b57db84
9bae4eb
ef1cd1a
e088c5b
9a8beef
d111b06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,11 @@ | |
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Contracts; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
|
||
#nullable enable | ||
|
||
namespace Microsoft.Toolkit.Collections | ||
{ | ||
|
@@ -22,8 +26,18 @@ public static class ObservableGroupedCollectionExtensions | |
/// <param name="key">The key of the group to query.</param> | ||
/// <returns>The first group matching <paramref name="key"/>.</returns> | ||
/// <exception cref="InvalidOperationException">The target group does not exist.</exception> | ||
[Pure] | ||
public static ObservableGroup<TKey, TValue> First<TKey, TValue>(this ObservableGroupedCollection<TKey, TValue> source, TKey key) | ||
=> source.First(group => GroupKeyPredicate(group, key)); | ||
{ | ||
ObservableGroup<TKey, TValue>? group = source.FirstOrDefault(key); | ||
|
||
if (group is null) | ||
{ | ||
ThrowArgumentExceptionForKeyNotFound(); | ||
} | ||
|
||
return group!; | ||
} | ||
|
||
/// <summary> | ||
/// Return the first group with <paramref name="key"/> key or null if not found. | ||
|
@@ -33,8 +47,34 @@ public static ObservableGroup<TKey, TValue> First<TKey, TValue>(this ObservableG | |
/// <param name="source">The source <see cref="ObservableGroupedCollection{TKey, TValue}"/> instance.</param> | ||
/// <param name="key">The key of the group to query.</param> | ||
/// <returns>The first group matching <paramref name="key"/> or null.</returns> | ||
public static ObservableGroup<TKey, TValue> FirstOrDefault<TKey, TValue>(this ObservableGroupedCollection<TKey, TValue> source, TKey key) | ||
=> source.FirstOrDefault(group => GroupKeyPredicate(group, key)); | ||
[Pure] | ||
public static ObservableGroup<TKey, TValue>? FirstOrDefault<TKey, TValue>(this ObservableGroupedCollection<TKey, TValue> source, TKey key) | ||
{ | ||
if (source.TryGetList(out var list)) | ||
{ | ||
foreach (var group in list!) | ||
{ | ||
if (EqualityComparer<TKey>.Default.Equals(group.Key, key)) | ||
{ | ||
return group; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
return FirstOrDefaultWithLinq(source, key); | ||
} | ||
|
||
/// <summary> | ||
/// Slow path for <see cref="First{TKey,TValue}"/>. | ||
/// </summary> | ||
[Pure] | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static ObservableGroup<TKey, TValue>? FirstOrDefaultWithLinq<TKey, TValue>(ObservableGroupedCollection<TKey, TValue> source, TKey key) | ||
{ | ||
return source.FirstOrDefault(group => EqualityComparer<TKey>.Default.Equals(group.Key, key)); | ||
michael-hawker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// <summary> | ||
/// Return the element at position <paramref name="index"/> from the first group with <paramref name="key"/> key. | ||
|
@@ -47,6 +87,7 @@ public static ObservableGroup<TKey, TValue> FirstOrDefault<TKey, TValue>(this Ob | |
/// <returns>The element.</returns> | ||
/// <exception cref="InvalidOperationException">The target group does not exist.</exception> | ||
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than zero or <paramref name="index"/> is greater than the group elements' count.</exception> | ||
[Pure] | ||
public static TValue ElementAt<TKey, TValue>( | ||
this ObservableGroupedCollection<TKey, TValue> source, | ||
TKey key, | ||
|
@@ -62,18 +103,21 @@ public static TValue ElementAt<TKey, TValue>( | |
/// <param name="key">The key of the group to query.</param> | ||
/// <param name="index">The index of the item from the targeted group.</param> | ||
/// <returns>The element or default(TValue) if it does not exist.</returns> | ||
[Pure] | ||
public static TValue ElementAtOrDefault<TKey, TValue>( | ||
this ObservableGroupedCollection<TKey, TValue> source, | ||
TKey key, | ||
int index) | ||
{ | ||
var existingGroup = source.FirstOrDefault(key); | ||
if (existingGroup is null) | ||
var group = source.FirstOrDefault(key); | ||
|
||
if (group is null || | ||
(uint)index >= (uint)group.Count) | ||
{ | ||
return default; | ||
return default!; | ||
} | ||
|
||
return existingGroup.ElementAtOrDefault(index); | ||
return group[index]; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -89,7 +133,7 @@ public static ObservableGroup<TKey, TValue> AddGroup<TKey, TValue>( | |
this ObservableGroupedCollection<TKey, TValue> source, | ||
TKey key, | ||
TValue value) | ||
=> AddGroup(source, key, new[] { value }); | ||
=> AddGroup(source, key, new[] { value }); | ||
|
||
/// <summary> | ||
/// Adds a key-collection <see cref="ObservableGroup{TKey, TValue}"/> item into a target <see cref="ObservableGroupedCollection{TKey, TValue}"/>. | ||
|
@@ -141,15 +185,17 @@ public static ObservableGroup<TKey, TValue> AddItem<TKey, TValue>( | |
TKey key, | ||
TValue item) | ||
{ | ||
var existingGroup = source.FirstOrDefault(key); | ||
if (existingGroup is null) | ||
var group = source.FirstOrDefault(key); | ||
|
||
if (group is null) | ||
{ | ||
existingGroup = new ObservableGroup<TKey, TValue>(key); | ||
source.Add(existingGroup); | ||
group = new ObservableGroup<TKey, TValue>(key); | ||
source.Add(group); | ||
} | ||
|
||
existingGroup.Add(item); | ||
return existingGroup; | ||
group.Add(item); | ||
|
||
return group; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -172,6 +218,7 @@ public static ObservableGroup<TKey, TValue> InsertItem<TKey, TValue>( | |
{ | ||
var existingGroup = source.First(key); | ||
existingGroup.Insert(index, item); | ||
|
||
return existingGroup; | ||
} | ||
|
||
|
@@ -195,6 +242,7 @@ public static ObservableGroup<TKey, TValue> SetItem<TKey, TValue>( | |
{ | ||
var existingGroup = source.First(key); | ||
existingGroup[index] = item; | ||
|
||
return existingGroup; | ||
} | ||
|
||
|
@@ -209,11 +257,38 @@ public static ObservableGroup<TKey, TValue> SetItem<TKey, TValue>( | |
public static void RemoveGroup<TKey, TValue>( | ||
this ObservableGroupedCollection<TKey, TValue> source, | ||
TKey key) | ||
{ | ||
if (source.TryGetList(out var list)) | ||
{ | ||
var index = 0; | ||
foreach (var group in list!) | ||
{ | ||
if (EqualityComparer<TKey>.Default.Equals(group.Key, key)) | ||
{ | ||
source.RemoveAt(index); | ||
|
||
return; | ||
} | ||
|
||
index++; | ||
} | ||
} | ||
else | ||
{ | ||
RemoveGroupWithLinq(source, key); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Slow path for <see cref="RemoveGroup{TKey,TValue}"/>. | ||
/// </summary> | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static void RemoveGroupWithLinq<TKey, TValue>(ObservableGroupedCollection<TKey, TValue> source, TKey key) | ||
{ | ||
var index = 0; | ||
foreach (var group in source) | ||
{ | ||
if (GroupKeyPredicate(group, key)) | ||
if (EqualityComparer<TKey>.Default.Equals(group.Key, key)) | ||
{ | ||
source.RemoveAt(index); | ||
return; | ||
|
@@ -238,15 +313,51 @@ public static void RemoveItem<TKey, TValue>( | |
TKey key, | ||
TValue item, | ||
bool removeGroupIfEmpty = true) | ||
{ | ||
if (source.TryGetList(out var list)) | ||
{ | ||
var index = 0; | ||
foreach (var group in list!) | ||
{ | ||
if (EqualityComparer<TKey>.Default.Equals(group.Key, key)) | ||
{ | ||
if (group.Remove(item) && | ||
removeGroupIfEmpty && | ||
group.Count == 0) | ||
{ | ||
source.RemoveAt(index); | ||
} | ||
|
||
return; | ||
} | ||
|
||
index++; | ||
} | ||
} | ||
else | ||
{ | ||
RemoveItemWithLinq(source, key, item, removeGroupIfEmpty); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Slow path for <see cref="RemoveItem{TKey,TValue}"/>. | ||
/// </summary> | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static void RemoveItemWithLinq<TKey, TValue>( | ||
ObservableGroupedCollection<TKey, TValue> source, | ||
TKey key, | ||
TValue item, | ||
bool removeGroupIfEmpty) | ||
{ | ||
var index = 0; | ||
foreach (var group in source) | ||
{ | ||
if (GroupKeyPredicate(group, key)) | ||
if (EqualityComparer<TKey>.Default.Equals(group.Key, key)) | ||
{ | ||
group.Remove(item); | ||
|
||
if (removeGroupIfEmpty && group.Count == 0) | ||
if (group.Remove(item) && | ||
removeGroupIfEmpty && | ||
group.Count == 0) | ||
{ | ||
source.RemoveAt(index); | ||
} | ||
|
@@ -268,16 +379,53 @@ public static void RemoveItem<TKey, TValue>( | |
/// <param name="key">The key of the group where the item at <paramref name="index"/> should be removed.</param> | ||
/// <param name="index">The index of the item to remove in the group.</param> | ||
/// <param name="removeGroupIfEmpty">If true (default value), the group will be removed once it becomes empty.</param> | ||
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than zero or <paramref name="index"/> is greater than the group elements' count.</exception> | ||
public static void RemoveItemAt<TKey, TValue>( | ||
this ObservableGroupedCollection<TKey, TValue> source, | ||
TKey key, | ||
int index, | ||
bool removeGroupIfEmpty = true) | ||
{ | ||
if (source.TryGetList(out var list)) | ||
{ | ||
var groupIndex = 0; | ||
foreach (var group in list!) | ||
{ | ||
if (EqualityComparer<TKey>.Default.Equals(group.Key, key)) | ||
{ | ||
group.RemoveAt(index); | ||
|
||
if (removeGroupIfEmpty && group.Count == 0) | ||
{ | ||
source.RemoveAt(groupIndex); | ||
} | ||
|
||
return; | ||
} | ||
|
||
groupIndex++; | ||
} | ||
} | ||
else | ||
{ | ||
RemoveItemAtWithLinq(source, key, index, removeGroupIfEmpty); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Slow path for <see cref="RemoveItemAt{TKey,TValue}"/>. | ||
/// </summary> | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static void RemoveItemAtWithLinq<TKey, TValue>( | ||
ObservableGroupedCollection<TKey, TValue> source, | ||
TKey key, | ||
int index, | ||
bool removeGroupIfEmpty) | ||
{ | ||
var groupIndex = 0; | ||
foreach (var group in source) | ||
{ | ||
if (GroupKeyPredicate(group, key)) | ||
if (EqualityComparer<TKey>.Default.Equals(group.Key, key)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we achieve the same performances by keeping The code feel easier to read with a named method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally the JIT should be able to produce the same code at the end, yeah. Though also from a style perspective, personally I feel like just leaving There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I usually prefer having a named method to describe the intent (even if it here, it seems that it missed the point ☺). I find it make the code easier to maintain because we have a single update point if one day we change the equality logic (even if it here it is unlikely to happen). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, yeah that makes sense. I guess I just tend to do the opposite in general out of habit when the wrapped code is so short just to give the JIT a litte nudge, but either approach is fine, really 😊 Not sure what you mean by "even if it here, it seems that it missed the point", would you like me to revert that change then and reintroduce that wrapping method, or do we want to leave this as is for now? Let me know and I'll make the changes if needed! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I was meaning that the naming wasn't that good since you said you had to look several times at the implementation to understand what it was doing 🙂. We can leave this code as-is for now. No need to push another update only for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, perfect then! 😊 |
||
{ | ||
group.RemoveAt(index); | ||
|
||
|
@@ -293,7 +441,13 @@ public static void RemoveItemAt<TKey, TValue>( | |
} | ||
} | ||
|
||
private static bool GroupKeyPredicate<TKey, TValue>(ObservableGroup<TKey, TValue> group, TKey expectedKey) | ||
=> EqualityComparer<TKey>.Default.Equals(group.Key, expectedKey); | ||
/// <summary> | ||
/// Throws a new <see cref="InvalidOperationException"/> when a key is not found. | ||
/// </summary> | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static void ThrowArgumentExceptionForKeyNotFound() | ||
{ | ||
throw new InvalidOperationException("The requested key was not present in the collection"); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sergio0694 Guard API or no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it might be best for us not to use that in this case so that we could have a clearer error message for users. As in, this way we'll get "The desired key is not present in the collection" rather than a more obscure "Parameter group must not be null". I mean, I could see devs going like, what group? Where? 🤔