feat: add span based Verifier #364
Draft
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.
Summary
Introduces a high-performance span-based verification API (
VerifierSpan) for .NET 8+ that significantly reduces memory allocations while maintaining full compatibility with the existingVerifierAPI.Motivation
The existing builder-based
VerifierAPI creates multiple intermediate allocations during signature verification, particularly when processing request bodies and constructing the JWS signing string. For high-throughput scenarios, these allocations create GC pressure and can impact performance.Changes
New Files
src/VerifierSpan.cs: High-performance span-based verification APIVerifierSpan.VerifyWithPem()- Verify with PEM-encoded public keyVerifierSpan.VerifyWith()- Verify with pre-parsed ECDsa key (most optimized)stackallocfor typical payloadsECDsa.VerifyData()calls bypassing Jose.JWT overheadtest/VerifierSpanTest.cs: Comprehensive test suite (38 tests)Modified Files
src/Util.cs: Added span-based utility methods for .NET 8+CalculateV2SigningPayloadSize()- Pre-calculate exact buffer sizesBuildV2SigningPayloadInto()- Zero-copy payload building into spanssrc/Verifier.cs: Fixed file corruption (typos on lines 62, 64)benchmarks/VerifierBenchmarks.cs: Updated to compare both APIsPerformance Improvements
Benchmarks show significant memory reduction across all payload sizes:
Key Improvements:
Implementation Details
Span-Based Optimizations
stackallocfor buffers <4KB (typical requests)System.Buffers.Text.Base64Urlwhen availableArchitecture
Key Design Decisions
Verifierbuilder pattern#if NET8_0_OR_GREATER)ReadOnlySpan<byte>for body to avoid allocationsTesting
All tests pass across all target frameworks.
Breaking Changes
None - This is a purely additive change. The existing
VerifierAPI remains unchanged and fully supported.Migration Guide
For high-throughput scenarios where performance matters:
Before:
After (NET8+):
Future Work
Potential areas for further optimization: