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

Improve CodeFormatter efficiency #272

Merged
merged 8 commits into from
Oct 4, 2021
Merged

Improve CodeFormatter efficiency #272

merged 8 commits into from
Oct 4, 2021

Conversation

zntfdr
Copy link
Contributor

@zntfdr zntfdr commented May 18, 2021

I've noticed that in CodeFormatter's getOperationContext(_:) we can avoid calling getResponseContext(_:) a few times.

I'm working with a big .yaml file:
In my testing this can save around 25% of memory use, and reduce swaggen's execution by a few seconds as well.

No change in the final output

context["successResponse"] = successResponses.first
context["successType"] = successResponse.flatMap(getResponseContext)?["type"]
context["defaultResponse"] = responses.first { $0.statusCode == nil }.flatMap(getResponseContext)
context["onlySuccessResponses"] = successResponse != nil && responses.count == 1
Copy link
Contributor Author

@zntfdr zntfdr May 18, 2021

Choose a reason for hiding this comment

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

In this PR I'm keeping the current behavior.

However, I wonder:
shouldn't this be context["onlySuccessResponses"] = successResponses.count == responses.count?

Copy link
Owner

Choose a reason for hiding this comment

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

Yep, that would make sense. The behaviour would be slightly different (this would be true with 2 success responses, where before it would be false), but the way it's used in the template, it would actually be more correct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you like me to make this change?

I haven't dove much deeper into the library: I'm not sure what this change could cause.

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah happy with it. It better reflects the name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! I've also updated the CHANGELOG. I think this PR is ready to go! 🎉

Copy link
Owner

Choose a reason for hiding this comment

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

Actually this does seem to have broken something. You can see the result if you run swift test and see the generated diff

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I restored the previous behavior: now all tests pass!

@yonaskolb
Copy link
Owner

Great, thanks @zntfdr. There are definitely many low hanging performance optimisations possible.
Could you add a changelog entry as well please under Changed

@zntfdr
Copy link
Contributor Author

zntfdr commented May 18, 2021

Could you add a changelog entry as well please under Changed

Done 😊

@zntfdr
Copy link
Contributor Author

zntfdr commented May 19, 2021

Updated by also improving getSchemaContext(_:)'s efficiency (it was making unnecessary extra getPropertyContext calls).

I will create new PRs if I spot further similar improvements.

Copy link
Owner

@yonaskolb yonaskolb left a comment

Choose a reason for hiding this comment

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

Thanks @zntfdr! Will release this work today

@yonaskolb yonaskolb merged commit cbe32ef into yonaskolb:master Oct 4, 2021
@zntfdr zntfdr deleted the efficiency branch October 4, 2021 03:23
rusik pushed a commit to agorra/SwagGen that referenced this pull request Oct 14, 2021
* remove unnecessary successResponse

* avoid calling getResponseContext unnecessarily

* update changelog

* avoid calling getSchemaContext unnecessarily

* allow onlySuccessResponses to be true when there are multiple success responses

* restore previous behaviour

* remove no longer true changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants