Skip to content

AssemblyBuilder.Save add custom attributes handling. #84580

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

Merged
merged 13 commits into from
Apr 26, 2023

Conversation

buyaa-n
Copy link
Contributor

@buyaa-n buyaa-n commented Apr 10, 2023

Contributes to #62956
This PR include following updates:

  • Expose internal ConstructorInfo Ctor, byte[] Data properties from CustomAttributeBuilder and use them for SetCustomAttribute(CustomAttributeBuilder customBuilder) in shared *Builder types. (initially was planning to make the fields internal, but Mono using properties).
  • Remove protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) implementations and convert byte[] binaryAttribute parameter of SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) into ReadOnlySpan<byte> binaryAttribute for all runtime *Builder types
  • Add CustomAttribute handling for new AssemblyBuilder implementation and corresponding tests
  • Add Pseudo custom attributes handling and testing

@ghost ghost assigned buyaa-n Apr 10, 2023
@ghost
Copy link

ghost commented Apr 10, 2023

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost
Copy link

ghost commented Apr 10, 2023

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

Issue Details

This PR include following updates:

  • Expose internal ConstructorInfo Ctor, byte[] Data properties from CustomAttributeBuilder and use them for SetCustomAttribute(CustomAttributeBuilder customBuilder) in shared *Builder types. (initially was planning to make the fields internal, but Mono using properties).
  • Remove protected override void SetCustomAttributeCore(CustomAttributeBuilder customBuilder) implementations from runtime *Builder types
  • Add CustomAttribute handling for new AssemblyBuilder implementation and corresponding tests
  • Pseudo custom attributes handling is not complete, so creating a draft PR
Author: buyaa-n
Assignees: buyaa-n
Labels:

area-System.Reflection, new-api-needs-documentation

Milestone: -

@buyaa-n buyaa-n marked this pull request as ready for review April 14, 2023 00:33

m_con = con;
m_binaryAttribute = binaryAttribute;
m_binaryAttribute = binaryAttribute.ToArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is introducing an extra array copy. Do you remember whether this regression was considered during the API review discussion that proposed changing the argument type to ReadOnlySpan?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you remember whether this regression was considered during the API review discussion that proposed changing the argument type to ReadOnlySpan?

No, I don't think so, there were not much discussion about this change, quickly agreed for this update. Would you suggest to keep the SetCustomAttributeCore(ConstructorInfo con, byte[] binaryAttribute) for now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the extra copy is fine, it is just something that I have noticed and that I did not expect. I do not have a strong opinion about it. What do you think?

Copy link
Contributor Author

@buyaa-n buyaa-n Apr 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this path would be used rarely (attributes for generic type parameter) so probably fine, but for the new assembly builder implementation we are creating this copy for each attribute:


I think I can workaround that to keep the BlobHandle of the ReadOnlySpan<byte> instead of byte[] in CustomAttributeWrapper. But that needs #81059 merged and a public API for ReadOnlySpan<byte> overload is added

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added TODO for using BlobHandle instead of byte[].

There is similar allocation added in mono:

It is OK in case the SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) overload used directly as previously it was also cloning the byte array but as you know if the SetCustomAttribute(CustomAttributeBuilder customBuilder) overload is used it will call SetCustomAttributeCore(customBuilder.Ctor, customBuilder.Data) and going to create another copy ...

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@buyaa-n
Copy link
Contributor Author

buyaa-n commented Apr 26, 2023

Thank you for review!

Failures unrelated and Build Analysis shows that all are known, merging.

@buyaa-n buyaa-n merged commit 2a75e65 into dotnet:main Apr 26, 2023
@buyaa-n buyaa-n deleted the custom_attribute branch April 26, 2023 16:28
@ghost ghost locked as resolved and limited conversation to collaborators May 26, 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.

5 participants