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

Extract out usages of Ruby IDs #2175

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

amomchilov
Copy link
Contributor

@amomchilov amomchilov commented Dec 18, 2024

This PR extracts out an rbs_constant_pool interface, which mimics the pm_constant_pool from Prism.

I use it to wrap all usages of ID as location child keys, instead of directly depending Ruby APIs like rb_intern(), ID2SYM, SYM2ID, etc. This includes the keys used to identify the children in a rb_loc, and the keys used in the parser's typevar tables.

For now, this is just a dummy implementation which delegates to those same Ruby APIs, but will eventually be replaced with Prism's real logic, which doesn't depend on ruby.h at all.

For easier reviewing, have a look at each commit separately.

@amomchilov amomchilov changed the title Extract out usages of Ruby Symbols and IDs Extract out usages of Ruby IDs Dec 18, 2024
@amomchilov amomchilov mentioned this pull request Dec 18, 2024
6 tasks
@amomchilov amomchilov force-pushed the ruby-based-constant-pool branch 2 times, most recently from 4ee0431 to fa6470f Compare December 18, 2024 05:16
@@ -221,9 +239,9 @@ static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
static VALUE location_aref(VALUE self, VALUE name) {
rbs_loc *loc = rbs_check_location(self);

ID id = SYM2ID(name);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this used to leak memory, since SYM2ID would allocate a static symbol.

@@ -1023,8 +1031,13 @@ static VALUE parse_simple(parserstate *state) {
return parse_symbol(state);
}
case tUIDENT: {
ID name = INTERN_TOKEN(state, state->current_token);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This INTERN_TOKEN macro was calling rb_intern3(), which was slowly leaking memory by creating static ("immortal") symbols.

The replacement rbs_constant_pool_find() function uses rb_check_id_cstr() instead, which checks for a match without intern a new symbol if there wasn't one.


/** The overall constant pool, which stores constants found while parsing. */
typedef struct {
void *dummy; // Workaround for structs not being allowed to be empty.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Eventually, this will be replaced with the real storage that will store the constant pool, instead of the one built into the Ruby VM.

@amomchilov amomchilov force-pushed the ruby-based-constant-pool branch from fa6470f to 0b4f69e Compare December 18, 2024 06:09
@amomchilov amomchilov force-pushed the ruby-based-constant-pool branch from 0b4f69e to 3ae33c6 Compare December 18, 2024 06:12
@amomchilov amomchilov marked this pull request as ready for review December 18, 2024 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant