Repository navigation
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-formats-cbor, @bartonjs, @vcsjones |
This branch has not been deployed
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.
Contributes to #137111 (change 3). Follows #137943.
Description
Every map, array and indefinite-length string pushes a
StackFrameon open and pops it on close. Besides the context the writer needs in every mode, the frame carries three fields used only by the duplicate and sorted key checks:KeysRequireSorting,KeyValuePairEncodingRangesandKeyEncodingRanges. Lax mode never sets them, since #137772 skips the key bookkeeping there. It still copies them into and out of every frame, and resets the three live fields on every push.This PR moves those three fields into a separate
KeyFrame, kept on its ownStack<KeyFrame>. It is pushed and popped only when_tracksMapKeysis set, which is every mode except Lax. In Lax, the second stack is never created and the three fields are never copied or reset.StackFramekeeps the other six fields and no longer holds any references.Stack<StackFrame>is unchanged otherwise. There are no public API changes.The issue described this change with "a separate array". That wording assumed change 1 (an array-based frame stack) went first. Change 1 turned out not to help on top of #137943, so I dropped it, and this PR uses a
Stack<KeyFrame>to match the existing frame stack.Why the behavior is the same
false/null. They are only set byHandleMapKeyWritten,HandleMapValueWrittenandCompleteMapWrite(directly or throughGetKeyEncodingRanges/GetKeyValueEncodingRanges), and all three run only when_tracksMapKeysis set. Saving, resetting and restoring values that never change has no effect, so skipping it changes nothing.PushDataItemandPopDataItempush and pop both stacks together, andReset()clears both, so the two stay in step.Benchmarks
BenchmarkDotNet 0.15.2, .NET 10.0.12, Arm64 RyuJIT, Apple M1 Pro, macOS 26.7.3.
mainand this branch were built from source in the same benchmark project (namespace changed, nothing else) and run as separate processes in 5 interleaved rounds, alternating which went first. Each column is the best mean of the 5 rounds. The writer isCborConformanceMode.Lax(Strict for DocStrict and ListStrict) withconvertIndefiniteLengthEncodings: true, created once and reused throughReset(). The benchmarks are the same as in #137772 and #137943, plus ListStrict.{"S": v}, indefinite length, per mapk: {"L": [{"S": v}, {"S": v}]}, Strict modeIn Lax, this PR was faster than
mainin every round except one Text round, which tied. In Strict, where every nested container now pushes two stacks instead of one, it's about 1% slower (by median: DocStrict 1,943 vs 1,959 ns, ListStrict 3,772 vs 3,813 ns). Allocations are unchanged: 0 B for Text, Pairs, Map1 and ListStrict, and 128 B for Doc and DocStrict (the benchmark's own key strings).Doc is
{"TableName": "my-table", "Item": {...}}, where Item has 20 short-key entries likek: {"S": v}ork: {"N": v}plus"nested": {"M": {...}}with 4 morek: {"S": v}entries: 28 maps, all started withWriteStartMap(null), and 77 short strings (389 bytes), thenEncode(Span<byte>)into a preallocated buffer. ListStrict starts every map and array with a null length (indefinite length).Testing
main. The 2 skipped tests are theRemoteExecutorculture tests, which I couldn't run locally.main, as described in CborWriter: reduce per-call overhead of map and array bookkeeping #137111. Boundary test: 1,512 documents with container sizes and string lengths around 24 and 256, in every conformance mode with conversion on and off. Fuzz: random sequences of the write APIs with misuse mixed in, 10,000 cases per configuration over all four conformance modes, conversion andAllowMultipleRootLevelValueson and off, with both a reused writer and a new writer per case. That is 121,581,530 calls per comparison. After every call, success or exception (type and message),CurrentDepth,BytesWrittenandIsWriteCompletedmatched, and so did the finalEncode()bytes: 0 differences.