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

perf: Optimize read MIME operation #9132

Merged
merged 2 commits into from
Aug 25, 2023
Merged

Conversation

filzrev
Copy link
Contributor

@filzrev filzrev commented Aug 24, 2023

What included in this PR

  • Set FileStream's BufferSize to 0 (Default:4096)
  • Set StreamReader's BufferSize to 128 (Default:1024, Min:128)
  • Set FileOptions.SequentialScan hint for Windows (It seems don't affects actual performance thought)

Background
YamlMime.ReadMime API read file's first line only.
So it can reduce internal buffer allocations. and reduce Gen0 GC allocations.

Benchmark results

Method Mean Error StdDev Ratio Gen0 Gen1 Allocated Alloc Ratio
ReadMime 19.23 μs 0.192 μs 0.180 μs 1.00 1.8616 0.0305 7.73 KB 1.00
ReadMime_Optimized 18.16 μs 0.083 μs 0.065 μs 0.94 0.2441 0.0305 1.02 KB 0.13

@codecov
Copy link

codecov bot commented Aug 24, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.01% 🎉

Comparison is base (277aa86) 77.58% compared to head (06e6704) 77.59%.
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9132      +/-   ##
==========================================
+ Coverage   77.58%   77.59%   +0.01%     
==========================================
  Files         591      591              
  Lines       24567    24578      +11     
==========================================
+ Hits        19060    19071      +11     
  Misses       5507     5507              
Files Changed Coverage Δ
src/Docfx.Common/YamlMime.cs 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -12,10 +15,8 @@ public static class YamlMime
public const string TableOfContent = YamlMimePrefix + nameof(TableOfContent);
public const string XRefMap = YamlMimePrefix + nameof(XRefMap);

public static string ReadMime(TextReader reader)
public static string? ReadMime([NotNull] TextReader reader)

Choose a reason for hiding this comment

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

IIUC, [NotNull] on the parameter means that, if reader were null, then ReadMime would throw rather than return. So that if another method calls ReadMime and passes a possibly-null value, then the compiler can assume the value is not null after ReadMime returns.

Is that the purpose for which you use [NotNull] here?

Copy link
Contributor Author

@filzrev filzrev Aug 24, 2023

Choose a reason for hiding this comment

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

As you pointed out. [NotNull] is not works as expected.
In Anyway it's public API. parameter's null checks are required.
So I've remove [NotNull] attribute and revert original null check codes.

@yufeih yufeih added the performance Makes the pull request appear in "Performance" section of the next release note label Aug 25, 2023
Copy link
Contributor

@yufeih yufeih left a comment

Choose a reason for hiding this comment

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

Thank you @filzrev !

@yufeih yufeih merged commit c921f82 into dotnet:main Aug 25, 2023
7 checks passed
p-kostov pushed a commit to ErpNetDocs/docfx that referenced this pull request Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Makes the pull request appear in "Performance" section of the next release note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants