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

Projects
None yet
4 participants
@koic
Copy link
Member

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 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

Drenmi approved these changes Sep 4, 2017

Copy link
Collaborator

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
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@bbatsov

This comment has been minimized.

Copy link
Collaborator

commented Sep 10, 2017

👍

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

@koic

This comment has been minimized.

Copy link
Member Author

commented Sep 10, 2017

Thank you!

@mpotter mpotter referenced this pull request Oct 21, 2017

Merged

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.