-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: InitialCapacity for CborWriter #91978
Comments
Tagging subscribers to this area: @dotnet/area-system-formats-cbor, @bartonjs, @vcsjones Issue DetailsBackground and motivationThe Callers that write large CBOR documents may end up spending a significant amount time constantly re-allocating and copying the buffer as it grows. This was observed in #91969. In this scenario, 98% of the time spent was in Callers that know before hand that they are going to write a large CBOR document currently don't have an easy way to give a hint that the internal buffer should be larger than the default. Conversely, callers that know they are going to create small CBOR documents may give a smaller-than-the-default hint to avoid over-allocating the internal buffer. This is similar to what we did for AsnWriter except now we are doing it for API Proposalnamespace System.Formats.Cbor;
public partial class CborWriter {
public CborWriter(int initialCapacity, CborConformanceMode conformanceMode = CborConformanceMode.Strict, bool convertIndefiniteLengthEncodings = false, bool allowMultipleRootLevelValues = false);
} API UsageCborWriter writer = new CborWriter(initialCapacity: 10000000);
// write 1000000 bytes of stuff to the writer. Alternative DesignsNo response RisksNo response
|
Sounds reasonable to me. |
namespace System.Formats.Cbor;
public partial class CborWriter {
[EditorBrowsable(Never)]
public CborWriter(
CborConformanceMode conformanceMode,
bool convertIndefiniteLengthEncodings,
bool allowMultipleRootLevelValues);
public CborWriter(
CborConformanceMode conformanceMode = CborConformanceMode.Strict,
bool convertIndefiniteLengthEncodings = false,
bool allowMultipleRootLevelValues = false,
int initialCapacity = -1);
} |
Background and motivation
The
CborWriter
class currently has an internal buffer that grows as data is written to the buffer.Callers that write large CBOR documents may end up spending a significant amount time constantly re-allocating and copying the buffer as it grows. This was observed in #91969. In this scenario, 98% of the time spent was in
Array.Resize
.Callers that know before hand that they are going to write a large CBOR document currently don't have an easy way to give a hint that the internal buffer should be larger than the default.
Conversely, callers that know they are going to create small CBOR documents may give a smaller-than-the-default hint to avoid over-allocating the internal buffer.
This is similar to what we did for AsnWriter except now we are doing it for
CborWriter
.API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: