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

Short circuit eval regexes in finding module definition #2253

Merged
merged 1 commit into from
Aug 19, 2022

Conversation

dduugg
Copy link
Contributor

@dduugg dduugg commented Aug 17, 2022

I was profiling gem RBI generation for Tapioca and found that it was spending half its time in Pry::WrappedModule::Candidate#class_regexes. I noticed that method evaluates three regexes on every invocation since this change, and have reverted it to the prior short-circuit implementation. I've verified that tests pass locally and that the output of Tapioca RBI generation is unchanged (while running 8% faster). Tapioca has since removed the dependency on this Pry code, but I thought you might find this patch useful regardless.

Note that i think there could be a few more possible optimizations here:

  • The start-of-string anchor (\A) should probably be preferred over the start-of-line anchor (^) in each regex
  • .match? should be preferred over =~ once the minimum supported ruby is 2.4
  • I think that the various invocations of #name are susceptible to the this performance bug, and should be cached in an ivar. I've reduced the invocations by two, but not taken on a larger refactor of WrappedModule.

@kyrylo
Copy link
Member

kyrylo commented Aug 19, 2022

Great insights! I appreciate the work that was done and the tips on possible optimizations.

@kyrylo kyrylo merged commit 43c2d3c into pry:master Aug 19, 2022
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

2 participants