Skip to content
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

Use initialCapacity in ASN.1 templates for some primitives #81626

Merged
merged 3 commits into from Feb 6, 2023

Conversation

vcsjones
Copy link
Member

@vcsjones vcsjones commented Feb 4, 2023

In our ASN.1 templated encoding, we encode managed values with AsnWriter to determine if they are equal to defaultDerInit.

For some primitives types, like an ASN.1 Boolean and ASN.1 Integer (where the integer is known to be a managed int) this has some waste as AsnWriter has an initial capacity of 1024 bytes.

For booleans, we know the encoded length is always going to be 3 bytes for DER. For integers (where the backing type is int) we know it will encode to at most 6 bytes.

Where possible, let's give these size hints to reduce the initial allocation size. For example, the Critical boolean in X.509 extensions has a default. Before and after of encoding a basic constraints extension:

Method Job Toolchain Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
NewX509BasicConstraints Job-KCCSSM branch 114.83 ns 0.219 ns 0.205 ns 0.76 0.2154 0.0005 - 1,352 B
NewX509BasicConstraints Job-AGAYBV main 150.80 ns 0.413 ns 0.387 ns 1.00 0.3774 0.0019 - 2,368 B

@ghost
Copy link

ghost commented Feb 4, 2023

Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones
See info in area-owners.md if you want to be subscribed.

Issue Details

In our ASN.1 templated encoding, we encode managed values with AsnWriter to determine if they are equal to defaultDerInit.

For some primitives types, like an ASN.1 Boolean and ASN.1 Integer (where the integer is known to be a managed int) this has some waste as AsnWriter has an initial capacity of 1024 bytes.

For booleans, we know the encoded length is always going to be 3 bytes for DER. For integers (where the backing type is int) we know it will encode to at most 6 bytes.

Where possible, let's give these size hints to reduce the initial allocation size. For example, the Critical boolean in X.509 extensions has a default. Before and after of encoding a basic constraints extension:

Method Job Toolchain Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
NewX509BasicConstraints Job-KCCSSM branch 114.83 ns 0.219 ns 0.205 ns 0.76 0.2154 0.0005 - 1,352 B
NewX509BasicConstraints Job-AGAYBV main 150.80 ns 0.413 ns 0.387 ns 1.00 0.3774 0.0019 - 2,368 B
Author: vcsjones
Assignees: vcsjones
Labels:

area-System.Security

Milestone: -

@vcsjones
Copy link
Member Author

vcsjones commented Feb 4, 2023

(If we ever get one-shot encoding that can write to a stack buffer, that would obviously be better, but we can do this right now)

@ghost
Copy link

ghost commented Feb 6, 2023

Tagging subscribers to this area: @dotnet/area-system-formats-asn1, @vcsjones
See info in area-owners.md if you want to be subscribed.

Issue Details

In our ASN.1 templated encoding, we encode managed values with AsnWriter to determine if they are equal to defaultDerInit.

For some primitives types, like an ASN.1 Boolean and ASN.1 Integer (where the integer is known to be a managed int) this has some waste as AsnWriter has an initial capacity of 1024 bytes.

For booleans, we know the encoded length is always going to be 3 bytes for DER. For integers (where the backing type is int) we know it will encode to at most 6 bytes.

Where possible, let's give these size hints to reduce the initial allocation size. For example, the Critical boolean in X.509 extensions has a default. Before and after of encoding a basic constraints extension:

Method Job Toolchain Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
NewX509BasicConstraints Job-KCCSSM branch 114.83 ns 0.219 ns 0.205 ns 0.76 0.2154 0.0005 - 1,352 B
NewX509BasicConstraints Job-AGAYBV main 150.80 ns 0.413 ns 0.387 ns 1.00 0.3774 0.0019 - 2,368 B
Author: vcsjones
Assignees: vcsjones
Labels:

area-System.Formats.Asn1

Milestone: -

@bartonjs bartonjs merged commit 876a97d into dotnet:main Feb 6, 2023
@vcsjones vcsjones deleted the asn-encode-allocs branch February 6, 2023 18:08
@dotnet dotnet locked as resolved and limited conversation to collaborators Mar 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants