Improve performance of SourceText.IsBinary on .NET Core#61148
Merged
jaredpar merged 1 commit intodotnet:mainfrom May 5, 2022
Merged
Improve performance of SourceText.IsBinary on .NET Core#61148jaredpar merged 1 commit intodotnet:mainfrom
jaredpar merged 1 commit intodotnet:mainfrom
Conversation
Contributor
|
That's awesome! |
CyrusNajmabadi
approved these changes
May 5, 2022
Member
|
Can you share out the benchmark you wrote here? Mostly for my own education. |
jaredpar
approved these changes
May 5, 2022
cston
approved these changes
May 5, 2022
Contributor
Author
|
This is the second benchmark: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;netcoreapp3.1;net472</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'net472'">
<PackageReference Include="System.Collections" Version="4.3.0" />
<PackageReference Include="System.IO.FileSystem.Primitives" Version="4.3.0" />
<PackageReference Include="System.Runtime.Extensions" Version="4.3.0" />
<PackageReference Include="System.Runtime.Handles" Version="4.3.0" />
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Compilers\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.csproj" />
</ItemGroup>
</Project>using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using Microsoft.CodeAnalysis.Text;
BenchmarkRunner.Run<SourceTextBenchmark>();
[SimpleJob(RuntimeMoniker.Net472)]
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
[SimpleJob(RuntimeMoniker.Net60)]
public class SourceTextBenchmark
{
private readonly string _largerTextPath =
@"C:\Users\{username}\source\repos\dotnet\roslyn\src\Compilers\CSharp\Portable\Generated\BoundNodes.xml.Generated.cs"; // 902 KB
private FileStream? _fileStream;
[IterationSetup]
public void Setup()
{
_fileStream = File.OpenRead(_largerTextPath);
}
[IterationCleanup]
public void Cleanup()
{
_fileStream!.Dispose();
_fileStream = null;
}
[Benchmark]
public SourceText SourceTextFrom()
{
return SourceText.From(_fileStream!, throwIfBinaryDetected: true);
}
}I don't have the source for the first benchmark anymore. First, I coped the |
Member
|
@Neme12 thanks for sharing! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The code is self explanatory 🙃
Tiny benchmark testing just the IsBinary method on a string from a 74 kB source file on .NET 6, where
IsBinaryis the old implementation andIsBinary2is the new one:Benchmark testing
SourceText.From(fileStream, throwIfBinaryDetected: true)for BoundNodes.xml.Generated.cs,before the change:
after the change:
EDIT: It seems the difference is exaggerated in the SourceTextFrom benchmarks, I guess because it's completely different runs. The actual difference shouldn't be more than 500ms because that seems to be the difference between using
throwIfBinaryDetectedtrue vs false with the original code.