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

Optimize Doc::Method#compute_doc_info to avoid duplicate regex #13324

Conversation

straight-shoota
Copy link
Member

The algorithm executed basically the same regex twice: once to see whether it matches and then to replace all occurrences.
This patch reduces it to a single regex match by avoiding the initial check.

return DocInfo.new(def_doc, nil)
end
# TODO: warn about `:inherit:` not finding an ancestor
inherit_def_doc = def_doc.gsub(/^[ \t]*:inherit:[ \t]*$/m) { ancestor_doc_info.try(&.doc) || break }
Copy link
Contributor

Choose a reason for hiding this comment

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

This would recompute #ancestor_doc_info on every iteration. Normally docs shouldn't contain more than one :inherit: but it doesn't hurt to cache that in a local variable

Copy link
Member

@oprypin oprypin Apr 17, 2023

Choose a reason for hiding this comment

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

Now it's still being recomputed if self.inherit_def_doc returns nil. This double meaning of nil is also bad for understanding the code. I'd just drop the caching (revert that commit).

Copy link
Member

Choose a reason for hiding this comment

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

Now it's still being recomputed if self.inherit_def_doc returns nil. This double meaning of nil is also bad for understanding the code. I'd just drop the caching (revert that commit).

(this issue still stands; reformatting didn't change the behavior)

Copy link
Member Author

@straight-shoota straight-shoota Apr 18, 2023

Choose a reason for hiding this comment

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

Ah sorry, I missed your comment here @oprypin

I suppose you mean self.ancestor_doc_info?
That doesn't get recomputed because when it returns nil, the block breaks and there's no repeat iteration.

@straight-shoota straight-shoota added this to the 1.9.0 milestone Apr 18, 2023
@straight-shoota straight-shoota merged commit 9bbd6c2 into crystal-lang:master Apr 20, 2023
@straight-shoota straight-shoota deleted the refactor/doc-generator-regex-doc_info branch April 20, 2023 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants