Skip to content

Commit a0f12a0

Browse files
authored
Use ID instead of GENTRY for gvars. (#3278)
Use ID instead of GENTRY for gvars. Global variables are compiled into GENTRY (a pointer to struct rb_global_entry). This patch replace this GENTRY to ID and make the code simple. We need to search GENTRY from ID every time (st_lookup), so additional overhead will be introduced. However, the performance of accessing global variables is not important now a day and this simplicity helps Ractor development.
1 parent 8655c2e commit a0f12a0

File tree

10 files changed

+68
-178
lines changed

10 files changed

+68
-178
lines changed

compile.c

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,13 +2318,6 @@ iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
23182318
case TS_ID: /* ID */
23192319
generated_iseq[code_index + 1 + j] = SYM2ID(operands[j]);
23202320
break;
2321-
case TS_GENTRY:
2322-
{
2323-
struct rb_global_entry *entry =
2324-
(struct rb_global_entry *)(operands[j] & (~1));
2325-
generated_iseq[code_index + 1 + j] = (VALUE)entry;
2326-
}
2327-
break;
23282321
case TS_FUNCPTR:
23292322
generated_iseq[code_index + 1 + j] = operands[j];
23302323
break;
@@ -4875,7 +4868,7 @@ defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
48754868
case NODE_GVAR:
48764869
ADD_INSN(ret, line, putnil);
48774870
ADD_INSN3(ret, line, defined, INT2FIX(DEFINED_GVAR),
4878-
ID2SYM(node->nd_entry->id), needstr);
4871+
ID2SYM(node->nd_entry), needstr);
48794872
return;
48804873

48814874
case NODE_CVAR:
@@ -5262,7 +5255,7 @@ compile_named_capture_assign(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE
52625255
LABEL *fail_label = NEW_LABEL(line), *end_label = NEW_LABEL(line);
52635256

52645257
#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
5265-
ADD_INSN1(ret, line, getglobal, ((VALUE)rb_global_entry(idBACKREF) | 1));
5258+
ADD_INSN1(ret, line, getglobal, ID2SYM(idBACKREF));
52665259
#else
52675260
ADD_INSN2(ret, line, getspecial, INT2FIX(1) /* '~' */, INT2FIX(0));
52685261
#endif
@@ -7599,8 +7592,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
75997592
if (!popped) {
76007593
ADD_INSN(ret, line, dup);
76017594
}
7602-
ADD_INSN1(ret, line, setglobal,
7603-
((VALUE)node->nd_entry | 1));
7595+
ADD_INSN1(ret, line, setglobal, ID2SYM(node->nd_entry));
76047596
break;
76057597
}
76067598
case NODE_IASGN:{
@@ -8212,8 +8204,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
82128204
break;
82138205
}
82148206
case NODE_GVAR:{
8215-
ADD_INSN1(ret, line, getglobal,
8216-
((VALUE)node->nd_entry | 1));
8207+
ADD_INSN1(ret, line, getglobal, ID2SYM(node->nd_entry));
82178208
if (popped) {
82188209
ADD_INSN(ret, line, pop);
82198210
}
@@ -8980,13 +8971,6 @@ insn_data_to_s_detail(INSN *iobj)
89808971
case TS_ID: /* ID */
89818972
rb_str_concat(str, opobj_inspect(OPERAND_AT(iobj, j)));
89828973
break;
8983-
case TS_GENTRY:
8984-
{
8985-
struct rb_global_entry *entry = (struct rb_global_entry *)
8986-
(OPERAND_AT(iobj, j) & (~1));
8987-
rb_str_append(str, rb_id2str(entry->id));
8988-
break;
8989-
}
89908974
case TS_IC: /* inline cache */
89918975
case TS_IVC: /* inline ivar cache */
89928976
case TS_ISE: /* inline storage entry */
@@ -9376,10 +9360,6 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor,
93769360
}
93779361
}
93789362
break;
9379-
case TS_GENTRY:
9380-
op = rb_to_symbol_type(op);
9381-
argv[j] = (VALUE)rb_global_entry(SYM2ID(op));
9382-
break;
93839363
case TS_ISE:
93849364
FL_SET((VALUE)iseq, ISEQ_MARKABLE_ISEQ);
93859365
/* fall through */
@@ -10136,19 +10116,6 @@ ibf_dump_iseq(struct ibf_dump *dump, const rb_iseq_t *iseq)
1013610116
}
1013710117
}
1013810118

10139-
static VALUE
10140-
ibf_dump_gentry(struct ibf_dump *dump, const struct rb_global_entry *entry)
10141-
{
10142-
return (VALUE)ibf_dump_id(dump, entry->id);
10143-
}
10144-
10145-
static VALUE
10146-
ibf_load_gentry(const struct ibf_load *load, const struct rb_global_entry *entry)
10147-
{
10148-
ID gid = ibf_load_id(load, (ID)(VALUE)entry);
10149-
return (VALUE)rb_global_entry(gid);
10150-
}
10151-
1015210119
static unsigned char
1015310120
ibf_load_byte(const struct ibf_load *load, ibf_offset_t *offset)
1015410121
{
@@ -10316,9 +10283,6 @@ ibf_dump_code(struct ibf_dump *dump, const rb_iseq_t *iseq)
1031610283
case TS_ID:
1031710284
wv = ibf_dump_id(dump, (ID)op);
1031810285
break;
10319-
case TS_GENTRY:
10320-
wv = ibf_dump_gentry(dump, (const struct rb_global_entry *)op);
10321-
break;
1032210286
case TS_FUNCPTR:
1032310287
rb_raise(rb_eRuntimeError, "TS_FUNCPTR is not supported");
1032410288
goto skip_wv;
@@ -10403,12 +10367,6 @@ ibf_load_code(const struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t bytecod
1040310367
code[code_index] = ibf_load_id(load, (ID)(VALUE)op);
1040410368
}
1040510369
break;
10406-
case TS_GENTRY:
10407-
{
10408-
VALUE op = ibf_load_small_value(load, &reading_pos);
10409-
code[code_index] = ibf_load_gentry(load, (const struct rb_global_entry *)(VALUE)op);
10410-
}
10411-
break;
1041210370
case TS_FUNCPTR:
1041310371
rb_raise(rb_eRuntimeError, "TS_FUNCPTR is not supported");
1041410372
break;

insns.def

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,25 +288,23 @@ setconstant
288288
/* get global variable id. */
289289
DEFINE_INSN
290290
getglobal
291-
(GENTRY entry)
291+
(ID gid)
292292
()
293293
(VALUE val)
294-
// attr bool leaf = leafness_of_getglobal(entry);
294+
// attr bool leaf = false;
295295
{
296-
struct rb_global_entry *gentry = (void *)entry;
297-
val = rb_gvar_get(gentry);
296+
val = rb_gvar_get(gid);
298297
}
299298

300299
/* set global variable id as val. */
301300
DEFINE_INSN
302301
setglobal
303-
(GENTRY entry)
302+
(ID gid)
304303
(VALUE val)
305304
()
306-
// attr bool leaf = leafness_of_setglobal(entry);
305+
// attr bool leaf = false;
307306
{
308-
struct rb_global_entry *gentry = (void *)entry;
309-
rb_gvar_set(gentry, val);
307+
rb_gvar_set(gid, val);
310308
}
311309

312310
/**********************************************************/

internal/variable.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@
1919

2020
#define ROBJECT_TRANSIENT_FLAG FL_USER13
2121

22-
struct rb_global_variable; /* defined in variable.c */
23-
24-
struct rb_global_entry {
25-
struct rb_global_variable *var;
26-
ID id;
27-
};
28-
2922
/* variable.c */
3023
void rb_gc_mark_global_tbl(void);
3124
void rb_gc_update_global_tbl(void);
@@ -36,9 +29,8 @@ VALUE rb_ivar_lookup(VALUE obj, ID id, VALUE undef);
3629
void rb_autoload_str(VALUE mod, ID id, VALUE file);
3730
VALUE rb_autoload_at_p(VALUE, ID, int);
3831
NORETURN(VALUE rb_mod_const_missing(VALUE,VALUE));
39-
rb_gvar_getter_t *rb_gvar_getter_function_of(const struct rb_global_entry *);
40-
rb_gvar_setter_t *rb_gvar_setter_function_of(const struct rb_global_entry *);
41-
bool rb_gvar_is_traced(const struct rb_global_entry *);
32+
rb_gvar_getter_t *rb_gvar_getter_function_of(ID);
33+
rb_gvar_setter_t *rb_gvar_setter_function_of(ID);
4234
void rb_gvar_readonly_setter(VALUE v, ID id, VALUE *_);
4335
static inline bool ROBJ_TRANSIENT_P(VALUE obj);
4436
static inline void ROBJ_TRANSIENT_SET(VALUE obj);
@@ -55,11 +47,9 @@ void rb_deprecate_constant(VALUE mod, const char *name);
5547
RUBY_SYMBOL_EXPORT_END
5648

5749
MJIT_SYMBOL_EXPORT_BEGIN
58-
struct rb_global_entry *rb_global_entry(ID);
59-
VALUE rb_gvar_get(struct rb_global_entry *);
60-
VALUE rb_gvar_set(struct rb_global_entry *, VALUE);
61-
VALUE rb_gvar_defined(struct rb_global_entry *);
62-
struct st_table *rb_ivar_generic_ivtbl(void);
50+
VALUE rb_gvar_get(ID);
51+
VALUE rb_gvar_set(ID, VALUE);
52+
VALUE rb_gvar_defined(ID);
6353
void rb_const_warn_if_deprecated(const rb_const_entry_t *, VALUE, ID);
6454
MJIT_SYMBOL_EXPORT_END
6555

iseq.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,12 +1946,6 @@ rb_insn_operand_intern(const rb_iseq_t *iseq,
19461946
}
19471947
break;
19481948
}
1949-
case TS_GENTRY:
1950-
{
1951-
struct rb_global_entry *entry = (struct rb_global_entry *)op;
1952-
ret = rb_str_dup(rb_id2str(entry->id));
1953-
}
1954-
break;
19551949

19561950
case TS_IC:
19571951
case TS_IVC:
@@ -2776,12 +2770,6 @@ iseq_data_to_ary(const rb_iseq_t *iseq)
27762770
}
27772771
}
27782772
break;
2779-
case TS_GENTRY:
2780-
{
2781-
struct rb_global_entry *entry = (struct rb_global_entry *)*seq;
2782-
rb_ary_push(ary, ID2SYM(entry->id));
2783-
}
2784-
break;
27852773
case TS_IC:
27862774
case TS_IVC:
27872775
case TS_ISE:

node.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
#define SIMPLE_FIELD1(name, ann) SIMPLE_FIELD(FIELD_NAME_LEN(name, ann), FIELD_NAME_DESC(name, ann))
6464
#define F_CUSTOM1(name, ann) SIMPLE_FIELD1(#name, ann)
6565
#define F_ID(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name)
66-
#define F_GENTRY(name, ann) SIMPLE_FIELD1(#name, ann) A_ID((node->name)->id)
66+
#define F_GENTRY(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name)
6767
#define F_INT(name, ann) SIMPLE_FIELD1(#name, ann) A_INT(node->name)
6868
#define F_LONG(name, ann) SIMPLE_FIELD1(#name, ann) A_LONG(node->name)
6969
#define F_LIT(name, ann) SIMPLE_FIELD1(#name, ann) A_LIT(node->name)

node.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ typedef struct RNode {
164164
struct RNode *node;
165165
ID id;
166166
long state;
167-
struct rb_global_entry *entry;
168167
struct rb_args_info *args;
169168
struct rb_ary_pattern_info *apinfo;
170169
struct rb_fnd_pattern_info *fpinfo;
@@ -228,7 +227,7 @@ typedef struct RNode {
228227

229228
#define nd_stts u1.node
230229

231-
#define nd_entry u3.entry
230+
#define nd_entry u3.id
232231
#define nd_vid u1.id
233232
#define nd_cflag u2.id
234233
#define nd_cval u3.value
@@ -316,7 +315,7 @@ typedef struct RNode {
316315
#define NEW_ZLIST(loc) NEW_NODE(NODE_ZLIST,0,0,0,loc)
317316
#define NEW_HASH(a,loc) NEW_NODE(NODE_HASH,a,0,0,loc)
318317
#define NEW_MASGN(l,r,loc) NEW_NODE(NODE_MASGN,l,0,r,loc)
319-
#define NEW_GASGN(v,val,loc) NEW_NODE(NODE_GASGN,v,val,rb_global_entry(v),loc)
318+
#define NEW_GASGN(v,val,loc) NEW_NODE(NODE_GASGN,v,val,v,loc)
320319
#define NEW_LASGN(v,val,loc) NEW_NODE(NODE_LASGN,v,val,0,loc)
321320
#define NEW_DASGN(v,val,loc) NEW_NODE(NODE_DASGN,v,val,0,loc)
322321
#define NEW_DASGN_CURR(v,val,loc) NEW_NODE(NODE_DASGN_CURR,v,val,0,loc)
@@ -329,7 +328,7 @@ typedef struct RNode {
329328
#define NEW_OP_ASGN_OR(i,val,loc) NEW_NODE(NODE_OP_ASGN_OR,i,val,0,loc)
330329
#define NEW_OP_ASGN_AND(i,val,loc) NEW_NODE(NODE_OP_ASGN_AND,i,val,0,loc)
331330
#define NEW_OP_CDECL(v,op,val,loc) NEW_NODE(NODE_OP_CDECL,v,val,op,loc)
332-
#define NEW_GVAR(v,loc) NEW_NODE(NODE_GVAR,v,0,rb_global_entry(v),loc)
331+
#define NEW_GVAR(v,loc) NEW_NODE(NODE_GVAR,v,0,v,loc)
333332
#define NEW_LVAR(v,loc) NEW_NODE(NODE_LVAR,v,0,0,loc)
334333
#define NEW_DVAR(v,loc) NEW_NODE(NODE_DVAR,v,0,0,loc)
335334
#define NEW_IVAR(v,loc) NEW_NODE(NODE_IVAR,v,0,0,loc)

tool/ruby_vm/models/typemap.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"..." => %w[. TS_VARIABLE],
1515
"CALL_DATA" => %w[C TS_CALLDATA],
1616
"CDHASH" => %w[H TS_CDHASH],
17-
"GENTRY" => %w[G TS_GENTRY],
1817
"IC" => %w[K TS_IC],
1918
"IVC" => %w[A TS_IVC],
2019
"ID" => %w[I TS_ID],

tool/ruby_vm/views/_leaf_helpers.erb

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,6 @@
88
%;
99
#line <%= __LINE__ + 1 %> <%=cstr __FILE__ %>
1010

11-
static bool
12-
leafness_of_getglobal(VALUE gentry)
13-
{
14-
const struct rb_global_entry *e = (void *)gentry;
15-
16-
if (UNLIKELY(rb_gvar_is_traced(e))) {
17-
return false;
18-
}
19-
else {
20-
/* We cannot write this function using a switch() because a
21-
* case label cannot be a function pointer. */
22-
static rb_gvar_getter_t *const allowlist[] = {
23-
rb_gvar_val_getter,
24-
rb_gvar_var_getter,
25-
/* rb_gvar_undef_getter issues rb_warning() */
26-
};
27-
rb_gvar_getter_t *f = rb_gvar_getter_function_of(e);
28-
int i;
29-
30-
for (i = 0; i < numberof(allowlist); i++) {
31-
if (f == allowlist[i]) {
32-
return true;
33-
}
34-
}
35-
return false;
36-
}
37-
}
38-
39-
static bool
40-
leafness_of_setglobal(VALUE gentry)
41-
{
42-
const struct rb_global_entry *e = (void *)gentry;
43-
44-
if (UNLIKELY(rb_gvar_is_traced(e))) {
45-
return false;
46-
}
47-
else {
48-
/* We cannot write this function using a switch() because a
49-
* case label cannot be a function pointer. */
50-
static rb_gvar_setter_t *const allowlist[] = {
51-
rb_gvar_val_setter,
52-
/* rb_gvar_readonly_setter issues rb_name_error() */
53-
rb_gvar_var_setter,
54-
rb_gvar_undef_setter,
55-
};
56-
rb_gvar_setter_t *f = rb_gvar_setter_function_of(e);
57-
int i;
58-
59-
for (i = 0; i < numberof(allowlist); i++) {
60-
if (f == allowlist[i]) {
61-
return true;
62-
}
63-
}
64-
return false;
65-
}
66-
}
67-
6811
#include "iseq.h"
6912

7013
static bool

0 commit comments

Comments
 (0)