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

Embed Range literals in the AST #2622

Closed
eregon opened this issue Mar 9, 2022 · 0 comments
Closed

Embed Range literals in the AST #2622

eregon opened this issue Mar 9, 2022 · 0 comments

Comments

@eregon
Copy link
Member

eregon commented Mar 9, 2022

I just remembered, there is a follow-up optimization related to this, range literals can be embedded in the AST:

$ ruby -ve '2.times { p (1..2).object_id }'  
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
60
60

However this is a problem for AST sharing (sharing the same AST for multiple execution contexts, which have different Ruby classes, etc), because RubyRange objects are currently not context-independent, they point to a given RubyClass (private RubyClass metaClass; in RubyDynamicObject).
So to solve this we would need RubyIntRange/RubyLongRange to no longer inherit from RubyRange (we'd also just remove RubyRange), and instead inherit from ImmutableRubyObject.
Then in the translator when building a literal Range with int or long literals for both begin & end (non-beginless & non-endless) we could create a RubyIntRange/RubyLongRange directly in the translator (in org.truffleruby.parser.BodyTranslator#visitDotNode) and use an ObjectLiteralNode to embed the range instance in the AST.
For the cases we need to create a non-frozen range with int/long begin/end we'd go to RubyObjectRange then (e.g., for Range#dup).

Originally posted by @eregon in #2570 (comment)
cc @MattAlp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants