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

Add new Performance/UriDefaultParser cop #4696

Merged

Conversation

koic
Copy link
Member

@koic koic commented Sep 3, 2017

This cop is inspired by #4694 (comment).

Feature

This cop identifies places where URI::Parser.new can be replaced by URI::DEFAULT_PARSER.

% cat /tmp/test.rb
# frozen_string_literal: true

require 'uri'

URI::Parser.new.make_regexp
% bundle exec rubocop /tmp/test.rb
Inspecting 1 file
C

Offenses:

/tmp/test.rb:5:1: C: Use URI::DEFAULT_PARSER instead of URI::Parser.new.
URI::Parser.new.make_regexp
^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

Target Problem

The following is a benchmark script.

require 'benchmark/ips'
require 'uri'

Benchmark.ips do |x|
  x.report('URI::Parser.new')     { URI::Parser.new }
  x.report('URI::DEFAULT_PARSER') { URI::DEFAULT_PARSER }
  x.compare!
end

It is the result of running the above benchmark script.

% ruby bench.rb
Warming up --------------------------------------
     URI::Parser.new    70.000  i/100ms
 URI::DEFAULT_PARSER   315.505k i/100ms
Calculating -------------------------------------
     URI::Parser.new    714.537  (± 4.2%) i/s -      3.570k in   5.005495s
 URI::DEFAULT_PARSER     10.950M (± 6.2%) i/s -     54.582M in   5.007247s

Comparison:
 URI::DEFAULT_PARSER: 10949722.8 i/s
     URI::Parser.new:      714.5 i/s - 15324.22x  slower

It is a constant as follows and it is fast because it does not instantiate every time.
https://github.com/ruby/ruby/blob/v2_4_1/lib/uri/common.rb#L22


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

@koic koic force-pushed the add_new_performance_uri_default_parser_cop branch from 7ac496b to 860dae9 Compare September 3, 2017 13:46
@koic koic force-pushed the add_new_performance_uri_default_parser_cop branch from 860dae9 to fba8446 Compare September 4, 2017 05:51
Copy link
Collaborator

@Drenmi Drenmi left a comment

Choose a reason for hiding this comment

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

15324.22x slower

😅

@bbatsov bbatsov merged commit ff8504b into rubocop:master Sep 10, 2017
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 10, 2017

👍

@koic koic deleted the add_new_performance_uri_default_parser_cop branch September 10, 2017 07:49
@koic
Copy link
Member Author

koic commented Sep 10, 2017

Thank you!

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.

None yet

4 participants