Skip to content

Commit

Permalink
* include/ruby/ruby.h (CONST_ID): constant ID cache for non-gcc.
Browse files Browse the repository at this point in the history
* *.c: no cache in init functions.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17053 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Jun 9, 2008
1 parent 250dd07 commit 5a647a3
Show file tree
Hide file tree
Showing 32 changed files with 141 additions and 85 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Mon Jun 9 18:25:30 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>

* include/ruby/ruby.h (CONST_ID): constant ID cache for non-gcc.

* *.c: no cache in init functions.

Mon Jun 9 17:56:30 2008 Akinori MUSHA <knu@iDaemons.org>

* lib/set.rb (Set#delete_if): Call to_a.
Expand Down
2 changes: 2 additions & 0 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -3400,6 +3400,8 @@ rb_ary_drop_while(VALUE ary)
void
Init_Array(void)
{
#undef rb_intern

rb_cArray = rb_define_class("Array", rb_cObject);
rb_include_module(rb_cArray, rb_mEnumerable);

Expand Down
26 changes: 17 additions & 9 deletions blockinlining.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ build_Integer_times_node(rb_iseq_t *iseq, NODE * node, NODE * lnode,
end
}
*/
ID _self = rb_intern("#_self");
ID _self;
CONST_ID(_self, "#_self");
if (iseq->argc == 0) {
ID e = rb_intern("#e");
ID e;
CONST_ID(e, "#e");
rb_ary_push(param_vars, ID2SYM(e));
rb_ary_push(param_vars, ID2SYM(_self));
iseq->argc += 2;
Expand All @@ -145,10 +147,11 @@ build_Integer_times_node(rb_iseq_t *iseq, NODE * node, NODE * lnode,
Qundef);
}
else {
ID _e = rb_intern("#_e");
ID _e;
ID e = SYM2ID(rb_ary_entry(param_vars, 0));
NODE *assign;

CONST_ID(_e, "#_e");
rb_ary_push(param_vars, ID2SYM(_self));
rb_ary_push(local_vars, ID2SYM(_e));
iseq->argc++;
Expand Down Expand Up @@ -230,9 +233,11 @@ build_Range_each_node(rb_iseq_t *iseq, NODE * node, NODE * lnode,
end
}
*/
ID _last = rb_intern("#_last");
ID _last;
CONST_ID(_last, "#_last");
if (iseq->argc == 0) {
ID e = rb_intern("#e");
ID e;
CONST_ID(e, "#e");
rb_ary_push(param_vars, ID2SYM(e));
rb_ary_push(param_vars, ID2SYM(_last));
iseq->argc += 2;
Expand All @@ -245,10 +250,11 @@ build_Range_each_node(rb_iseq_t *iseq, NODE * node, NODE * lnode,
Qundef);
}
else {
ID _e = rb_intern("#_e");
ID _e;
ID e = SYM2ID(rb_ary_entry(param_vars, 0));
NODE *assign;

CONST_ID(_e, "#_e");
rb_ary_push(param_vars, ID2SYM(_last));
rb_ary_push(local_vars, ID2SYM(_e));
iseq->argc++;
Expand Down Expand Up @@ -362,11 +368,13 @@ build_Array_each_node(rb_iseq_t *iseq, NODE * node, NODE * lnode,
}
*/

ID _self = rb_intern("#_self");
ID _i = rb_intern("#_i");
ID _self, _i;

CONST_ID(_self, "#_self");
CONST_ID(_i, "#_i");
if (iseq->argc == 0) {
ID _e = rb_intern("#_e");
ID _e;
CONST_ID(_e, "#_e");
rb_ary_push(param_vars, ID2SYM(_e));
rb_ary_push(param_vars, ID2SYM(_self));
iseq->argc += 2;
Expand Down
12 changes: 8 additions & 4 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ rb_mod_init_copy(VALUE clone, VALUE orig)
ID id;

RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig));
id = rb_intern("__classpath__");
CONST_ID(id, "__classpath__");
st_delete(RCLASS_IV_TBL(clone), (st_data_t*)&id, 0);
id = rb_intern("__classid__");
CONST_ID(id, "__classid__");
st_delete(RCLASS_IV_TBL(clone), (st_data_t*)&id, 0);
}
if (RCLASS_M_TBL(orig)) {
Expand Down Expand Up @@ -169,10 +169,12 @@ void
rb_singleton_class_attached(VALUE klass, VALUE obj)
{
if (FL_TEST(klass, FL_SINGLETON)) {
ID attached;
if (!RCLASS_IV_TBL(klass)) {
RCLASS_IV_TBL(klass) = st_init_numtable();
}
st_insert(RCLASS_IV_TBL(klass), rb_intern("__attached__"), obj);
CONST_ID(attached, "__attached__");
st_insert(RCLASS_IV_TBL(klass), attached, obj);
}
}

Expand Down Expand Up @@ -214,8 +216,10 @@ rb_define_class_id(ID id, VALUE super)
VALUE
rb_class_inherited(VALUE super, VALUE klass)
{
ID inherited;
if (!super) super = rb_cObject;
return rb_funcall(super, rb_intern("inherited"), 1, klass);
CONST_ID(inherited, "inherited");
return rb_funcall(super, inherited, 1, klass);
}

VALUE
Expand Down
2 changes: 2 additions & 0 deletions compar.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ cmp_between(VALUE x, VALUE min, VALUE max)
void
Init_Comparable(void)
{
#undef rb_intern

rb_mComparable = rb_define_module("Comparable");
rb_define_method(rb_mComparable, "==", cmp_equal, 1);
rb_define_method(rb_mComparable, ">", cmp_gt, 1);
Expand Down
22 changes: 11 additions & 11 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,9 @@ iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *anchor)
static int
iseq_set_exception_local_table(rb_iseq_t *iseq)
{
static ID id_dollar_bang;
ID id_dollar_bang;

if (!id_dollar_bang) {
id_dollar_bang = rb_intern("#$!");
}
CONST_ID(id_dollar_bang, "#$!");
iseq->local_table = (ID *)ALLOC_N(ID *, 1);
iseq->local_table_size = 1;
iseq->local_size = iseq->local_table_size + 1;
Expand Down Expand Up @@ -3718,15 +3716,14 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
}
/* only joke */
{
static ID goto_id;
static ID label_id;
ID goto_id;
ID label_id;
VALUE label;
VALUE label_sym;

if (goto_id == 0) {
goto_id = rb_intern("__goto__");
label_id = rb_intern("__label__");
}

CONST_ID(goto_id, "__goto__");
CONST_ID(label_id, "__label__");

if (nd_type(node) == NODE_FCALL &&
(mid == goto_id || mid == label_id)) {
Expand Down Expand Up @@ -4319,14 +4316,16 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
break;
}
case NODE_SCLASS:{
ID singletonclass;
VALUE iseqval =
NEW_ISEQVAL(node->nd_body, rb_str_new2("singletonclass"),
ISEQ_TYPE_CLASS);

COMPILE(ret, "sclass#recv", node->nd_recv);
ADD_INSN (ret, nd_line(node), putnil);
CONST_ID(singletonclass, "singletonclass");
ADD_INSN3(ret, nd_line(node), defineclass,
ID2SYM(rb_intern("singletonclass")), iseqval, INT2FIX(1));
ID2SYM(singletonclass), iseqval, INT2FIX(1));

if (poped) {
ADD_INSN(ret, nd_line(node), pop);
Expand Down Expand Up @@ -4787,6 +4786,7 @@ register_label(rb_iseq_t *iseq, struct st_table *labels_table, VALUE obj)
static VALUE
get_exception_sym2type(VALUE sym)
{
#undef rb_intern
static VALUE symRescue, symEnsure, symRetry;
static VALUE symBreak, symRedo, symNext;

Expand Down
2 changes: 2 additions & 0 deletions complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,8 @@ numeric_conjugate(VALUE self)
void
Init_Complex(void)
{
#undef rb_intern

assert(fprintf(stderr, "assert() is now active\n"));

id_Unify = rb_intern("Unify");
Expand Down
6 changes: 3 additions & 3 deletions encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,7 @@ enc_check_capable(VALUE x)
ID
rb_id_encoding(void)
{
if (!id_encoding) {
id_encoding = rb_intern("encoding");
}
CONST_ID(id_encoding, "encoding");
return id_encoding;
}

Expand Down Expand Up @@ -1162,6 +1160,8 @@ rb_enc_aliases(VALUE klass)
void
Init_Encoding(void)
{
#undef rb_intern

id_base_encoding = rb_intern("#base_encoding");

rb_cEncoding = rb_define_class("Encoding", rb_cObject);
Expand Down
4 changes: 3 additions & 1 deletion enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ enum_zip(int argc, VALUE *argv, VALUE obj)
}
}
if (!allary) {
conv = rb_intern("to_enum");
CONST_ID(conv, "to_enum");
for (i=0; i<argc; i++) {
argv[i] = rb_funcall(argv[i], conv, 1, ID2SYM(id_each));
}
Expand Down Expand Up @@ -1800,6 +1800,8 @@ enum_cycle(int argc, VALUE *argv, VALUE obj)
void
Init_Enumerable(void)
{
#undef rb_intern

rb_mEnumerable = rb_define_module("Enumerable");

rb_define_method(rb_mEnumerable, "to_a", enum_to_a, -1);
Expand Down
13 changes: 8 additions & 5 deletions error.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,17 +496,17 @@ exc_inspect(VALUE exc)
static VALUE
exc_backtrace(VALUE exc)
{
static ID bt;
ID bt;

if (!bt) bt = rb_intern("bt");
CONST_ID(bt, "bt");
return rb_attr_get(exc, bt);
}

VALUE
rb_check_backtrace(VALUE bt)
{
long i;
static const char *err = "backtrace must be Array of String";
static const char err[] = "backtrace must be Array of String";

if (!NIL_P(bt)) {
int t = TYPE(bt);
Expand Down Expand Up @@ -552,11 +552,12 @@ exc_set_backtrace(VALUE exc, VALUE bt)
static VALUE
exc_equal(VALUE exc, VALUE obj)
{
ID id_mesg = rb_intern("mesg");
ID id_mesg;

if (exc == obj) return Qtrue;
if (rb_obj_class(exc) != rb_obj_class(obj))
return rb_equal(obj, exc);
CONST_ID(id_mesg, "mesg");
if (!rb_equal(rb_attr_get(exc, id_mesg), rb_attr_get(obj, id_mesg)))
return Qfalse;
if (!rb_equal(exc_backtrace(exc), exc_backtrace(obj)))
Expand Down Expand Up @@ -963,7 +964,9 @@ static VALUE
syserr_eqq(VALUE self, VALUE exc)
{
VALUE num, e;
ID en = rb_intern("errno");
ID en;

CONST_ID(en, "errno");

if (!rb_obj_is_kind_of(exc, rb_eSystemCallError)) {
if (!rb_respond_to(exc, en)) return Qfalse;
Expand Down
2 changes: 1 addition & 1 deletion eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ rb_make_exception(int argc, VALUE *argv)
case 3:
n = 1;
exception_call:
exception = rb_intern("exception");
CONST_ID(exception, "exception");
if (!rb_respond_to(argv[0], exception)) {
rb_raise(rb_eTypeError, "exception class/object expected");
}
Expand Down
7 changes: 3 additions & 4 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,14 @@ static VALUE
rb_get_path_check(VALUE obj, int check)
{
VALUE tmp;
static ID to_path;
ID to_path;

if (check) rb_check_safe_obj(obj);
tmp = rb_check_string_type(obj);
if (!NIL_P(tmp)) goto exit;

if (!to_path) {
to_path = rb_intern("to_path");
}

CONST_ID(to_path, "to_path");
if (rb_respond_to(obj, to_path)) {
tmp = rb_funcall(obj, to_path, 0, 0);
}
Expand Down
2 changes: 2 additions & 0 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -2564,6 +2564,8 @@ env_update(VALUE env, VALUE hash)
void
Init_Hash(void)
{
#undef rb_intern

id_hash = rb_intern("hash");
id_yield = rb_intern("yield");
id_default = rb_intern("default");
Expand Down
2 changes: 2 additions & 0 deletions id.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
void
Init_id(void)
{
#undef rb_intern

/* Symbols */
symIFUNC = ID2SYM(rb_intern("<IFUNC>"));
symCFUNC = ID2SYM(rb_intern("<CFUNC>"));
Expand Down
16 changes: 10 additions & 6 deletions include/ruby/ruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -764,17 +764,21 @@ const char *rb_id2name(ID);
ID rb_to_id(VALUE);
VALUE rb_id2str(ID);

#define CONST_ID_CACHE(result, str) \
({ \
static ID rb_intern_id_cache; \
if (!rb_intern_id_cache) \
rb_intern_id_cache = (rb_intern)(str); \
result rb_intern_id_cache; \
})
#define CONST_ID(var, str) \
do {CONST_ID_CACHE(var =, str);} while (0)
#ifdef __GNUC__
/* __builtin_constant_p and statement expression is available
* since gcc-2.7.2.3 at least. */
#define rb_intern(str) \
(__builtin_constant_p(str) ? \
({ \
static ID rb_intern_id_cache; \
if (!rb_intern_id_cache) \
rb_intern_id_cache = rb_intern(str); \
rb_intern_id_cache; \
}) : \
CONST_ID_CACHE(/**/, str) : \
rb_intern(str))
#endif

Expand Down
10 changes: 6 additions & 4 deletions io.c
Original file line number Diff line number Diff line change
Expand Up @@ -4231,7 +4231,7 @@ rb_f_open(int argc, VALUE *argv)
int redirect = Qfalse;

if (argc >= 1) {
to_open = rb_intern("to_open");
CONST_ID(to_open, "to_open");
if (rb_respond_to(argv[0], to_open)) {
redirect = Qtrue;
}
Expand Down Expand Up @@ -6178,11 +6178,11 @@ open_key_args(int argc, VALUE *argv, struct foreach_arg *arg)
if (!encoding) {
ID id;

id = rb_intern("encoding");
CONST_ID(id, "encoding");
encoding = ID2SYM(id);
id = rb_intern("mode");
CONST_ID(id, "mode");
mode = ID2SYM(id);
id = rb_intern("open_args");
CONST_ID(id, "open_args");
open_args = ID2SYM(id);
}
v = rb_hash_aref(opt, open_args);
Expand Down Expand Up @@ -7484,6 +7484,8 @@ rb_get_argv(void)
void
Init_IO(void)
{
#undef rb_intern

VALUE rb_cARGF;
#ifdef __CYGWIN__
#include <sys/cygwin.h>
Expand Down
Loading

0 comments on commit 5a647a3

Please sign in to comment.