Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

eiriktsarpalis
Copy link
Member

Rewrites the shared JsonSerializerOptions.CachingContext implementation so that matching instances are looked up using lock-free linear search over an array of WeakReference<CachingContext> instances. This achieves a couple of things:

  1. Fixes Consider clearing JsonSerializerOptions.Caching cache using a timer, not just based on incoming calls #76548, in which user-defined converters referencing a large amount memory could end up being rooted in the global cache in scenaria where serialization operations are not performed frequently.
  2. Improves performance compared to the existing ConcurrentDictionary approach:
Method Job Branch Mean Error StdDev Median Min Max Ratio MannWhitney(3%) RatioSD Gen 0 Gen 1 Gen 2 Allocated Alloc Ratio
NewDefaultOptions Job-CLWPWY main 998.3 ns 47.26 ns 54.43 ns 1,010.4 ns 885.2 ns 1,087.4 ns 1.00 Base 0.00 0.0408 0.0041 0.0041 461 B 1.00
NewDefaultOptions Job-AKKANL PR 901.2 ns 46.17 ns 53.17 ns 894.7 ns 832.5 ns 1,001.2 ns 0.91 Faster 0.08 0.0398 0.0040 0.0040 461 B 1.00
NewCustomizedOptions Job-CLWPWY main 1,079.3 ns 52.27 ns 55.93 ns 1,077.0 ns 990.1 ns 1,211.8 ns 1.00 Base 0.00 0.0435 0.0040 0.0040 485 B 1.00
NewCustomizedOptions Job-AKKANL PR 1,027.9 ns 45.85 ns 52.80 ns 1,032.2 ns 938.0 ns 1,102.2 ns 0.95 Same 0.07 0.0427 0.0039 0.0039 487 B 1.00
NewCustomConverter Job-CLWPWY main 21,462.8 ns 1,790.02 ns 1,915.31 ns 20,926.3 ns 18,933.0 ns 25,316.8 ns 1.00 Base 0.00 0.7659 0.0766 - 8027 B 1.00
NewCustomConverter Job-AKKANL PR 20,540.2 ns 848.39 ns 977.01 ns 20,651.8 ns 19,212.5 ns 22,677.2 ns 0.95 Same 0.09 0.7716 0.0772 - 8005 B 1.00
NewCachedCustomConverter Job-CLWPWY main 1,228.5 ns 89.28 ns 99.23 ns 1,204.1 ns 1,067.9 ns 1,422.1 ns 1.00 Base 0.00 0.0502 0.0046 0.0046 545 B 1.00
NewCachedCustomConverter Job-AKKANL PR 1,089.1 ns 43.82 ns 50.47 ns 1,105.0 ns 982.9 ns 1,159.6 ns 0.89 Faster 0.08 0.0509 0.0039 0.0039 545 B 1.00

@ghost ghost added the area-System.Text.Json label Oct 4, 2022
@ghost ghost assigned eiriktsarpalis Oct 4, 2022
@ghost
Copy link

ghost commented Oct 4, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Rewrites the shared JsonSerializerOptions.CachingContext implementation so that matching instances are looked up using lock-free linear search over an array of WeakReference<CachingContext> instances. This achieves a couple of things:

  1. Fixes Consider clearing JsonSerializerOptions.Caching cache using a timer, not just based on incoming calls #76548, in which user-defined converters referencing a large amount memory could end up being rooted in the global cache in scenaria where serialization operations are not performed frequently.
  2. Improves performance compared to the existing ConcurrentDictionary approach:
Method Job Branch Mean Error StdDev Median Min Max Ratio MannWhitney(3%) RatioSD Gen 0 Gen 1 Gen 2 Allocated Alloc Ratio
NewDefaultOptions Job-CLWPWY main 998.3 ns 47.26 ns 54.43 ns 1,010.4 ns 885.2 ns 1,087.4 ns 1.00 Base 0.00 0.0408 0.0041 0.0041 461 B 1.00
NewDefaultOptions Job-AKKANL PR 901.2 ns 46.17 ns 53.17 ns 894.7 ns 832.5 ns 1,001.2 ns 0.91 Faster 0.08 0.0398 0.0040 0.0040 461 B 1.00
NewCustomizedOptions Job-CLWPWY main 1,079.3 ns 52.27 ns 55.93 ns 1,077.0 ns 990.1 ns 1,211.8 ns 1.00 Base 0.00 0.0435 0.0040 0.0040 485 B 1.00
NewCustomizedOptions Job-AKKANL PR 1,027.9 ns 45.85 ns 52.80 ns 1,032.2 ns 938.0 ns 1,102.2 ns 0.95 Same 0.07 0.0427 0.0039 0.0039 487 B 1.00
NewCustomConverter Job-CLWPWY main 21,462.8 ns 1,790.02 ns 1,915.31 ns 20,926.3 ns 18,933.0 ns 25,316.8 ns 1.00 Base 0.00 0.7659 0.0766 - 8027 B 1.00
NewCustomConverter Job-AKKANL PR 20,540.2 ns 848.39 ns 977.01 ns 20,651.8 ns 19,212.5 ns 22,677.2 ns 0.95 Same 0.09 0.7716 0.0772 - 8005 B 1.00
NewCachedCustomConverter Job-CLWPWY main 1,228.5 ns 89.28 ns 99.23 ns 1,204.1 ns 1,067.9 ns 1,422.1 ns 1.00 Base 0.00 0.0502 0.0046 0.0046 545 B 1.00
NewCachedCustomConverter Job-AKKANL PR 1,089.1 ns 43.82 ns 50.47 ns 1,105.0 ns 982.9 ns 1,159.6 ns 0.89 Faster 0.08 0.0509 0.0039 0.0039 545 B 1.00
Author: eiriktsarpalis
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@eiriktsarpalis eiriktsarpalis added this to the 8.0.0 milestone Oct 4, 2022
@eiriktsarpalis
Copy link
Member Author

FYI @SteveSandersonMS

@eiriktsarpalis eiriktsarpalis added the tenet-performance Performance related issue label Oct 4, 2022
@eiriktsarpalis
Copy link
Member Author

I've simplified the cache implementation so that evictions are no longer performed. Instead, every lookup will perform a linear search across all 64 entries until a matching one is found. Updated benchmarks:

Method Job Branch Mean Error StdDev Median Min Max Ratio MannWhitney(3%) RatioSD Gen 0 Gen 1 Gen 2 Allocated Alloc Ratio
NewDefaultOptions Job-FFWUTS main 1,013.0 ns 43.04 ns 49.57 ns 1,007.6 ns 920.9 ns 1,102.3 ns 1.00 Base 0.00 0.0405 0.0040 0.0040 461 B 1.00
NewDefaultOptions Job-ZLXSEW PR 960.6 ns 41.54 ns 47.83 ns 944.0 ns 896.3 ns 1,092.7 ns 0.95 Same 0.07 0.0453 0.0038 0.0038 459 B 1.00
NewCustomizedOptions Job-FFWUTS main 1,141.8 ns 51.71 ns 59.55 ns 1,119.8 ns 1,068.5 ns 1,279.2 ns 1.00 Base 0.00 0.0432 0.0043 0.0043 484 B 1.00
NewCustomizedOptions Job-ZLXSEW PR 1,056.7 ns 53.45 ns 61.55 ns 1,051.2 ns 938.2 ns 1,184.9 ns 0.93 Faster 0.06 0.0424 0.0039 0.0039 486 B 1.00
NewCustomConverter Job-FFWUTS main 20,046.4 ns 439.57 ns 506.20 ns 20,003.1 ns 19,403.1 ns 20,968.4 ns 1.00 Base 0.00 0.7512 0.0751 - 8026 B 1.00
NewCustomConverter Job-ZLXSEW PR 20,828.4 ns 843.85 ns 971.78 ns 20,691.9 ns 19,709.1 ns 23,267.3 ns 1.04 Same 0.06 0.7166 0.0796 - 8004 B 1.00
NewCachedCustomConverter Job-FFWUTS main 1,115.7 ns 39.86 ns 45.90 ns 1,124.0 ns 1,028.3 ns 1,177.7 ns 1.00 Base 0.00 0.0504 0.0046 0.0046 545 B 1.00
NewCachedCustomConverter Job-ZLXSEW PR 1,075.1 ns 34.65 ns 39.90 ns 1,071.3 ns 1,010.3 ns 1,143.5 ns 0.97 Same 0.05 0.0501 0.0046 0.0046 545 B 1.00

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

LGTM

@eiriktsarpalis
Copy link
Member Author

/backport to release/7.0

@github-actions github-actions bot unlocked this conversation Jan 12, 2023
@github-actions
Copy link
Contributor

Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/3905535681

@github-actions
Copy link
Contributor

@eiriktsarpalis backporting to release/7.0 failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Rewrite the shared JsonSerializerOptions cache implementation.
Using index info to reconstruct a base tree...
M	src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs
Applying: address feedback
Applying: Simplify cache implementation& address feedback.
error: sha1 information is lacking or useless (src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0003 Simplify cache implementation& address feedback.
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@github-actions
Copy link
Contributor

@eiriktsarpalis an error occurred while backporting to release/7.0, please check the run log for details!

Error: git am failed, most likely due to a merge conflict.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider clearing JsonSerializerOptions.Caching cache using a timer, not just based on incoming calls
3 participants