Skip to content

Commit 31ffaa0

Browse files
committed
Use rbs_constant_pool for typevar table
1 parent 138e4ec commit 31ffaa0

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

ext/rbs_extension/parser.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,8 +1031,13 @@ static VALUE parse_simple(parserstate *state) {
10311031
return parse_symbol(state);
10321032
}
10331033
case tUIDENT: {
1034-
ID name = INTERN_TOKEN(state, state->current_token);
1034+
const char *name_str = peek_token(state->lexstate, state->current_token);
1035+
size_t name_len = token_bytes(state->current_token);
1036+
1037+
rbs_constant_id_t name = rbs_constant_pool_find(&state->constant_pool, (const uint8_t *) name_str, name_len);
1038+
10351039
if (parser_typevar_member(state, name)) {
1040+
ID name = rb_intern3(name_str, name_len, rb_enc_get(state->lexstate->string));
10361041
return rbs_variable(ID2SYM(name), rbs_location_current_token(state));
10371042
}
10381043
// fallthrough for type name
@@ -1190,8 +1195,13 @@ static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_p
11901195
parser_advance_assert(state, tUIDENT);
11911196
range name_range = state->current_token.range;
11921197

1193-
ID id = INTERN_TOKEN(state, state->current_token);
1194-
VALUE name = ID2SYM(id);
1198+
rbs_constant_id_t id = rbs_constant_pool_insert_shared(
1199+
&state->constant_pool,
1200+
(const uint8_t *) peek_token(state->lexstate, state->current_token),
1201+
token_bytes(state->current_token)
1202+
);
1203+
1204+
VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
11951205

11961206
parser_insert_typevar(state, id);
11971207

ext/rbs_extension/parserstate.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "rbs_extension.h"
2+
#include "rbs/util/rbs_constant_pool.h"
23

34
#define RESET_TABLE_P(table) (table->size == 0)
45

@@ -8,7 +9,7 @@ id_table *alloc_empty_table(void) {
89
*table = (id_table) {
910
.size = 10,
1011
.count = 0,
11-
.ids = calloc(10, sizeof(ID)),
12+
.ids = calloc(10, sizeof(rbs_constant_id_t)),
1213
.next = NULL,
1314
};
1415

@@ -61,7 +62,7 @@ void parser_pop_typevar_table(parserstate *state) {
6162
}
6263
}
6364

64-
void parser_insert_typevar(parserstate *state, ID id) {
65+
void parser_insert_typevar(parserstate *state, rbs_constant_id_t id) {
6566
id_table *table = state->vars;
6667

6768
if (RESET_TABLE_P(table)) {
@@ -70,17 +71,17 @@ void parser_insert_typevar(parserstate *state, ID id) {
7071

7172
if (table->size == table->count) {
7273
// expand
73-
ID *ptr = table->ids;
74+
rbs_constant_id_t *ptr = table->ids;
7475
table->size += 10;
75-
table->ids = calloc(table->size, sizeof(ID));
76-
memcpy(table->ids, ptr, sizeof(ID) * table->count);
76+
table->ids = calloc(table->size, sizeof(rbs_constant_id_t));
77+
memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count);
7778
free(ptr);
7879
}
7980

8081
table->ids[table->count++] = id;
8182
}
8283

83-
bool parser_typevar_member(parserstate *state, ID id) {
84+
bool parser_typevar_member(parserstate *state, rbs_constant_id_t id) {
8485
id_table *table = state->vars;
8586

8687
while (table && !RESET_TABLE_P(table)) {
@@ -332,8 +333,12 @@ parserstate *alloc_parser(VALUE buffer, lexstate *lexer, int start_pos, int end_
332333

333334
.vars = NULL,
334335
.last_comment = NULL,
336+
337+
.constant_pool = {},
335338
};
336339

340+
rbs_constant_pool_init(&parser->constant_pool, 0);
341+
337342
parser_advance(parser);
338343
parser_advance(parser);
339344
parser_advance(parser);
@@ -350,7 +355,15 @@ parserstate *alloc_parser(VALUE buffer, lexstate *lexer, int start_pos, int end_
350355
for (long i = 0; i < rb_array_len(variables); i++) {
351356
VALUE index = INT2FIX(i);
352357
VALUE symbol = rb_ary_aref(1, &index, variables);
353-
parser_insert_typevar(parser, SYM2ID(symbol));
358+
VALUE name = rb_sym2str(symbol);
359+
360+
rbs_constant_id_t id = rbs_constant_pool_insert_shared(
361+
&parser->constant_pool,
362+
(const uint8_t *) RSTRING_PTR(name),
363+
RSTRING_LEN(name)
364+
);
365+
366+
parser_insert_typevar(parser, id);
354367
}
355368
}
356369

@@ -362,5 +375,6 @@ void free_parser(parserstate *parser) {
362375
if (parser->last_comment) {
363376
free_comment(parser->last_comment);
364377
}
378+
rbs_constant_pool_free(&parser->constant_pool);
365379
free(parser);
366380
}

ext/rbs_extension/parserstate.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
#include "location.h"
88

99
/**
10-
* id_table represents a set of IDs.
10+
* id_table represents a set of RBS constant IDs.
1111
* This is used to manage the set of bound variables.
1212
* */
1313
typedef struct id_table {
1414
size_t size;
1515
size_t count;
16-
ID *ids;
16+
rbs_constant_id_t *ids;
1717
struct id_table *next;
1818
} id_table;
1919

@@ -55,6 +55,8 @@ typedef struct {
5555

5656
id_table *vars; /* Known type variables */
5757
comment *last_comment; /* Last read comment */
58+
59+
rbs_constant_pool_t constant_pool;
5860
} parserstate;
5961

6062
comment *alloc_comment(token comment_token, comment *last_comment);
@@ -84,14 +86,14 @@ void parser_pop_typevar_table(parserstate *state);
8486
/**
8587
* Insert new type variable into the latest table.
8688
* */
87-
void parser_insert_typevar(parserstate *state, ID id);
89+
void parser_insert_typevar(parserstate *state, rbs_constant_id_t id);
8890

8991
/**
9092
* Returns true if given type variable is recorded in the table.
9193
* If not found, it goes one table up, if it's not a reset table.
9294
* Or returns false, if it's a reset table.
9395
* */
94-
bool parser_typevar_member(parserstate *state, ID id);
96+
bool parser_typevar_member(parserstate *state, rbs_constant_id_t id);
9597

9698
/**
9799
* Allocate new lexstate object.

0 commit comments

Comments
 (0)