-
Notifications
You must be signed in to change notification settings - Fork 116
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
Don't instantiate or use Regexp when string.start_with?
will do
#3177
Conversation
🦋 Changeset detectedLatest commit: e2072fb The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
I should add that I looked for other calls to |
- See Rubular.com for regexes -- \A is preferable
Changeset added -- chose patch given that it should not change functionality. Please advise if I need to correct it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thank you! 🎉
What are you trying to accomplish?
While doing some unrelated profiling work in GitHub, I noticed
infer_selector_key
coming up often. I looked at the source because I was curious, and saw it was using thisHash
of a bunch ofRegexp
. At first I though the goal ininfer_selector_key
was to gsub (replace) the contents ofselector
, but it looks like it just returns the value of the hash if it matches.Regexp
instances have a little bit more overhead than Strings, especially when String'sstart_with?
is written all in C.Generally, we don't need to wrap things in Regexp unless we want to do something complex with capture groups, etc., in Ruby.
Before making a change, I wrote a benchmark using benchmark-ips (which figures out how many Iterations-Per-Second the blocks of code can do, on its own, so I didn't have to pick statistically-significant iteration counts) and this script just isolates out the logic of the change we want to use --
And the outputs:
As you can see, switching to a string and using
start_with
is faster when we isolate out looping over the Hash, etc.On the code I was looking at, the
start_with?
version getting called 1306 times should result in roughly a 3ms faster execution. Not huge, but since I could see this improvement, I made it.The only place where Regexp.new was actually needed as in
lib/tasks/utilities.rake
where we do a sub, but it was also essentially wrapping the keys and therefore callingRegexp.new(Regexp.new("^f"))
(or whatever the value inside was. The rake task probably doesn't get called as much as the class is used, but we should still benefit here.Integration
No, this should not change any usage of
infer_selector_key
.List the issues that this change affects.
Closes #3176
Risk Assessment
What approach did you choose and why?
See above with benchmarks.
Anything you want to highlight for special attention from reviewers?
Accessibility
N/A should not change functionality so should not affect accessibility
Merge checklist
Take a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.