Skip to content

Commit 8015499

Browse files
authored
Merge pull request #2304 from ksss/parse-object
Reduce Array object allocation during parsing
2 parents 17e4b55 + 6322c7d commit 8015499

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

ext/rbs_extension/parser.c

+22-26
Original file line numberDiff line numberDiff line change
@@ -1088,53 +1088,49 @@ static VALUE parse_simple(parserstate *state) {
10881088
| {} <optional>
10891089
*/
10901090
static VALUE parse_intersection(parserstate *state) {
1091-
range rg;
1092-
rg.start = state->next_token.range.start;
1093-
1091+
position start = state->next_token.range.start;
10941092
VALUE type = parse_optional(state);
1095-
VALUE intersection_types = rb_ary_new();
1093+
if (state->next_token.type != pAMP) {
1094+
return type;
1095+
}
10961096

1097+
VALUE intersection_types = rb_ary_new();
10971098
rb_ary_push(intersection_types, type);
10981099
while (state->next_token.type == pAMP) {
10991100
parser_advance(state);
11001101
rb_ary_push(intersection_types, parse_optional(state));
11011102
}
1102-
1103-
rg.end = state->current_token.range.end;
1104-
1105-
if (rb_array_len(intersection_types) > 1) {
1106-
VALUE location = rbs_new_location(state->buffer, rg);
1107-
type = rbs_intersection(intersection_types, location);
1108-
}
1109-
1110-
return type;
1103+
range rg = (range) {
1104+
.start = start,
1105+
.end = state->current_token.range.end,
1106+
};
1107+
VALUE location = rbs_new_location(state->buffer, rg);
1108+
return rbs_intersection(intersection_types, location);
11111109
}
11121110

11131111
/*
11141112
union ::= {} intersection '|' ... '|' <intersection>
11151113
| {} <intersection>
11161114
*/
11171115
VALUE parse_type(parserstate *state) {
1118-
range rg;
1119-
rg.start = state->next_token.range.start;
1120-
1116+
position start = state->next_token.range.start;
11211117
VALUE type = parse_intersection(state);
1122-
VALUE union_types = rb_ary_new();
1118+
if (state->next_token.type != pBAR) {
1119+
return type;
1120+
}
11231121

1122+
VALUE union_types = rb_ary_new();
11241123
rb_ary_push(union_types, type);
11251124
while (state->next_token.type == pBAR) {
11261125
parser_advance(state);
11271126
rb_ary_push(union_types, parse_intersection(state));
11281127
}
1129-
1130-
rg.end = state->current_token.range.end;
1131-
1132-
if (rb_array_len(union_types) > 1) {
1133-
VALUE location = rbs_new_location(state->buffer, rg);
1134-
type = rbs_union(union_types, location);
1135-
}
1136-
1137-
return type;
1128+
range rg = (range) {
1129+
.start = start,
1130+
.end = state->current_token.range.end,
1131+
};
1132+
VALUE location = rbs_new_location(state->buffer, rg);
1133+
return rbs_union(union_types, location);
11381134
}
11391135

11401136
/*

0 commit comments

Comments
 (0)