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

Optimization/grouped extensions #3260

Conversation

Sergio0694
Copy link
Member

Follow up to #3246

This PR just includes some speed/memory optimizations to the extensions for the ObservableGroupedCollection<TKey, TValue> type. In particular, it leverages the fact that the underlying collection is almost always a List<T> instance to obtain a struct enumerator and use that to perform the requested query. This allows the operation to be performed faster (as the MoveNext and Current are no longer callvirt-s and hence can be inlined) and without allocations (the enumerator is just a value type now). Here's a benchmark to show the OG/PR First method:

Method N Mean Error StdDev Ratio Gen 0 Allocated
OG 1024 11.477 us 0.1461 us 0.1366 us 1.00 0.0305 128 B
PR 1024 4.728 us 0.0143 us 0.0133 us 0.41 - -
OG 10000 119.007 us 0.9308 us 0.7267 us 1.00 - 129 B
PR 10000 48.001 us 0.1269 us 0.1125 us 0.40 - -

You can see how in this example, no matter the size of the grouped collection, the updated First method is consistently over twice as fast, and without any memory allocations at all.

PR Type

What kind of change does this PR introduce?

  • Refactoring (no functional changes, no api changes)

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Pull Request has been submitted to the documentation repository instructions. Link:
  • Sample in sample app has been added / updated (for bug fixes / features)
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • If new feature, add to Feature List
  • Contains NO breaking changes

Other information

Note to self: once #3167 gets merged with the .NET Standard 2.1 multitarget, these extensions need to be decorated with nullability attributes for uncostrained generic type parameters as well.

@ghost
Copy link

ghost commented Apr 30, 2020

Thanks Sergio0694 for opening a Pull Request! The reviewers will test the PR and highlight if there is any conflict or changes required. If the PR is approved we will proceed to merge the pull request 🙌

@ghost ghost assigned Kyaa-dost Apr 30, 2020
Copy link
Contributor

@vgromfeld vgromfeld left a comment

Choose a reason for hiding this comment

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

Nice performance improvements !

{
var groupIndex = 0;
foreach (var group in source)
{
if (GroupKeyPredicate(group, key))
if (EqualityComparer<TKey>.Default.Equals(group.Key, key))
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we achieve the same performances by keeping GroupKeyPredicate(group, key) here and adding the [MethodImpl(MethodImplOptions.AggressiveInlining)] to the GroupKeyPredicate() method ?

The code feel easier to read with a named method

Copy link
Member Author

Choose a reason for hiding this comment

The 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 EqualityComparer<T> call directly makes the code clearer in its behavior, instead of hiding the logic away. Like, while working on this PR I found myself looking up that method multiple times as I wasn't sure exactly what its semantics was or how it was working exactly. Since it's just a wrapper on this call and it's only shorter by a few characters, I think I'd prefer to just leave the EqualityComparer<T> invocation explicit. Just my two cents though, so let me know what you think! 😊

Copy link
Contributor

Choose a reason for hiding this comment

The 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).

Copy link
Member Author

Choose a reason for hiding this comment

The 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!

Copy link
Contributor

Choose a reason for hiding this comment

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

even if it here, it seems that it missed the point;

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, perfect then! 😊
Thank you for the review!

@michael-hawker michael-hawker added this to the 6.1 milestone May 18, 2020

if (group is null)
{
ThrowArgumentExceptionForKeyNotFound();
Copy link
Member

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?

Copy link
Member Author

@Sergio0694 Sergio0694 May 26, 2020

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? 🤔

@michael-hawker michael-hawker merged commit 3add501 into CommunityToolkit:master May 26, 2020
ghost pushed a commit that referenced this pull request Aug 20, 2020
Quick-fix PR to remove an unnecessary `[MethodImpl(MethodImplOptions.NoInlining)]` attribute from a throw helper method.
That attribute caused the JIT not to see the throw in the body, resulting in the caller branch assuming the forward path being taken (the faulty one). This produced worse codegen, as the non-faulting path was always forced to have an extra jump ahead.

Also did another small codegen tweak and added a more detailed exception message for users.

## Leftover fix from #3260
<!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. -->
 

<!-- Add a brief overview here of the feature/bug & fix. -->

## PR Type
What kind of change does this PR introduce?
<!-- Please uncomment one or more that apply to this PR. -->

<!-- - Bugfix -->
<!-- - Feature -->
- Optimization
<!-- - Code style update (formatting) -->
<!-- - Refactoring (no functional changes, no api changes) -->
<!-- - Build or CI related changes -->
<!-- - Documentation content changes -->
<!-- - Sample app changes -->
<!-- - Other... Please describe: -->


## What is the current behavior?
<!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->
Unoptimal codegen for this throw helper method:

https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/30452cf0bbf627f12825cf50bbd31b0e526b9abe/Microsoft.Toolkit/Collections/ObservableGroupedCollectionExtensions.cs#L447-L451

## What is the new behavior?
<!-- Describe how was this issue resolved or changed? -->
The JIT is now properly handling calls to this method.

## PR Checklist

Please check if your PR fulfills the following requirements:

- [X] Tested code with current [supported SDKs](../readme.md#supported)
- [ ] ~~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->~~
- [ ] ~~Sample in sample app has been added / updated (for bug fixes / features)~~
    - [ ] ~~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)~~
- [X] Tests for the changes have been added (for bug fixes / features) (if applicable)
- [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*)
- [X] Contains **NO** breaking changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants