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

Avoid repeated allocations of node-type array #1452

Merged
merged 1 commit into from Jan 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/yard/parser/ruby/ruby_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ def file_encoding
AST_TOKENS = [:CHAR, :backref, :const, :cvar, :gvar, :heredoc_end, :ident,
:int, :float, :ivar, :label, :period, :regexp_end, :tstring_content, :backtick]

COMMENT_SKIP_NODE_TYPES = [
:comment,
:void_stmt,
:list
].freeze

MAPPINGS.each do |k, v|
if Array === v
v.each {|vv| (REV_MAPPINGS[vv] ||= []) << k }
Expand Down Expand Up @@ -613,7 +619,7 @@ def comment_starts_line?(charno)

def insert_comments
root.traverse do |node|
next if [:comment, :void_stmt, :list].include?(node.type) || node.parent.type != :list
next if COMMENT_SKIP_NODE_TYPES.include?(node.type) || node.parent.type != :list
Copy link
Contributor

Choose a reason for hiding this comment

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

Because of how Set is implemented in MRI, the array implementation is likely to be faster at this scale. (set is also not a prior dependency of yard, although that is moot as of Ruby 3.2)

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review. I am now using an array again.


# never attach comments to if/unless mod nodes
if node.type == :if_mod || node.type == :unless_mod
Expand Down