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

Source generator tweaks #262

Merged
merged 7 commits into from
Mar 22, 2021
Merged

Conversation

Turnerj
Copy link
Collaborator

@Turnerj Turnerj commented Mar 21, 2021

Various source generator tweaks and improvements:

  • The comment field is now immutable (we were already refusing to parse any classes/enums/properties that didn't have one so it made sense to do)
  • Optimizing the parsing of various properties depending on the type (eg. only parsing schema:domainIncludes for properties only)
  • Deserializing the HTTP stream for the JSON rather than a string buffer (note the HttpCompletionOption which helps this) which can improve performance and allocations
  • Removes the additional Task.Run in the generator's initializer
  • Caches schema object data between different runs of the source generator for the different targets

Other plans:

  • Investigate whether the source generator is triggered for every target (if so, see if we can cache the JSON)
  • Any other performance improvements in processing the JSON

Other notes:

Primarily moves processing only to the branches that need it.
Additionally makes the comment field not-nullable as we perform an initial check at the beginning of the Read method on SchemaPropertyJsonConverter.
Technically the code was asynchronous before however the initial request would buffer internally, which we then serialize to a string and parse back out. Now we start processing the stream as we receive the content - improving performance and decreasing allocations.
@Turnerj Turnerj added the enhancement Issues describing an enhancement or pull requests adding an enhancement. label Mar 21, 2021
@Turnerj
Copy link
Collaborator Author

Turnerj commented Mar 21, 2021

Investigate whether the source generator is triggered for every target (if so, see if we can cache the JSON)

Confirmed - initialization and executing occur multiple times. Next check will be if they use the same instance of the generator and if they do, we can skip the initialization if the SchemaObjects property is not null.

Example Log

Initialize: 21/03/2021 5:21:30 AM
Execute: 21/03/2021 5:21:30 AM
Initialize: 21/03/2021 5:21:39 AM
Execute: 21/03/2021 5:21:39 AM
Initialize: 21/03/2021 5:21:40 AM
Execute: 21/03/2021 5:21:40 AM
Initialize: 21/03/2021 5:21:40 AM
Execute: 21/03/2021 5:21:40 AM
Initialize: 21/03/2021 5:21:40 AM
Execute: 21/03/2021 5:21:40 AM

@Turnerj
Copy link
Collaborator Author

Turnerj commented Mar 21, 2021

Next check will be if they use the same instance of the generator and if they do, we can skip the initialization if the SchemaObjects property is not null.

Unfortunately they use different instances however as a static property, we can share data between them. The multi-target builds seem to overlap so adding some locking to it seems like a good idea.

@Turnerj Turnerj marked this pull request as ready for review March 21, 2021 07:20
@@ -9,8 +9,8 @@ public class SchemaClass : SchemaObject
{
private static readonly Uri EnumerationId = new("https://schema.org/Enumeration");

public SchemaClass(Uri id, string label, string layer)
: base(id, label, layer)
public SchemaClass(string comment, Uri id, string label, string layer)
Copy link
Owner

Choose a reason for hiding this comment

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

Minor comment. It would be nice if the order of parameters in these constructos followed the hierarchy of data.

string layer, Uri id, string label, string comment

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Happy to do that. While I know what hierarchy means, I'm not sure exactly how you're deriving it - like these properties exist in the same hierarchy of inheritance. Just want to understand how you want it.

I went alphabetical for this but felt weird to have comment first.

Copy link
Owner

Choose a reason for hiding this comment

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

I think layer comes first because it groups many types together. The ID for a type makes sense next as it is the unique identifier. After that the label seems to make sense to me, although I can't explain why. After these three, I think going with alphabetical ordering is fine, unless it makes sense to group parameters together somehow.

@RehanSaeed
Copy link
Owner

Nice nice improvements. Do you think we can release a major version after this?

@Turnerj
Copy link
Collaborator Author

Turnerj commented Mar 21, 2021

We can, yeah. I do want to try and get pending types but that may be better after.

Changes the order of constructor parameters and also pushes description/comment and "IsCombined" into constructors, making the properties immutable.
@RehanSaeed
Copy link
Owner

LGTM

@Turnerj Turnerj merged commit 79789f1 into RehanSaeed:main Mar 22, 2021
@Turnerj Turnerj deleted the source-generator-tweaks branch March 25, 2021 08:52
@RehanSaeed RehanSaeed added the patch Pull requests requiring a patch version update according to semantic versioning. label Apr 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Issues describing an enhancement or pull requests adding an enhancement. patch Pull requests requiring a patch version update according to semantic versioning.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants