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 regex allocations by checking arity condition first #1455

Merged
merged 1 commit into from Sep 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/yard/parser/ruby/ruby_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def file_encoding
PARSER_EVENT_TABLE.each do |event, arity|
node_class = AstNode.node_class_for(event)

if /_new\z/ =~ event.to_s && arity == 0
if arity == 0 && /_new\z/ =~ event.to_s
Copy link
Contributor

Choose a reason for hiding this comment

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

Could the regex check be replaced with event.to_s.end_with?('_new')

Copy link
Owner

Choose a reason for hiding this comment

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

arity is better since ends_with breaks prior Ruby compatibility.

Copy link
Contributor

@dduugg dduugg Aug 18, 2022

Choose a reason for hiding this comment

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

What’s the minimum supported ruby version? The gemspec says it depends on webrick 1.7.0, which requires ruby >= 2.3.0, which supports String#end_with?

edit: confirmed yard installation fails in ruby 2.2.10:

$ gem install yard                                                                                                                                                    17:39
Fetching: webrick-1.7.0.gem (100%)
ERROR:  Error installing yard:
	webrick requires Ruby version >= 2.3.0.

Copy link
Author

Choose a reason for hiding this comment

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

CONTRIBUTING.md says:

Please also remember that YARD supports a number of environments, including OS X, Linux, Windows, and a number of older Ruby versions (1.8+)

If the minimum version is no longer 1.8, then some of the =~ checks can indeed be replaced with less expensive end_with? and start_with? checks.

Copy link
Owner

Choose a reason for hiding this comment

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

Good to know that end_with is available, but a little moot since arity is already here :)

module_eval(<<-eof, __FILE__, __LINE__ + 1)
def on_#{event}(*args)
#{node_class}.new(:list, args, :listchar => charno...charno, :listline => lineno..lineno)
Expand Down