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

Use ReadOnlyMemory when calculating the related file extensions #5943

Merged
merged 7 commits into from
Aug 1, 2024

Conversation

nkolev92
Copy link
Member

@nkolev92 nkolev92 commented Jul 31, 2024

Bug

Related: NuGet/Home#12728

Description

This gets us about 4MB on OrchardCore.
We just use span instead of optimistically using substring.
Note that this calculation is cached on a per package level already.

This probably isn't perfect, but it's a start.

Before:

Name                                                                                                                                    	Inc %	          Inc
 nuget.packaging!ContentItemCollection.GetRelatedFileExtensionProperty                                                                  	  0.1	   11,851,736
+ mscorlib.ni!System.String.Substring(Int32, Int32)                                                                                     	  0.1	    7,679,344
+ nuget.packaging!NuGet.NoAllocEnumerateExtensions+OptimisticallyNonAllocatingEnumerable`1[System.__Canon].GetEnumerator()              	  0.0	    1,803,320
+ nuget.packaging!NuGet.NoAllocEnumerateExtensions+OptimisticallyNonAllocatingEnumerable`1+Enumerator[System.__Canon].MoveNext()        	  0.0	    1,100,856
+ mscorlib.ni!System.Collections.Concurrent.ConcurrentDictionary`2[System.__Canon,System.__Canon].TryAdd(System.__Canon, System.__Canon)	  0.0	    1,052,960
+ mscorlib.ni!String.Join                                                                                                               	  0.0	      108,760
+ clr!JIT_New                                                                                                                           	  0.0	      106,496

After:

Name                                                                                                                            	Inc %	         Inc
 nuget.packaging!ContentItemCollection.GetRelatedFileExtensionProperty                                                          	  0.1	   8,327,688
+ mscorlib!System.Collections.Generic.Dictionary`2[System.ReadOnlyMemory`1[System.Char],System.__Canon].set_Item(!0,!1)         	  0.0	   3,842,840
+ mscorlib!System.Collections.Generic.Dictionary`2[System.ReadOnlyMemory`1[System.Char],System.__Canon].TryGetValue(!0,!1&)     	  0.0	   1,668,760
+ nuget.packaging!NuGet.NoAllocEnumerateExtensions+OptimisticallyNonAllocatingEnumerable`1+Enumerator[System.__Canon].MoveNext()	  0.0	   1,243,928
+ nuget.packaging!NuGet.NoAllocEnumerateExtensions+OptimisticallyNonAllocatingEnumerable`1[System.__Canon].GetEnumerator()      	  0.0	   1,106,464
+ mscorlib.ni!String.Join                                                                                                       	  0.0	     345,640
+ clr!JIT_New                                                                                                                   	  0.0	     120,056

In microbenchmarks, it registers about the same, but we can also notice that it's faster.

Before:

| Method                | Mean          | Error      | StdDev     | Gen0       | Gen1      | Allocated    |
|---------------------- |--------------:|-----------:|-----------:|-----------:|----------:|-------------:|
| NJ                    |      44.10 us |   0.164 us |   0.153 us |     4.3335 |    0.1221 |     73.78 KB |
| MicrosoftBuildRuntime |      66.96 us |   0.285 us |   0.267 us |     8.9111 |    0.7324 |     151.6 KB |
| AllPackages           | 155,316.21 us | 563.189 us | 526.807 us | 15250.0000 | 1750.0000 | 259587.62 KB |

After:

| Method                | Mean          | Error      | StdDev     | Gen0       | Gen1      | Allocated    |
|---------------------- |--------------:|-----------:|-----------:|-----------:|----------:|-------------:|
| NJ                    |      44.74 us |   0.120 us |   0.112 us |     4.2114 |    0.1221 |     71.77 KB |
| MicrosoftBuildRuntime |      66.51 us |   0.223 us |   0.209 us |     8.9111 |    0.7324 |     151.6 KB |
| AllPackages           | 151,262.23 us | 354.896 us | 331.970 us | 15000.0000 | 2000.0000 | 254570.14 KB |

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc.

@nkolev92 nkolev92 changed the title Fun with benchmarks Use ReadOnlyMemory when calculating the related file extensions Jul 31, 2024
@nkolev92 nkolev92 marked this pull request as ready for review July 31, 2024 21:01
@nkolev92 nkolev92 requested a review from a team as a code owner July 31, 2024 21:01
@nkolev92 nkolev92 merged commit 3274cb8 into dev Aug 1, 2024
28 checks passed
@nkolev92 nkolev92 deleted the dev-nkolev92-TryMatchPlay branch August 1, 2024 00:51
return relatedFileExtensionsProperty;
}

static bool ReadOnlyMemoryEquals(ReadOnlyMemory<char> x, ReadOnlyMemory<char> y)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it worth making this an extension method that the whole repo can use?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I had merged before this.
I think we could eventually.
You were thinking a build/shared type of method?

@@ -50,6 +52,7 @@ public void Load(IEnumerable<string> paths)
}
}

[Obsolete("Unused and will be removed in a future version.")]
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems to be used in the dotnet/sdk: dotnet/sdk#42458 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

2 options for you.

  • Supress the warning
  • Move to PopulateItemGroups

I'll revert the obsolete attribute for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks. I think moving to PopulateItemGroups is the right approach going forward.

private List<Asset> _assets;
private ConcurrentDictionary<string, string> _assemblyRelatedExtensions;
private Dictionary<ReadOnlyMemory<char>, string> _assemblyRelatedExtensions;
Copy link
Contributor

Choose a reason for hiding this comment

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

@nkolev92 the SDK PR is now failing with this error in multiple tests, I wonder if it is related to this change:

C:\h\w\AADF0A48\p\d\sdk\9.0.100-ci\NuGet.targets(180,5): error : Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.

Copy link
Member Author

Choose a reason for hiding this comment

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

Weird, this codepath isn't run concurrently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants