Skip to content

DEMO: Compare manual and DTO-based UOPD JSON serialization - #153

Closed
Brennan1994 wants to merge 1 commit into
InvestigateExistingJsonfrom
ManualUncertainPairedDataJson
Closed

Brennan1994 wants to merge 1 commit into
InvestigateExistingJsonfrom
ManualUncertainPairedDataJson

Conversation

@Brennan1994

Copy link
Copy Markdown
Collaborator

Summary

DEMO: Compare two JSON serialization strategies for UncertainOrderedPairedData (UOPD), keeping both available in the same assembly for benchmarking. This PR builds on #152 and adds manual ToJson() / FromJson(string) methods, equivalence and invalid-input tests, and a reproducible BenchmarkDotNet comparison.

Strategies and code complexity

Converter and DTO approach (from #152): UOPD implements IList<UncertainOrdinate>, so default serialization treats it as an array and omits the collection's settings. An attributed UncertainOrderedPairedDataConverter avoids that limitation by serializing a plain CollectionData DTO containing the seven ordering/validation settings and an OrdinateData[]. Each ordinate DTO contains X and Y and selects the existing UnivariateDistributionConverter for Y through an attribute. Attributes enforce required fields, preserve default-valued settings, and handle named floating-point values. This keeps the collection converter comparatively short and delegates most format handling to System.Text.Json.

Manual approach (added here): ToJson and FromJson use Utf8JsonWriter and Utf8JsonReader directly for the collection and ordinates. They do not depend on DTOs or serialization attributes; they invoke the same existing distribution converter explicitly for Y. FromJson appends directly to the final collection and validates after all settings and ordinates have been read. It avoids the intermediate DTO collection, but requires more handwritten code for required-property tracking, token types, property order, unknown fields, enum values, and non-finite doubles.

Both paths preserve the same default JSON format and recompute validity rather than storing IsValid. The manual methods target default serializer conventions; they do not expose the converter path's configurable serializer options. Null Y distributions are rejected when reading. Existing numerical validation rules remain unchanged. The new public methods have documented pynumerics coverage exclusions; Python persistence remains available through the existing XML wrapper.

Deserialization results

Manual deserialization took 22-31% less time and allocated 20-26% fewer managed bytes across these fixtures.

Ordinates Converter + DTOs Manual Time saved Time reduction
10 10.068 us 6.972 us 3.096 us 30.7%
50 43.960 us 33.793 us 10.167 us 23.1%
100 87.653 us 68.255 us 19.398 us 22.1%
1,000 861.161 us 665.937 us 195.224 us 22.7%
Ordinates Converter bytes Manual bytes Bytes saved Allocation reduction
10 4,560 3,384 1,176 25.8%
50 19,264 15,256 4,008 20.8%
100 37,736 30,256 7,480 19.8%
1,000 361,680 289,336 72,344 20.0%

Both implementations scale approximately linearly over this range. Increasing the ordinate count from 50 to 1,000 (20x) increased execution time 19.6x for the converter and 19.7x for manual; allocations increased 18.8x and 19.0x, respectively. Manual parsing lowers the cost per ordinate rather than changing the apparent complexity class. At 1,000 ordinates, converter/manual costs were approximately 0.861/0.666 us and 362/289 allocated bytes per ordinate.

Serialization results and tradeoff

Ordinates Converter time Manual time Converter bytes Manual bytes
10 3.896 us 3.766 us 2,488 8,544
50 17.232 us 17.199 us 9,688 20,016
100 36.000 us 34.068 us 18,688 34,832
1,000 419.043 us 372.252 us 184,319 272,023

The writers had similar timings at 50 ordinates; at 1,000, manual writing took 11.2% less time but allocated 47.6% more memory. The current manual writer creates a fresh Utf8JsonWriter over a MemoryStream, with separate writer and stream buffers, then creates the returned string. System.Text.Json's string serializer uses cached writers and pooled buffers. This buffering difference is a code-based explanation of the allocation tradeoff, not an allocation-profiler breakdown. No writer-buffer optimization is included in this demo.

Measurement conditions

  • BenchmarkDotNet 0.15.8; .NET 10.0.9 x64; Windows 11; AMD EPYC 9654 with eight logical cores exposed; Release build.
  • One launch, five warmup iterations, fifteen measured iterations per case, with BenchmarkDotNet's outlier filtering. All sixteen benchmark cases completed.
  • Valid ascending curves with Normal distributions and default tail validation. Both readers receive the same prebuilt JSON string. Reconstruction and full validation are included; fixture creation and equivalence checks are excluded.
  • Times are means per complete collection. Allocation is managed bytes allocated per call, not peak or retained memory. These are warmed results for this fixture, not cold-start measurements or predictions for every distribution type.
  • The benchmark source is Benchmarks_Numerics/Serialization/UncertainPairedDataManualJsonBenchmarks.cs; its opening comment records the results and conditions.

Reproduce with:

dotnet run -c Release --project Benchmarks_Numerics -- --filter '*UncertainPairedDataManualJsonBenchmarks*'

Validation

  • dotnet build -c Release: passed for net10.0 and net481 with zero errors and warnings.
  • Focused UOPD serialization tests: 56 passed on each target framework, including output equivalence, cross-reading, ordering, mixed distributions, tail validation, malformed inputs, missing fields, and null distributions.
  • dotnet test -c Release --no-build --filter "TestCategory!=LiveServiceIntegration": 4,131 tests passed on net10.0 and 4,114 on net481; zero failures or skipped tests.
  • Python tests were not run at the user's request.

@Brennan1994 Brennan1994 closed this Dec 4, 2026
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