Repository navigation
DEMO: Compare manual and DTO-based UOPD JSON serialization - #153
Closed
Brennan1994 wants to merge 1 commit into
Closed
Brennan1994 wants to merge 1 commit into
Brennan1994 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
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 manualToJson()/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 attributedUncertainOrderedPairedDataConverteravoids that limitation by serializing a plainCollectionDataDTO containing the seven ordering/validation settings and anOrdinateData[]. Each ordinate DTO contains X and Y and selects the existingUnivariateDistributionConverterfor 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):
ToJsonandFromJsonuseUtf8JsonWriterandUtf8JsonReaderdirectly for the collection and ordinates. They do not depend on DTOs or serialization attributes; they invoke the same existing distribution converter explicitly for Y.FromJsonappends 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.
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
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
Utf8JsonWriterover aMemoryStream, 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
Benchmarks_Numerics/Serialization/UncertainPairedDataManualJsonBenchmarks.cs; its opening comment records the results and conditions.Reproduce with:
Validation
dotnet build -c Release: passed for net10.0 and net481 with zero errors and warnings.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.