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

* *.c: no cache in init functions.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17053 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2008-06-09 09:25:32 +00:00
parent 250dd07021
commit 5a647a3f5f
32 changed files with 141 additions and 85 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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;
@ -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++;
@ -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;
@ -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++;
@ -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;

12
class.c
View File

@ -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)) {
@ -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);
}
}
@ -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

View File

@ -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);

View File

@ -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;
@ -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)) {
@ -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);
@ -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;

View File

@ -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");

View File

@ -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;
}
@ -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);

4
enum.c
View File

@ -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));
}
@ -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);

13
error.c
View File

@ -496,9 +496,9 @@ 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);
}
@ -506,7 +506,7 @@ 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);
@ -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)))
@ -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;

2
eval.c
View File

@ -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");
}

7
file.c
View File

@ -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);
}

2
hash.c
View File

@ -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");

2
id.c
View File

@ -18,6 +18,8 @@
void
Init_id(void)
{
#undef rb_intern
/* Symbols */
symIFUNC = ID2SYM(rb_intern("<IFUNC>"));
symCFUNC = ID2SYM(rb_intern("<CFUNC>"));

View File

@ -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

10
io.c
View File

@ -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;
}
@ -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);
@ -7484,6 +7484,8 @@ rb_get_argv(void)
void
Init_IO(void)
{
#undef rb_intern
VALUE rb_cARGF;
#ifdef __CYGWIN__
#include <sys/cygwin.h>

12
iseq.c
View File

@ -956,12 +956,12 @@ exception_type2symbol(VALUE type)
{
ID id;
switch(type) {
case CATCH_TYPE_RESCUE: id = rb_intern("rescue"); break;
case CATCH_TYPE_ENSURE: id = rb_intern("ensure"); break;
case CATCH_TYPE_RETRY: id = rb_intern("retry"); break;
case CATCH_TYPE_BREAK: id = rb_intern("break"); break;
case CATCH_TYPE_REDO: id = rb_intern("redo"); break;
case CATCH_TYPE_NEXT: id = rb_intern("next"); break;
case CATCH_TYPE_RESCUE: CONST_ID(id, "rescue"); break;
case CATCH_TYPE_ENSURE: CONST_ID(id, "ensure"); break;
case CATCH_TYPE_RETRY: CONST_ID(id, "retry"); break;
case CATCH_TYPE_BREAK: CONST_ID(id, "break"); break;
case CATCH_TYPE_REDO: CONST_ID(id, "redo"); break;
case CATCH_TYPE_NEXT: CONST_ID(id, "next"); break;
default:
rb_bug("...");
}

View File

@ -1669,6 +1669,8 @@ marshal_load(int argc, VALUE *argv)
void
Init_marshal(void)
{
#undef rb_intern
VALUE rb_mMarshal = rb_define_module("Marshal");
s_dump = rb_intern("_dump");

View File

@ -3092,6 +3092,8 @@ fix_even_p(VALUE num)
void
Init_Numeric(void)
{
#undef rb_intern
#if defined(__FreeBSD__) && __FreeBSD__ < 4
/* allow divide by zero -- Inf */
fpsetmask(fpgetmask() & ~(FP_X_DZ|FP_X_INV|FP_X_OFL));

View File

@ -2365,6 +2365,8 @@ boot_defclass(const char *name, VALUE super)
void
Init_Object(void)
{
#undef rb_intern
VALUE metaclass;
rb_cBasicObject = boot_defclass("BasicObject", 0);

View File

@ -7761,9 +7761,9 @@ assignable_gen(struct parser_params *parser, ID id, NODE *val)
static void
shadowing_lvar_gen(struct parser_params *parser, ID name)
{
static ID uscore;
ID uscore;
if (!uscore) uscore = rb_intern("_");
CONST_ID(uscore, "_");
if (uscore == name) return;
if (dyna_in_block()) {
if (dvar_curr(name)) {

2
prec.c
View File

@ -124,6 +124,8 @@ prec_included(VALUE module, VALUE include)
void
Init_Precision(void)
{
#undef rb_intern
rb_mPrecision = rb_define_module("Precision");
rb_define_singleton_method(rb_mPrecision, "included", prec_included, 1);
rb_define_method(rb_mPrecision, "prec", prec_prec, 1);

View File

@ -896,6 +896,8 @@ range_alloc(VALUE klass)
void
Init_Range(void)
{
#undef rb_intern
id_cmp = rb_intern("<=>");
id_succ = rb_intern("succ");
id_beg = rb_intern("begin");

View File

@ -1501,6 +1501,8 @@ nurat_s_induced_from(VALUE klass, VALUE n)
void
Init_Rational(void)
{
#undef rb_intern
assert(fprintf(stderr, "assert() is now active\n"));
id_Unify = rb_intern("Unify");

3
ruby.c
View File

@ -452,9 +452,10 @@ static void
require_libraries(struct cmdline_options *opt)
{
VALUE list = opt->req_list;
ID require = rb_intern("require");
ID require;
Init_ext(); /* should be called here for some reason :-( */
CONST_ID(require, "require");
while (list && RARRAY_LEN(list) > 0) {
VALUE feature = rb_ary_shift(list);
rb_funcall2(rb_vm_top_self(), require, 1, &feature);

View File

@ -2632,7 +2632,7 @@ rb_str_upto(int argc, VALUE *argv, VALUE beg)
rb_scan_args(argc, argv, "11", &end, &exclusive);
excl = RTEST(exclusive);
succ = rb_intern("succ");
CONST_ID(succ, "succ");
StringValue(end);
enc = rb_enc_check(beg, end);
if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1 &&
@ -6526,6 +6526,8 @@ rb_to_id(VALUE name)
void
Init_String(void)
{
#undef rb_intern
rb_cString = rb_define_class("String", rb_cObject);
rb_include_module(rb_cString, rb_mComparable);
rb_define_alloc_func(rb_cString, str_alloc);

View File

@ -3167,6 +3167,8 @@ ruby_suppress_tracing(VALUE (*func)(VALUE, int), VALUE arg, int always)
void
Init_Thread(void)
{
#undef rb_intern
VALUE cThGroup;
rb_define_singleton_method(rb_cThread, "new", thread_s_new, -1);

2
time.c
View File

@ -2338,6 +2338,8 @@ time_load(VALUE klass, VALUE str)
void
Init_Time(void)
{
#undef rb_intern
id_divmod = rb_intern("divmod");
id_mul = rb_intern("*");
id_submicro = rb_intern("submicro");

View File

@ -26,9 +26,9 @@ Init_var_tables(void)
{
rb_global_tbl = st_init_numtable();
rb_class_tbl = st_init_numtable();
autoload = rb_intern("__autoload__");
classpath = rb_intern("__classpath__");
tmp_classpath = rb_intern("__tmp_classpath__");
CONST_ID(autoload, "__autoload__");
CONST_ID(classpath, "__classpath__");
CONST_ID(tmp_classpath, "__tmp_classpath__");
}
struct fc_result {
@ -142,7 +142,9 @@ classname(VALUE klass)
if (!klass) klass = rb_cObject;
if (RCLASS_IV_TBL(klass)) {
if (!st_lookup(RCLASS_IV_TBL(klass), classpath, &path)) {
ID classid = rb_intern("__classid__");
ID classid;
CONST_ID(classid, "__classid__");
if (!st_lookup(RCLASS_IV_TBL(klass), classid, &path)) {
return find_class_path(klass);

14
vm.c
View File

@ -754,22 +754,22 @@ make_localjump_error(const char *mesg, VALUE value, int reason)
switch (reason) {
case TAG_BREAK:
id = rb_intern("break");
CONST_ID(id, "break");
break;
case TAG_REDO:
id = rb_intern("redo");
CONST_ID(id, "redo");
break;
case TAG_RETRY:
id = rb_intern("retry");
CONST_ID(id, "retry");
break;
case TAG_NEXT:
id = rb_intern("next");
CONST_ID(id, "next");
break;
case TAG_RETURN:
id = rb_intern("return");
CONST_ID(id, "return");
break;
default:
id = rb_intern("noreason");
CONST_ID(id, "noreason");
break;
}
rb_iv_set(exc, "@exit_value", value);
@ -1241,7 +1241,7 @@ rb_thread_method_id_and_class(rb_thread_t *th,
}
while (iseq) {
if (RUBY_VM_IFUNC_P(iseq)) {
if (idp) *idp = rb_intern("<ifunc>");
if (idp) CONST_ID(*idp, "<ifunc>");
if (klassp) *klassp = 0;
return 1;
}

View File

@ -425,18 +425,16 @@ debug_print_post(rb_thread_t *th, rb_control_frame_t *cfp
void
vm_analysis_insn(int insn)
{
static ID usage_hash;
static ID bigram_hash;
ID usage_hash;
ID bigram_hash;
static int prev_insn = -1;
VALUE uh;
VALUE ihash;
VALUE cv;
if (usage_hash == 0) {
usage_hash = rb_intern("USAGE_ANALYSIS_INSN");
bigram_hash = rb_intern("USAGE_ANALYSIS_INSN_BIGRAM");
}
CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
CONST_ID(bigram_hash, "USAGE_ANALYSIS_INSN_BIGRAM");
uh = rb_const_get(rb_cVM, usage_hash);
if ((ihash = rb_hash_aref(uh, INT2FIX(insn))) == Qnil) {
ihash = rb_hash_new();
@ -473,7 +471,7 @@ extern VALUE insn_operand_intern(int insn, int op_no, VALUE op,
void
vm_analysis_operand(int insn, int n, VALUE op)
{
static ID usage_hash;
ID usage_hash;
VALUE uh;
VALUE ihash;
@ -481,9 +479,7 @@ vm_analysis_operand(int insn, int n, VALUE op)
VALUE valstr;
VALUE cv;
if (usage_hash == 0) {
usage_hash = rb_intern("USAGE_ANALYSIS_INSN");
}
CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
uh = rb_const_get(rb_cVM, usage_hash);
if ((ihash = rb_hash_aref(uh, INT2FIX(insn))) == Qnil) {
@ -507,11 +503,11 @@ vm_analysis_operand(int insn, int n, VALUE op)
void
vm_analysis_register(int reg, int isset)
{
static ID usage_hash;
ID usage_hash;
VALUE uh;
VALUE rhash;
VALUE valstr;
char *regstrs[] = {
static const char regstrs[][5] = {
"pc", /* 0 */
"sp", /* 1 */
"cfp", /* 2 */
@ -520,7 +516,7 @@ vm_analysis_register(int reg, int isset)
"self", /* 5 */
"iseq", /* 6 */
};
char *getsetstr[] = {
static const char getsetstr[][4] = {
"get",
"set",
};
@ -528,12 +524,11 @@ vm_analysis_register(int reg, int isset)
VALUE cv;
if (usage_hash == 0) {
CONST_ID(usage_hash, "USAGE_ANALYSIS_REGS");
if (syms[0] == 0) {
char buff[0x10];
int i;
usage_hash = rb_intern("USAGE_ANALYSIS_REGS");
for (i = 0; i < sizeof(regstrs) / sizeof(regstrs[0]); i++) {
int j;
for (j = 0; j < 2; j++) {

View File

@ -1105,6 +1105,8 @@ obj_respond_to(int argc, VALUE *argv, VALUE obj)
void
Init_eval_method(void)
{
#undef rb_intern
rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1);
basic_respond_to = rb_method_node(rb_cObject, idRespond_to);
rb_register_mark_object((VALUE)basic_respond_to);