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

Cop idea: Switch regex groups to non capturing if not needed #7799

Closed
choosen opened this issue Mar 13, 2020 · 3 comments
Closed

Cop idea: Switch regex groups to non capturing if not needed #7799

choosen opened this issue Mar 13, 2020 · 3 comments
Labels
stale Issues that haven't been active in a while

Comments

@choosen
Copy link

choosen commented Mar 13, 2020

Is your feature request related to a problem? Please describe.

We don't need to use captured groups using ruby methods for matching content against regex or splitting. Using them will affect badly on performance of RE.
Exception is the back references in regex, so for sure we need regexp parser for that.

# bad
'str'.match? /(?<X>s)/
# => true

# bad
'str'.match? /(s)/
# => true

# good
'str'.match? /s/
# => true

Describe the solution you'd like

Add a cop to detect this problem, maybe also autofix ?

Describe alternatives you've considered

It can be optimized on ruby regex library without cop ? https://github.com/k-takata/Onigmo

Additional context

During the splitting we also don't check/fetch groups so it can be more beneficial to omit groups

It's not only regexp optimization because it is bound with ruby method using this regexps

require 'benchmark/ips'

regexp1 = %r{\s([\&/]\s)?}
regexp2 = %r{\s(?:[\&/]\s)?}
string = 'HR / People & People Ops'

Benchmark.ips do |x|
  x.report("with group") do
    string.split regexp1
  end

  x.report("without capture") do
    string.split regexp2
  end

  x.compare!
end

Calculating -------------------------------------
          with group    495.069k (± 2.7%) i/s -      2.496M in   5.045809s
     without capture    555.395k (± 1.4%) i/s -      2.797M in   5.037793s

Comparison:
     without capture:   555395.2 i/s
          with group:   495069.1 i/s - 1.12x  slower
@choosen
Copy link
Author

choosen commented Mar 13, 2020

somehow related with #7746

@stale
Copy link

stale bot commented Sep 10, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

@stale stale bot added the stale Issues that haven't been active in a while label Sep 10, 2020
@stale
Copy link

stale bot commented Dec 11, 2020

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.

@stale stale bot closed this as completed Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Issues that haven't been active in a while
Projects
None yet
Development

No branches or pull requests

1 participant