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

Replace JsonWebToken ReadPayloadValue with a delegate #2981

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

pmaytak
Copy link
Contributor

@pmaytak pmaytak commented Nov 5, 2024

@pmaytak pmaytak changed the title Replace JsonWebToken read overload method with delegates to read token values. Replace JsonWebToken ReadPayloadValue with a delegate Nov 6, 2024
@pmaytak
Copy link
Contributor Author

pmaytak commented Nov 7, 2024

Looks like when using delegates there're extra allocations because of:

string claimName = reader.GetString();
claims[claimName] = ReadTokenPayloadValueDelegate(ref reader, claimName);
Delegates Method Mean Ratio Error StdDev P90 P95 P100 Gen0 Allocated Alloc Ratio
Without JsonWebTokenHandler_ValidateTokenAsyncWithTVP 30.72 μs 1.000 0.026 μs 0.055 μs 30.79 μs 30.81 μs 30.89 μs 0.2441 7.23 KB 1.00
With JsonWebTokenHandler_ValidateTokenAsyncWithTVP 30.66 μs 0.998 0.124 μs 0.273 μs 30.96 μs 31.02 μs 31.10 μs 0.3052 7.55 KB 1.044
Delegates Method Mean Ratio Error StdDev P90 P95 P100 Gen0 Allocated Alloc Ratio
Without ReadJWS_FromMemory 8.373 μs 1.00 0.0331 μs 0.0727 μs 8.465 μs 8.499 μs 8.530 μs 0.2289 5.84 KB 1.00
With ReadJWS_FromMemory 8.201 μs 0.979 0.0287 μs 0.0636 μs 8.295 μs 8.313 μs 8.357 μs 0.2441 6.07 KB 1.039

@pmaytak pmaytak requested a review from Copilot January 8, 2025 08:35

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 9 changed files in this pull request and generated no comments.

Files not reviewed (4)
  • src/Microsoft.IdentityModel.JsonWebTokens/InternalAPI.Unshipped.txt: Language not supported
  • src/Microsoft.IdentityModel.Tokens/InternalAPI.Unshipped.txt: Language not supported
  • test/Microsoft.IdentityModel.JsonWebTokens.Tests/CustomJsonWebToken.cs: Evaluated as low risk
  • src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonWebToken.PayloadClaimSet.cs: Evaluated as low risk
Comments suppressed due to low confidence (1)

src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonClaimSet.cs:31

  • The initialization of _jsonClaims should be 'new Dictionary<string, object>()' instead of '[]'.
_jsonClaims = [];
@pmaytak
Copy link
Contributor Author

pmaytak commented Jan 15, 2025

Updated results comparing delegates which have a dictionary as a parameter. Ran JsonWebTokenHandler_ValidateTokenAsyncWithTVP and ReadJWS_FromMemory with extended claims. These test using the default delegate implemenation.

Delegates Method Mean Ratio Error StdDev P90 P95 P100 Gen0 Allocated Alloc Ratio
Without JsonWebTokenHandler_ValidateTokenAsyncWithTVP 30.13 μs 1.00 0.169 μs 0.374 μs 30.61 μs 30.74 μs 31.07 μs 0.2441 7.23 KB 1.00
With JsonWebTokenHandler_ValidateTokenAsyncWithTVP 29.34 μs 0.974 0.085 μs 0.188 μs 29.59 μs 29.61 μs 29.87 μs 0.2441 7.33 KB 1.014
Delegates Method Mean Ratio Error StdDev P90 P95 P100 Gen0 Allocated Alloc Ratio
Without ReadJWS_FromMemory 7.523 μs 1.00 0.0333 μs 0.0724 μs 7.599 μs 7.612 μs 7.672 μs 0.2365 5.84 KB 1.00
With ReadJWS_FromMemory 7.059 μs 0.938 0.0416 μs 0.0905 μs 7.160 μs 7.170 μs 7.205 μs 0.2365 5.84 KB 1.00

BenchmarkDotNet v0.13.12, Windows 11 (10.0.26100.2605) (Hyper-V)
Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 16 logical and 8 physical cores
.NET SDK 9.0.101
[Host] : .NET 8.0.11 (8.0.1124.51707), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
MediumRun : .NET 8.0.11 (8.0.1124.51707), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI

@pmaytak pmaytak reopened this Jan 24, 2025
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.

1 participant