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

Reduce Array object allocation during parsing #2304

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions ext/rbs_extension/parser.c
Original file line number Diff line number Diff line change
@@ -1088,53 +1088,49 @@ static VALUE parse_simple(parserstate *state) {
| {} <optional>
*/
static VALUE parse_intersection(parserstate *state) {
range rg;
rg.start = state->next_token.range.start;

position start = state->next_token.range.start;
VALUE type = parse_optional(state);
VALUE intersection_types = rb_ary_new();
if (state->next_token.type != pAMP) {
return type;
}

VALUE intersection_types = rb_ary_new();
rb_ary_push(intersection_types, type);
while (state->next_token.type == pAMP) {
parser_advance(state);
rb_ary_push(intersection_types, parse_optional(state));
}

rg.end = state->current_token.range.end;

if (rb_array_len(intersection_types) > 1) {
VALUE location = rbs_new_location(state->buffer, rg);
type = rbs_intersection(intersection_types, location);
}

return type;
range rg = (range) {
.start = start,
.end = state->current_token.range.end,
};
VALUE location = rbs_new_location(state->buffer, rg);
return rbs_intersection(intersection_types, location);
}

/*
union ::= {} intersection '|' ... '|' <intersection>
| {} <intersection>
*/
VALUE parse_type(parserstate *state) {
range rg;
rg.start = state->next_token.range.start;

position start = state->next_token.range.start;
VALUE type = parse_intersection(state);
VALUE union_types = rb_ary_new();
if (state->next_token.type != pBAR) {
return type;
}

VALUE union_types = rb_ary_new();
rb_ary_push(union_types, type);
while (state->next_token.type == pBAR) {
parser_advance(state);
rb_ary_push(union_types, parse_intersection(state));
}

rg.end = state->current_token.range.end;

if (rb_array_len(union_types) > 1) {
VALUE location = rbs_new_location(state->buffer, rg);
type = rbs_union(union_types, location);
}

return type;
range rg = (range) {
.start = start,
.end = state->current_token.range.end,
};
VALUE location = rbs_new_location(state->buffer, rg);
return rbs_union(union_types, location);
}

/*