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 JToken to Dictionary conversion performance #9096

Merged
merged 3 commits into from
Aug 19, 2023

Conversation

filzrev
Copy link
Contributor

@filzrev filzrev commented Aug 17, 2023

What's included in this PR

  • Optimize ConvertToObjectHelper.ConvertJObjectToObject method performance.
  • Refactor code and append AggressiveOptimization attribute.
  • Remove unused test code.

Background
When running Visual Studio profiler.
Sometimes ConvertJObjectToObject method is reported as Hot Path.
And this method is called about 64702 times when running docfx to samples/seed projecft.

It seems jObject.ToObject<Dictionary<string, object>>() call can be safely removed.
Because JObject already implemented IDictionary<string,JToken> interface implicitly.

Rewrite code from

jObject.ToObject<Dictionary<string, object>>().ToDictionary(p => p.Key, p => ConvertJObjectToObject(p.Value));

To

((IDictionary<string,JToken>jObject).ToDictionary(p => p.Key, p => ConvertJObjectToObject(p.Value));

@codecov
Copy link

codecov bot commented Aug 17, 2023

Codecov Report

Patch coverage: 93.75% and project coverage change: -0.01% ⚠️

Comparison is base (f698eac) 77.57% compared to head (40cbffc) 77.57%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9096      +/-   ##
==========================================
- Coverage   77.57%   77.57%   -0.01%     
==========================================
  Files         592      592              
  Lines       24608    24616       +8     
==========================================
+ Hits        19090    19096       +6     
- Misses       5518     5520       +2     
Files Changed Coverage Δ
src/Docfx.Common/ConvertToObjectHelper.cs 85.96% <93.75%> (+0.25%) ⬆️

... and 1 file with indirect coverage changes

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

case JTokenType.Object:
return ConvertJObjectToDictionary((JObject)jToken);
default:
if (jToken is JValue jValue)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: why not also cast here?

Suggested change
if (jToken is JValue jValue)
return ((JValue)jToken).Value;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nit: why not also cast here?
There is some types that derived from JToken but not JValue. (e.g. JContainer, JProperty)
(Thought these types should not be passed to this function)

@yufeih yufeih merged commit e7a2777 into dotnet:main Aug 19, 2023
@yufeih yufeih added the new-feature Makes the pull request to appear in "New Features" section of the next release note label Aug 21, 2023
@filzrev filzrev deleted the perf-optimize-dictionary-conversion branch August 24, 2023 02:46
p-kostov pushed a commit to ErpNetDocs/docfx that referenced this pull request Jun 28, 2024
* perf: optimize jToken conversion performance

* fix: fix problems that may cause infinite loop

* fix: add unit tests for code coverage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-feature Makes the pull request to appear in "New Features" section of the next release note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants