Skip to content

Commit 991bacb

Browse files
authored
Merge pull request #2175 from Shopify/ruby-based-constant-pool
Extract out usages of Ruby IDs
2 parents b3c00e8 + 94b0308 commit 991bacb

File tree

9 files changed

+381
-113
lines changed

9 files changed

+381
-113
lines changed

ext/rbs_extension/extconf.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
$INCFLAGS << " -I$(srcdir)/../../include"
55

66
$VPATH << "$(srcdir)/../../src"
7+
$VPATH << "$(srcdir)/../../src/util"
78
$VPATH << "$(srcdir)/ext/rbs_extension"
89

910
root_dir = File.expand_path('../../../', __FILE__)
10-
$srcs = Dir.glob("#{root_dir}/src/*.c") +
11+
$srcs = Dir.glob("#{root_dir}/src/**/*.c") +
1112
Dir.glob("#{root_dir}/ext/rbs_extension/*.c")
1213

1314
append_cflags ['-std=gnu99']

ext/rbs_extension/location.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ static void check_children_cap(rbs_loc *loc) {
5656
}
5757
}
5858

59-
void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r) {
59+
void rbs_loc_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r) {
6060
rbs_loc_add_optional_child(loc, name, r);
6161

6262
unsigned short last_index = loc->children->len - 1;
6363
loc->children->required_p |= 1 << last_index;
6464
}
6565

66-
void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r) {
66+
void rbs_loc_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r) {
6767
check_children_cap(loc);
6868

6969
unsigned short i = loc->children->len++;
@@ -168,14 +168,20 @@ static VALUE location_end_pos(VALUE self) {
168168
return INT2FIX(loc->rg.end);
169169
}
170170

171+
static rbs_constant_id_t rbs_find_constant_id_from_ruby_symbol(VALUE symbol) {
172+
VALUE name = rb_sym2str(symbol);
173+
174+
return rbs_constant_pool_find(RBS_GLOBAL_CONSTANT_POOL, (const uint8_t *) RSTRING_PTR(name), RSTRING_LEN(name));
175+
}
176+
171177
static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) {
172178
rbs_loc *loc = rbs_check_location(self);
173179

174180
range rg;
175181
rg.start = rbs_loc_position(FIX2INT(start));
176182
rg.end = rbs_loc_position(FIX2INT(end));
177183

178-
rbs_loc_add_required_child(loc, SYM2ID(name), rg);
184+
rbs_loc_add_required_child(loc, rbs_find_constant_id_from_ruby_symbol(name), rg);
179185

180186
return Qnil;
181187
}
@@ -187,15 +193,15 @@ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VA
187193
rg.start = rbs_loc_position(FIX2INT(start));
188194
rg.end = rbs_loc_position(FIX2INT(end));
189195

190-
rbs_loc_add_optional_child(loc, SYM2ID(name), rg);
196+
rbs_loc_add_optional_child(loc, rbs_find_constant_id_from_ruby_symbol(name), rg);
191197

192198
return Qnil;
193199
}
194200

195201
static VALUE location_add_optional_no_child(VALUE self, VALUE name) {
196202
rbs_loc *loc = rbs_check_location(self);
197203

198-
rbs_loc_add_optional_child(loc, SYM2ID(name), NULL_RANGE);
204+
rbs_loc_add_optional_child(loc, rbs_find_constant_id_from_ruby_symbol(name), NULL_RANGE);
199205

200206
return Qnil;
201207
}
@@ -221,9 +227,9 @@ static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
221227
static VALUE location_aref(VALUE self, VALUE name) {
222228
rbs_loc *loc = rbs_check_location(self);
223229

224-
ID id = SYM2ID(name);
230+
rbs_constant_id_t id = rbs_find_constant_id_from_ruby_symbol(name);
225231

226-
if (loc->children != NULL) {
232+
if (loc->children != NULL && id != RBS_CONSTANT_ID_UNSET) {
227233
for (unsigned short i = 0; i < loc->children->len; i++) {
228234
if (loc->children->entries[i].name == id) {
229235
rbs_loc_range result = loc->children->entries[i].rg;
@@ -241,6 +247,11 @@ static VALUE location_aref(VALUE self, VALUE name) {
241247
rb_raise(rb_eRuntimeError, "Unknown child name given: %s", RSTRING_PTR(string));
242248
}
243249

250+
static VALUE rbs_constant_to_ruby_symbol(rbs_constant_t *constant) {
251+
// Casts back the Ruby Symbol that was inserted by `rbs_constant_pool_insert_constant()`.
252+
return (VALUE) constant;
253+
}
254+
244255
static VALUE location_optional_keys(VALUE self) {
245256
VALUE keys = rb_ary_new();
246257

@@ -252,8 +263,9 @@ static VALUE location_optional_keys(VALUE self) {
252263

253264
for (unsigned short i = 0; i < children->len; i++) {
254265
if (RBS_LOC_OPTIONAL_P(loc, i)) {
255-
rb_ary_push(keys, ID2SYM(children->entries[i].name));
256-
266+
rbs_constant_t *key_id = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name);
267+
VALUE key_sym = rbs_constant_to_ruby_symbol(key_id);
268+
rb_ary_push(keys, key_sym);
257269
}
258270
}
259271

@@ -271,7 +283,9 @@ static VALUE location_required_keys(VALUE self) {
271283

272284
for (unsigned short i = 0; i < children->len; i++) {
273285
if (RBS_LOC_REQUIRED_P(loc, i)) {
274-
rb_ary_push(keys, ID2SYM(children->entries[i].name));
286+
rbs_constant_t *key_id = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name);
287+
VALUE key_sym = rbs_constant_to_ruby_symbol(key_id);
288+
rb_ary_push(keys, key_sym);
275289
}
276290
}
277291

ext/rbs_extension/location.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "ruby.h"
55
#include "lexer.h"
6+
#include "rbs/util/rbs_constant_pool.h"
67

78
/**
89
* RBS::Location class
@@ -15,7 +16,7 @@ typedef struct {
1516
} rbs_loc_range;
1617

1718
typedef struct {
18-
ID name;
19+
rbs_constant_id_t name;
1920
rbs_loc_range rg;
2021
} rbs_loc_entry;
2122

@@ -58,14 +59,14 @@ void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap);
5859
*
5960
* Allocate memory for children with rbs_loc_alloc_children before calling this function.
6061
* */
61-
void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r);
62+
void rbs_loc_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r);
6263

6364
/**
6465
* Add an optional child range with given name.
6566
*
6667
* Allocate memory for children with rbs_loc_alloc_children before calling this function.
6768
* */
68-
void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r);
69+
void rbs_loc_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r);
6970

7071
/**
7172
* Returns RBS::Location object with start/end positions.

ext/rbs_extension/main.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
#include "rbs_extension.h"
2+
#include "rbs/util/rbs_constant_pool.h"
3+
4+
#include "ruby/vm.h"
5+
6+
static
7+
void Deinit_rbs_extension(ruby_vm_t *_) {
8+
rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
9+
}
210

311
void
412
Init_rbs_extension(void)
513
{
614
#ifdef HAVE_RB_EXT_RACTOR_SAFE
715
rb_ext_ractor_safe(true);
8-
#endif
16+
#endif
917
rbs__init_constants();
1018
rbs__init_location();
1119
rbs__init_parser();
20+
21+
rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, 0);
22+
ruby_vm_at_exit(Deinit_rbs_extension);
1223
}

0 commit comments

Comments
 (0)