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



None yet
4 participants
Copy link

commented Sep 3, 2017

This cop is inspired by #4694 (comment).


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

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

require 'uri'
% bundle exec rubocop /tmp/test.rb
Inspecting 1 file


/tmp/test.rb:5:1: C: Use URI::DEFAULT_PARSER instead of

1 file inspected, 1 offense detected

Target Problem

The following is a benchmark script.

require 'benchmark/ips'
require 'uri'

Benchmark.ips do |x|'')     { }'URI::DEFAULT_PARSER') { URI::DEFAULT_PARSER }!

It is the result of running the above benchmark script.

% ruby bench.rb
Warming up --------------------------------------    70.000  i/100ms
 URI::DEFAULT_PARSER   315.505k i/100ms
Calculating -------------------------------------    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

 URI::DEFAULT_PARSER: 10949722.8 i/s      714.5 i/s - 15324.22x  slower

It is a constant as follows and it is fast because it does not instantiate every time.

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 koic:add_new_performance_uri_default_parser_cop branch from 7ac496b to 860dae9 Sep 3, 2017

@koic koic force-pushed the koic:add_new_performance_uri_default_parser_cop branch from 860dae9 to fba8446 Sep 4, 2017


Drenmi approved these changes Sep 4, 2017

Copy link

left a comment

15324.22x slower


@bbatsov bbatsov merged commit ff8504b into rubocop-hq:master Sep 10, 2017

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
continuous-integration/travis-ci/pr The Travis CI build passed

This comment has been minimized.

Copy link

commented Sep 10, 2017


@koic koic deleted the koic:add_new_performance_uri_default_parser_cop branch Sep 10, 2017


This comment has been minimized.

Copy link
Member Author

commented Sep 10, 2017

Thank you!

@mpotter mpotter referenced this pull request Oct 21, 2017


Upgrade to rubocop 0.51.0 #55

5 of 5 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.