Skip to content

Commit

Permalink
Use ID instead of GENTRY for gvars. (#3278)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ko1 committed Jul 3, 2020
1 parent 8655c2e commit a0f12a0
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 178 deletions.
50 changes: 4 additions & 46 deletions compile.c
Expand Up @@ -2318,13 +2318,6 @@ iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
case TS_ID: /* ID */
generated_iseq[code_index + 1 + j] = SYM2ID(operands[j]);
break;
case TS_GENTRY:
{
struct rb_global_entry *entry =
(struct rb_global_entry *)(operands[j] & (~1));
generated_iseq[code_index + 1 + j] = (VALUE)entry;
}
break;
case TS_FUNCPTR:
generated_iseq[code_index + 1 + j] = operands[j];
break;
Expand Down Expand Up @@ -4875,7 +4868,7 @@ defined_expr0(rb_iseq_t *iseq, LINK_ANCHOR *const ret,
case NODE_GVAR:
ADD_INSN(ret, line, putnil);
ADD_INSN3(ret, line, defined, INT2FIX(DEFINED_GVAR),
ID2SYM(node->nd_entry->id), needstr);
ID2SYM(node->nd_entry), needstr);
return;

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

#if !(defined(NAMED_CAPTURE_BY_SVAR) && NAMED_CAPTURE_BY_SVAR-0)
ADD_INSN1(ret, line, getglobal, ((VALUE)rb_global_entry(idBACKREF) | 1));
ADD_INSN1(ret, line, getglobal, ID2SYM(idBACKREF));
#else
ADD_INSN2(ret, line, getspecial, INT2FIX(1) /* '~' */, INT2FIX(0));
#endif
Expand Down Expand Up @@ -7599,8 +7592,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
if (!popped) {
ADD_INSN(ret, line, dup);
}
ADD_INSN1(ret, line, setglobal,
((VALUE)node->nd_entry | 1));
ADD_INSN1(ret, line, setglobal, ID2SYM(node->nd_entry));
break;
}
case NODE_IASGN:{
Expand Down Expand Up @@ -8212,8 +8204,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
break;
}
case NODE_GVAR:{
ADD_INSN1(ret, line, getglobal,
((VALUE)node->nd_entry | 1));
ADD_INSN1(ret, line, getglobal, ID2SYM(node->nd_entry));
if (popped) {
ADD_INSN(ret, line, pop);
}
Expand Down Expand Up @@ -8980,13 +8971,6 @@ insn_data_to_s_detail(INSN *iobj)
case TS_ID: /* ID */
rb_str_concat(str, opobj_inspect(OPERAND_AT(iobj, j)));
break;
case TS_GENTRY:
{
struct rb_global_entry *entry = (struct rb_global_entry *)
(OPERAND_AT(iobj, j) & (~1));
rb_str_append(str, rb_id2str(entry->id));
break;
}
case TS_IC: /* inline cache */
case TS_IVC: /* inline ivar cache */
case TS_ISE: /* inline storage entry */
Expand Down Expand Up @@ -9376,10 +9360,6 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *const anchor,
}
}
break;
case TS_GENTRY:
op = rb_to_symbol_type(op);
argv[j] = (VALUE)rb_global_entry(SYM2ID(op));
break;
case TS_ISE:
FL_SET((VALUE)iseq, ISEQ_MARKABLE_ISEQ);
/* fall through */
Expand Down Expand Up @@ -10136,19 +10116,6 @@ ibf_dump_iseq(struct ibf_dump *dump, const rb_iseq_t *iseq)
}
}

static VALUE
ibf_dump_gentry(struct ibf_dump *dump, const struct rb_global_entry *entry)
{
return (VALUE)ibf_dump_id(dump, entry->id);
}

static VALUE
ibf_load_gentry(const struct ibf_load *load, const struct rb_global_entry *entry)
{
ID gid = ibf_load_id(load, (ID)(VALUE)entry);
return (VALUE)rb_global_entry(gid);
}

static unsigned char
ibf_load_byte(const struct ibf_load *load, ibf_offset_t *offset)
{
Expand Down Expand Up @@ -10316,9 +10283,6 @@ ibf_dump_code(struct ibf_dump *dump, const rb_iseq_t *iseq)
case TS_ID:
wv = ibf_dump_id(dump, (ID)op);
break;
case TS_GENTRY:
wv = ibf_dump_gentry(dump, (const struct rb_global_entry *)op);
break;
case TS_FUNCPTR:
rb_raise(rb_eRuntimeError, "TS_FUNCPTR is not supported");
goto skip_wv;
Expand Down Expand Up @@ -10403,12 +10367,6 @@ ibf_load_code(const struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t bytecod
code[code_index] = ibf_load_id(load, (ID)(VALUE)op);
}
break;
case TS_GENTRY:
{
VALUE op = ibf_load_small_value(load, &reading_pos);
code[code_index] = ibf_load_gentry(load, (const struct rb_global_entry *)(VALUE)op);
}
break;
case TS_FUNCPTR:
rb_raise(rb_eRuntimeError, "TS_FUNCPTR is not supported");
break;
Expand Down
14 changes: 6 additions & 8 deletions insns.def
Expand Up @@ -288,25 +288,23 @@ setconstant
/* get global variable id. */
DEFINE_INSN
getglobal
(GENTRY entry)
(ID gid)
()
(VALUE val)
// attr bool leaf = leafness_of_getglobal(entry);
// attr bool leaf = false;
{
struct rb_global_entry *gentry = (void *)entry;
val = rb_gvar_get(gentry);
val = rb_gvar_get(gid);
}

/* set global variable id as val. */
DEFINE_INSN
setglobal
(GENTRY entry)
(ID gid)
(VALUE val)
()
// attr bool leaf = leafness_of_setglobal(entry);
// attr bool leaf = false;
{
struct rb_global_entry *gentry = (void *)entry;
rb_gvar_set(gentry, val);
rb_gvar_set(gid, val);
}

/**********************************************************/
Expand Down
20 changes: 5 additions & 15 deletions internal/variable.h
Expand Up @@ -19,13 +19,6 @@

#define ROBJECT_TRANSIENT_FLAG FL_USER13

struct rb_global_variable; /* defined in variable.c */

struct rb_global_entry {
struct rb_global_variable *var;
ID id;
};

/* variable.c */
void rb_gc_mark_global_tbl(void);
void rb_gc_update_global_tbl(void);
Expand All @@ -36,9 +29,8 @@ VALUE rb_ivar_lookup(VALUE obj, ID id, VALUE undef);
void rb_autoload_str(VALUE mod, ID id, VALUE file);
VALUE rb_autoload_at_p(VALUE, ID, int);
NORETURN(VALUE rb_mod_const_missing(VALUE,VALUE));
rb_gvar_getter_t *rb_gvar_getter_function_of(const struct rb_global_entry *);
rb_gvar_setter_t *rb_gvar_setter_function_of(const struct rb_global_entry *);
bool rb_gvar_is_traced(const struct rb_global_entry *);
rb_gvar_getter_t *rb_gvar_getter_function_of(ID);
rb_gvar_setter_t *rb_gvar_setter_function_of(ID);
void rb_gvar_readonly_setter(VALUE v, ID id, VALUE *_);
static inline bool ROBJ_TRANSIENT_P(VALUE obj);
static inline void ROBJ_TRANSIENT_SET(VALUE obj);
Expand All @@ -55,11 +47,9 @@ void rb_deprecate_constant(VALUE mod, const char *name);
RUBY_SYMBOL_EXPORT_END

MJIT_SYMBOL_EXPORT_BEGIN
struct rb_global_entry *rb_global_entry(ID);
VALUE rb_gvar_get(struct rb_global_entry *);
VALUE rb_gvar_set(struct rb_global_entry *, VALUE);
VALUE rb_gvar_defined(struct rb_global_entry *);
struct st_table *rb_ivar_generic_ivtbl(void);
VALUE rb_gvar_get(ID);
VALUE rb_gvar_set(ID, VALUE);
VALUE rb_gvar_defined(ID);
void rb_const_warn_if_deprecated(const rb_const_entry_t *, VALUE, ID);
MJIT_SYMBOL_EXPORT_END

Expand Down
12 changes: 0 additions & 12 deletions iseq.c
Expand Up @@ -1946,12 +1946,6 @@ rb_insn_operand_intern(const rb_iseq_t *iseq,
}
break;
}
case TS_GENTRY:
{
struct rb_global_entry *entry = (struct rb_global_entry *)op;
ret = rb_str_dup(rb_id2str(entry->id));
}
break;

case TS_IC:
case TS_IVC:
Expand Down Expand Up @@ -2776,12 +2770,6 @@ iseq_data_to_ary(const rb_iseq_t *iseq)
}
}
break;
case TS_GENTRY:
{
struct rb_global_entry *entry = (struct rb_global_entry *)*seq;
rb_ary_push(ary, ID2SYM(entry->id));
}
break;
case TS_IC:
case TS_IVC:
case TS_ISE:
Expand Down
2 changes: 1 addition & 1 deletion node.c
Expand Up @@ -63,7 +63,7 @@
#define SIMPLE_FIELD1(name, ann) SIMPLE_FIELD(FIELD_NAME_LEN(name, ann), FIELD_NAME_DESC(name, ann))
#define F_CUSTOM1(name, ann) SIMPLE_FIELD1(#name, ann)
#define F_ID(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name)
#define F_GENTRY(name, ann) SIMPLE_FIELD1(#name, ann) A_ID((node->name)->id)
#define F_GENTRY(name, ann) SIMPLE_FIELD1(#name, ann) A_ID(node->name)
#define F_INT(name, ann) SIMPLE_FIELD1(#name, ann) A_INT(node->name)
#define F_LONG(name, ann) SIMPLE_FIELD1(#name, ann) A_LONG(node->name)
#define F_LIT(name, ann) SIMPLE_FIELD1(#name, ann) A_LIT(node->name)
Expand Down
7 changes: 3 additions & 4 deletions node.h
Expand Up @@ -164,7 +164,6 @@ typedef struct RNode {
struct RNode *node;
ID id;
long state;
struct rb_global_entry *entry;
struct rb_args_info *args;
struct rb_ary_pattern_info *apinfo;
struct rb_fnd_pattern_info *fpinfo;
Expand Down Expand Up @@ -228,7 +227,7 @@ typedef struct RNode {

#define nd_stts u1.node

#define nd_entry u3.entry
#define nd_entry u3.id
#define nd_vid u1.id
#define nd_cflag u2.id
#define nd_cval u3.value
Expand Down Expand Up @@ -316,7 +315,7 @@ typedef struct RNode {
#define NEW_ZLIST(loc) NEW_NODE(NODE_ZLIST,0,0,0,loc)
#define NEW_HASH(a,loc) NEW_NODE(NODE_HASH,a,0,0,loc)
#define NEW_MASGN(l,r,loc) NEW_NODE(NODE_MASGN,l,0,r,loc)
#define NEW_GASGN(v,val,loc) NEW_NODE(NODE_GASGN,v,val,rb_global_entry(v),loc)
#define NEW_GASGN(v,val,loc) NEW_NODE(NODE_GASGN,v,val,v,loc)
#define NEW_LASGN(v,val,loc) NEW_NODE(NODE_LASGN,v,val,0,loc)
#define NEW_DASGN(v,val,loc) NEW_NODE(NODE_DASGN,v,val,0,loc)
#define NEW_DASGN_CURR(v,val,loc) NEW_NODE(NODE_DASGN_CURR,v,val,0,loc)
Expand All @@ -329,7 +328,7 @@ typedef struct RNode {
#define NEW_OP_ASGN_OR(i,val,loc) NEW_NODE(NODE_OP_ASGN_OR,i,val,0,loc)
#define NEW_OP_ASGN_AND(i,val,loc) NEW_NODE(NODE_OP_ASGN_AND,i,val,0,loc)
#define NEW_OP_CDECL(v,op,val,loc) NEW_NODE(NODE_OP_CDECL,v,val,op,loc)
#define NEW_GVAR(v,loc) NEW_NODE(NODE_GVAR,v,0,rb_global_entry(v),loc)
#define NEW_GVAR(v,loc) NEW_NODE(NODE_GVAR,v,0,v,loc)
#define NEW_LVAR(v,loc) NEW_NODE(NODE_LVAR,v,0,0,loc)
#define NEW_DVAR(v,loc) NEW_NODE(NODE_DVAR,v,0,0,loc)
#define NEW_IVAR(v,loc) NEW_NODE(NODE_IVAR,v,0,0,loc)
Expand Down
1 change: 0 additions & 1 deletion tool/ruby_vm/models/typemap.rb
Expand Up @@ -14,7 +14,6 @@
"..." => %w[. TS_VARIABLE],
"CALL_DATA" => %w[C TS_CALLDATA],
"CDHASH" => %w[H TS_CDHASH],
"GENTRY" => %w[G TS_GENTRY],
"IC" => %w[K TS_IC],
"IVC" => %w[A TS_IVC],
"ID" => %w[I TS_ID],
Expand Down
57 changes: 0 additions & 57 deletions tool/ruby_vm/views/_leaf_helpers.erb
Expand Up @@ -8,63 +8,6 @@
%;
#line <%= __LINE__ + 1 %> <%=cstr __FILE__ %>

static bool
leafness_of_getglobal(VALUE gentry)
{
const struct rb_global_entry *e = (void *)gentry;

if (UNLIKELY(rb_gvar_is_traced(e))) {
return false;
}
else {
/* We cannot write this function using a switch() because a
* case label cannot be a function pointer. */
static rb_gvar_getter_t *const allowlist[] = {
rb_gvar_val_getter,
rb_gvar_var_getter,
/* rb_gvar_undef_getter issues rb_warning() */
};
rb_gvar_getter_t *f = rb_gvar_getter_function_of(e);
int i;

for (i = 0; i < numberof(allowlist); i++) {
if (f == allowlist[i]) {
return true;
}
}
return false;
}
}

static bool
leafness_of_setglobal(VALUE gentry)
{
const struct rb_global_entry *e = (void *)gentry;

if (UNLIKELY(rb_gvar_is_traced(e))) {
return false;
}
else {
/* We cannot write this function using a switch() because a
* case label cannot be a function pointer. */
static rb_gvar_setter_t *const allowlist[] = {
rb_gvar_val_setter,
/* rb_gvar_readonly_setter issues rb_name_error() */
rb_gvar_var_setter,
rb_gvar_undef_setter,
};
rb_gvar_setter_t *f = rb_gvar_setter_function_of(e);
int i;

for (i = 0; i < numberof(allowlist); i++) {
if (f == allowlist[i]) {
return true;
}
}
return false;
}
}

#include "iseq.h"

static bool
Expand Down

0 comments on commit a0f12a0

Please sign in to comment.