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

Use Prism to determine valid names #2060

Merged
merged 1 commit into from
Oct 30, 2024

Conversation

vinistock
Copy link
Member

Motivation

While hunting for memory optimizations, I noticed that a lot of memory was being retained by RubyVM::InstructionSequence. The only place in Tapioca where we use this is to determine if method or parameter names are valid to avoid generating incorrect RBIs.

We can ditch this internal API and rely on Prism to achieve the same, which won't retain memory and is actually faster.

Implementation

Switched to using Prism for determining if a method/parameter name is valid. I benchmarked the current approach vs the one on this branch and these were the results:

Tapioca
  RBIHelper
ruby 3.3.4 (2024-07-09 revision be1089c8ec) [arm64-darwin23]
Warming up --------------------------------------
                 new   474.000 i/100ms
Calculating -------------------------------------
                 new      4.767k (± 3.8%) i/s  (209.79 μs/i) -     24.174k in   5.079176s

Comparison:
                 new:     4766.7 i/s
                 old:     3113.2 i/s - 1.53x  slower

Tests

Current tests should cover it.

@vinistock vinistock added the enhancement New feature or request label Oct 30, 2024
@vinistock vinistock self-assigned this Oct 30, 2024
@vinistock vinistock requested a review from a team as a code owner October 30, 2024 13:18
@vinistock vinistock force-pushed the vs-use-prism-to-determine-valid-names branch from e62913d to 04233fd Compare October 30, 2024 13:46
op == :definemethod && arg == name.to_sym
rescue SyntaxError
false
# Special case: Prism supports parsing `def @foo; end`, but the Sorbet parser doesn't. This condition can go away
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prism supports parsing def @foo; end

Is that a bug?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if Prism should be rejecting this syntax or not. @kddnewton thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's definitely a bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib/tapioca/helpers/rbi_helper.rb Show resolved Hide resolved
@vinistock vinistock merged commit 954a09f into main Oct 30, 2024
28 checks passed
@vinistock vinistock deleted the vs-use-prism-to-determine-valid-names branch October 30, 2024 16:58
# once Sorbet is using Prism under the hood as it will no longer result in an RBI that Sorbet can't parse
return false if name.start_with?("@")

result = Prism.parse("def #{name}(a); end")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not test Prism.parse("def self.#{name}(a); end") instead? I think it would address both the @foo concern and the self.foo one with just the parse.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, great idea #2062.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants