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

Optimize Small Dictionaries #10733

Closed

Conversation

nzdev
Copy link
Contributor

@nzdev nzdev commented Jul 25, 2021

Prerequisites

  • I have added steps to test this contribution in the description below

If there's an existing issue for this PR then this fixes

Description

Port AdaptiveCapacityDictionary from dotnet/aspnetcore#31360 which uses an array internally for capacity < 10, then uses a Hashtable once capacity >=10. This saves on memory and improves speed for small dictionaries.

Changes:

Use AdaptiveCapacityDictionary where appropriate.
Set Dictionary/AdaptiveCapacityDictionary inital capacity where known
Change from Dictionary to IDictionary where private/internal as required.

How to test?

Unit tests and benchmarks ported from dotnet/aspnetcore#31360.
Existing Umbraco tests pass.

Benchmarks:

// * Summary *

BenchmarkDotNet=v0.11.3, OS=Windows 10.0.19043
Intel Core i7-10750H CPU 2.60GHz, 1 CPU, 12 logical and 6 physical cores
[Host] : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.8.4390.0
Job-FTOBUC : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.8.4390.0

InvocationCount=1 UnrollFactor=1

                  Method |        Mean |     Error |    StdDev |        Median |        P95 |
      OneValue_SmallDict |   766.96 ns |  56.63 ns | 164.28 ns |   835.0000 ns | 1,035.0 ns |
           OneValue_Dict |   816.05 ns |  49.68 ns | 142.54 ns |   815.0000 ns | 1,115.0 ns |
  OneValue_SmallDict_Set |    61.24 ns |  22.73 ns |  65.94 ns |    80.0000 ns |   180.0 ns |
       OneValue_Dict_Set |   678.45 ns |  89.43 ns | 259.45 ns |   730.0000 ns | 1,030.0 ns |
  OneValue_SmallDict_Get |    27.70 ns |  13.18 ns |  38.85 ns |     0.0000 ns |   145.0 ns |
       OneValue_Dict_Get |   377.95 ns |  30.76 ns |  84.72 ns |   370.0000 ns |   570.0 ns |
    FourValues_SmallDict | 1,611.61 ns |  71.24 ns | 195.01 ns | 1,530.0000 ns | 2,030.0 ns |
    FiveValues_SmallDict | 2,026.70 ns |  64.99 ns | 182.24 ns | 1,930.0000 ns | 2,430.0 ns |
     SixValues_SmallDict | 2,931.00 ns | 247.50 ns | 729.76 ns | 2,640.0000 ns | 4,340.0 ns |
   SevenValues_SmallDict | 3,046.33 ns |  65.86 ns | 166.44 ns | 2,945.0000 ns | 3,345.0 ns |
   EightValues_SmallDict | 3,366.11 ns |  84.14 ns |  90.03 ns | 3,355.0000 ns | 3,555.0 ns |
    NineValues_SmallDict | 3,981.67 ns |  83.84 ns | 109.01 ns | 3,965.0000 ns | 4,165.0 ns |
     TenValues_SmallDict | 4,482.31 ns |  76.70 ns |  64.05 ns | 4,490.0000 ns | 4,590.0 ns |
         FourValues_Dict |   832.00 ns |  29.71 ns |  87.59 ns |   830.0000 ns |   935.0 ns |
         FiveValues_Dict |   865.91 ns |  24.55 ns |  67.61 ns |   900.0000 ns | 1,000.0 ns |
          SixValues_Dict |   926.18 ns |  25.98 ns |  73.69 ns |   895.0000 ns | 1,035.0 ns |
        SevenValues_Dict |   992.54 ns |  25.70 ns |  61.08 ns | 1,000.0000 ns | 1,100.0 ns |
        EightValues_Dict | 1,232.50 ns |  29.43 ns |  84.91 ns | 1,220.0000 ns | 1,345.0 ns |
         NineValues_Dict | 1,186.90 ns |  26.76 ns |  69.54 ns | 1,135.0000 ns | 1,335.0 ns |
          TenValues_Dict | 1,263.93 ns |  29.20 ns |  65.91 ns | 1,300.0000 ns | 1,400.0 ns |
 FourValues_SmallDictGet |   199.76 ns |  15.05 ns |  40.42 ns |   220.0000 ns |   220.0 ns |
 FiveValues_SmallDictGet |   201.76 ns |  16.75 ns |  45.28 ns |   230.0000 ns |   230.0 ns |

SixValues_SmallDictGetGet | 200.00 ns | 11.54 ns | 30.19 ns | 210.0000 ns | 210.0 ns |
SevenValues_SmallDictGetGet | 278.18 ns | 39.68 ns | 116.38 ns | 260.0000 ns | 470.0 ns |
EightValues_SmallDictGet | 265.00 ns | 22.68 ns | 63.98 ns | 290.0000 ns | 390.0 ns |
NineValues_SmallDictGet | 323.85 ns | 27.78 ns | 77.90 ns | 270.0000 ns | 470.0 ns |
TenValues_SmallDictGet | 300.43 ns | 33.83 ns | 96.52 ns | 260.0000 ns | 460.0 ns |
TenValues_DictGet | 439.84 ns | 21.75 ns | 61.71 ns | 485.0000 ns | 525.0 ns |
SmallDict | 1,020.00 ns | 191.76 ns | 565.40 ns | 705.0000 ns | 1,905.0 ns |
Dict | 1,308.47 ns | 199.45 ns | 588.08 ns | 976.4700 ns | 2,181.5 ns |
SmallDictFour | 1,034.00 ns | 193.78 ns | 571.37 ns | 670.0000 ns | 1,920.0 ns |
DictFour | 1,285.00 ns | 218.60 ns | 644.56 ns | 915.0000 ns | 2,320.0 ns |
SmallDictTen | 1,023.00 ns | 226.80 ns | 668.73 ns | 610.0000 ns | 2,115.0 ns |
DictTen | 1,345.00 ns | 199.83 ns | 589.19 ns | 1,010.0000 ns | 2,320.0 ns |

@umbrabot
Copy link

umbrabot commented Jul 25, 2021

Hi there @nzdev, thank you for this contribution! 👍

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

@nzdev nzdev marked this pull request as draft August 6, 2021 01:50
@nzdev
Copy link
Contributor Author

nzdev commented Aug 6, 2021

public dictionaries may need to remain dictionaries as due to casting / is operator to Dictionary instead of IDictionary fails (ModelsBuilder community version)

@nzdev nzdev marked this pull request as ready for review August 6, 2021 06:30
@nzdev nzdev marked this pull request as draft August 9, 2021 22:52
@umbrabot
Copy link

umbrabot commented Nov 4, 2021

Hiya @nzdev!

We wanted to thank you for your work and let you know that we have spent some time reviewing the contribution and have decided that we are going to close this one.

We appreciate that you have worked hard to build and document this contribution to create the changes you want to see but given the status of Umbraco 8, moving into LTS and Umbraco 9 being ready and stable and packed with new features, we are working through the open PRs and deciding which are a good fit for the last version 8 minor and which are not.

If you'd like to know a little more about this process, please check out this blog post, explaining how we came to the decisions we have made here. We'd like to reassure you though that the CMS team, along with some special guests, took the time to assess each PR that is being closed and decided how to proceed - whilst the message you're receiving is automatic, the work behind it was done by the people that make up the CMS team, along with community members.

If there are specifics around this PR that you'd like explained that are not covered by the article, please let us know.

All the best to you, our wonderful contributor.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

@umbrabot umbrabot closed this Nov 4, 2021
@nul800sebastiaan nul800sebastiaan added the status/idea The ideas in this issue are great idea, we're not ready to work on it in the near future. label Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/idea The ideas in this issue are great idea, we're not ready to work on it in the near future.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants