Purview.ZodSharp is designed for maximum performance: validation rules are readonly record structs, hot paths use Span<T>, and the source generator emits direct typed codegen with no reflection. The committed BenchmarkDotNet suite measures every scenario.
Results are written to BenchmarkDotNet.Artifacts/ (HTML, Markdown, logs) in the project directory. Use -c Release; the suite uses [MemoryDiagnoser] and a [SimpleJob] profile.
BenchmarkDotNet 0.15.8, .NET 10.0.12, Windows 11 (10.0.28020.2991).
These figures are stale in one respect: the repository now pins BenchmarkDotNet
0.16.0-preview.2 (Directory.Packages.props), so the numbers below were produced by a different version
than the suite currently builds against. They are also from one machine. Re-run the suite before relying
on absolute values; use them for relative comparison between scenarios.
UUID validation uses a zero-allocation char-scan (version nibble at position 14, variant nibble at position 19) instead of a regex. Measured against the previous compiled regex:
Scenario
Mean
Allocated
Rule_CharScan_Valid (.UUID())
21.99 ns
0 B
Rule_LegacyRegex_Valid (previous implementation)
27.50 ns
0 B
Rule_CharScan_Invalid
< 1 ns
0 B
Rule_LegacyRegex_Invalid
14.22 ns
0 B
Rule_CharScan_Nil
20.67 ns
0 B
Rule_CharScanV7_Valid (.UUID(UuidVersion.V7))
20.89 ns
0 B
Rule_CharScanV7_Mismatch
20.11 ns
0 B
Schema_UUID_Valid
29.80 ns
0 B
Schema_UUIDV7_Valid
26.64 ns
0 B
The char-scan is ~20% faster than the previous regex on the valid path, is version-aware at no extra cost, and rejects wrong-length strings in under a nanosecond.
Struct-based rules — every rule is a readonly record struct implementing IValidationRule<T>, so there is no per-validation object allocation.
Span-aware helpers — Span<T>/ReadOnlySpan<T> APIs and ArrayPool<T>-backed helpers. The shipped string rules implement IStringValidationRule (except the URL and Base64 rules, which need a string), so ZodString.ValidateSpan validates the span directly and only materialises the value string for its result; ZodString.IsValidSpan avoids that allocation entirely.
Source generation — [ZodSchema] emits direct property access and typed equality checks with no reflection (see Source Generator).
Cacheable schema instances — a fully built schema is safe to cache and share, so SchemaCache avoids repeated construction (see Compiled Validators and Caching). Building is not immutable: the fluent rule methods mutate the receiver. See Custom Rules for extending rules.
The allocations on a successful validation are limited to the string transforms (ToLower/ToUpper/Trim), ZodString.ValidateSpan (its result carries a string), and the union non-first-option paths.