1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Prefer rb_fstring_lit over rb_fstring_cstr

The former states explicitly that the argument must be a literal,
and can optimize away `strlen` on all compilers.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65059 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-10-13 09:59:22 +00:00
parent 83a01e6f52
commit fa8b08b424
13 changed files with 37 additions and 39 deletions

View file

@ -7153,7 +7153,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
}
case NODE_SCLASS:{
ID singletonclass;
const rb_iseq_t *singleton_class = NEW_ISEQ(node->nd_body, rb_fstring_cstr("singleton class"),
const rb_iseq_t *singleton_class = NEW_ISEQ(node->nd_body, rb_fstring_lit("singleton class"),
ISEQ_TYPE_CLASS, line);
CHECK(COMPILE(ret, "sclass#recv", node->nd_recv));
@ -8281,7 +8281,7 @@ caller_location(VALUE *path, VALUE *realpath)
return line;
}
else {
*path = rb_fstring_cstr("<compiled>");
*path = rb_fstring_lit("<compiled>");
*realpath = *path;
return 1;
}

View file

@ -1840,7 +1840,7 @@ syntax_error_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE mesg;
if (argc == 0) {
mesg = rb_fstring_cstr("compile error");
mesg = rb_fstring_lit("compile error");
argc = 1;
argv = &mesg;
}

View file

@ -292,7 +292,7 @@ rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo)
rb_ec_raised_set(ec, raised_flag);
}
#define undef_mesg_for(v, k) rb_fstring_cstr("undefined"v" method `%1$s' for "k" `%2$s'")
#define undef_mesg_for(v, k) rb_fstring_lit("undefined"v" method `%1$s' for "k" `%2$s'")
#define undef_mesg(v) ( \
is_mod ? \
undef_mesg_for(v, "module") : \
@ -320,7 +320,7 @@ rb_print_undef_str(VALUE klass, VALUE name)
rb_name_err_raise_str(undef_mesg(""), klass, name);
}
#define inaccessible_mesg_for(v, k) rb_fstring_cstr("method `%1$s' for "k" `%2$s' is "v)
#define inaccessible_mesg_for(v, k) rb_fstring_lit("method `%1$s' for "k" `%2$s' is "v)
#define inaccessible_mesg(v) ( \
is_mod ? \
inaccessible_mesg_for(v, "module") : \

2
file.c
View file

@ -6276,7 +6276,7 @@ Init_File(void)
rb_define_singleton_method(rb_cFile, "extname", rb_file_s_extname, 1);
rb_define_singleton_method(rb_cFile, "path", rb_file_s_path, 1);
separator = rb_fstring_cstr("/");
separator = rb_fstring_lit("/");
/* separates directory parts in path */
rb_define_const(rb_cFile, "Separator", separator);
/* separates directory parts in path */

2
io.c
View file

@ -13034,7 +13034,7 @@ Init_IO(void)
rb_output_fs = Qnil;
rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
rb_default_rs = rb_fstring_cstr("\n"); /* avoid modifying RS_default */
rb_default_rs = rb_fstring_lit("\n"); /* avoid modifying RS_default */
rb_gc_register_mark_object(rb_default_rs);
rb_rs = rb_default_rs;
rb_output_rs = Qnil;

12
iseq.c
View file

@ -668,7 +668,7 @@ rb_iseq_new_top(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath
rb_iseq_t *
rb_iseq_new_main(const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_iseq_t *parent)
{
return rb_iseq_new_with_opt(ast, rb_fstring_cstr("<main>"),
return rb_iseq_new_with_opt(ast, rb_fstring_lit("<main>"),
path, realpath, INT2FIX(0),
parent, ISEQ_TYPE_MAIN, &COMPILE_OPTION_DEFAULT);
}
@ -894,7 +894,7 @@ rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, c
else {
INITIALIZED VALUE label = parent ?
parent->body->location.label :
rb_fstring_cstr("<compiled>");
rb_fstring_lit("<compiled>");
iseq = rb_iseq_new_with_opt(&ast->body, label, file, realpath, line,
parent, type, &option);
rb_ast_dispose(ast);
@ -1076,7 +1076,7 @@ iseqw_s_compile(int argc, VALUE *argv, VALUE self)
case 2: file = argv[--i];
}
if (NIL_P(file)) file = rb_fstring_cstr("<compiled>");
if (NIL_P(file)) file = rb_fstring_lit("<compiled>");
if (NIL_P(path)) path = file;
if (NIL_P(line)) line = INT2FIX(1);
@ -1139,7 +1139,7 @@ iseqw_s_compile_file(int argc, VALUE *argv, VALUE self)
make_compile_option(&option, opt);
ret = iseqw_new(rb_iseq_new_with_opt(&ast->body, rb_fstring_cstr("<main>"),
ret = iseqw_new(rb_iseq_new_with_opt(&ast->body, rb_fstring_lit("<main>"),
file,
rb_realpath_internal(Qnil, file, 1),
line, NULL, ISEQ_TYPE_TOP, &option));
@ -1702,10 +1702,10 @@ rb_insn_operand_intern(const rb_iseq_t *iseq,
if (insn == BIN(defined) && op_no == 0) {
enum defined_type deftype = (enum defined_type)op;
if (deftype == DEFINED_FUNC) {
ret = rb_fstring_cstr("func"); break;
ret = rb_fstring_lit("func"); break;
}
if (deftype == DEFINED_REF) {
ret = rb_fstring_cstr("ref"); break;
ret = rb_fstring_lit("ref"); break;
}
ret = rb_iseq_defined_string(deftype);
if (ret) break;

2
load.c
View file

@ -604,7 +604,7 @@ rb_load_internal0(rb_execution_context_t *ec, VALUE fname, int wrap)
VALUE parser = rb_parser_new();
rb_parser_set_context(parser, NULL, FALSE);
ast = (rb_ast_t *)rb_parser_load_file(parser, fname);
iseq = rb_iseq_new_top(&ast->body, rb_fstring_cstr("<top (required)>"),
iseq = rb_iseq_new_top(&ast->body, rb_fstring_lit("<top (required)>"),
fname, rb_realpath_internal(Qnil, fname, 1), NULL);
rb_ast_dispose(ast);
}

6
proc.c
View file

@ -1643,7 +1643,7 @@ method_owner(VALUE obj)
void
rb_method_name_error(VALUE klass, VALUE str)
{
#define MSG(s) rb_fstring_cstr("undefined method `%1$s' for"s" `%2$s'")
#define MSG(s) rb_fstring_lit("undefined method `%1$s' for"s" `%2$s'")
VALUE c = klass;
VALUE s;
@ -2819,7 +2819,7 @@ proc_binding(VALUE self)
const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
if (IS_METHOD_PROC_IFUNC(ifunc)) {
VALUE method = (VALUE)ifunc->data;
VALUE name = rb_fstring_cstr("<empty_iseq>");
VALUE name = rb_fstring_lit("<empty_iseq>");
rb_iseq_t *empty;
binding_self = method_receiver(method);
iseq = rb_method_iseq(method);
@ -2852,7 +2852,7 @@ proc_binding(VALUE self)
}
else {
RB_OBJ_WRITE(bindval, &bind->pathobj,
rb_iseq_pathobj_new(rb_fstring_cstr("(binding)"), Qnil));
rb_iseq_pathobj_new(rb_fstring_lit("(binding)"), Qnil));
bind->first_lineno = 1;
}

View file

@ -826,7 +826,7 @@ rb_strftime_with_timespec(VALUE ftime, const char *format, size_t format_len,
args[0] = INT2FIX(precision);
args[1] = subsec;
result = rb_str_format(2, args,
rb_fstring_cstr("%0*d"));
rb_fstring_lit("%0*d"));
(void)strlcpy(s, StringValueCStr(result), endp-s);
s += precision;
}

View file

@ -4294,7 +4294,7 @@ rb_str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALU
}
else {
ID op = excl ? '<' : idLE;
VALUE args[2], fmt = rb_fstring_cstr("%.*d");
VALUE args[2], fmt = rb_fstring_lit("%.*d");
args[0] = INT2FIX(width);
while (rb_funcall(b, op, 1, e)) {
@ -4337,7 +4337,7 @@ rb_str_upto_endless_each(VALUE beg, int (*each)(VALUE, VALUE), VALUE arg)
/* both edges are all digits */
if (is_ascii_string(beg) && ISDIGIT(RSTRING_PTR(beg)[0]) &&
all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg))) {
VALUE b, args[2], fmt = rb_fstring_cstr("%.*d");
VALUE b, args[2], fmt = rb_fstring_lit("%.*d");
int width = RSTRING_LENINT(beg);
b = rb_str_to_inum(beg, 10, FALSE);
if (FIXNUM_P(b)) {

22
time.c
View file

@ -866,8 +866,6 @@ timegmw_noleapsecond(struct vtm *vtm)
return wret;
}
#define rb_fstring_usascii(str) rb_fstring_enc_cstr((str), rb_usascii_encoding())
static VALUE
zone_str(const char *zone)
{
@ -877,7 +875,7 @@ zone_str(const char *zone)
size_t len;
if (zone == NULL) {
return rb_fstring_usascii("(NO-TIMEZONE-ABBREVIATION)");
return rb_fstring_lit("(NO-TIMEZONE-ABBREVIATION)");
}
for (p = zone; *p; p++)
@ -994,7 +992,7 @@ gmtimew_noleapsecond(wideval_t timew, struct vtm *vtm)
}
vtm->utc_offset = INT2FIX(0);
vtm->zone = rb_fstring_usascii("UTC");
vtm->zone = rb_fstring_lit("UTC");
}
static struct tm *
@ -1262,7 +1260,7 @@ gmtimew(wideval_t timew, struct vtm *result)
result->yday = tm.tm_yday+1;
result->isdst = tm.tm_isdst;
#if 0
result->zone = rb_fstring_usascii("UTC");
result->zone = rb_fstring_lit("UTC");
#endif
return result;
@ -1382,7 +1380,7 @@ guess_local_offset(struct vtm *vtm_utc, int *isdst_ret, VALUE *zone_ret)
if (lt(vtm_utc->year, INT2FIX(1916))) {
VALUE off = INT2FIX(0);
int isdst = 0;
zone = rb_fstring_usascii("UTC");
zone = rb_fstring_lit("UTC");
# if defined(NEGATIVE_TIME_T)
# if SIZEOF_TIME_T <= 4
@ -1426,7 +1424,7 @@ guess_local_offset(struct vtm *vtm_utc, int *isdst_ret, VALUE *zone_ret)
timev = w2v(rb_time_unmagnify(timegmw(&vtm2)));
t = NUM2TIMET(timev);
zone = rb_fstring_usascii("UTC");
zone = rb_fstring_lit("UTC");
if (localtime_with_gmtoff_zone(&t, &tm, &gmtoff, &zone)) {
if (isdst_ret)
*isdst_ret = tm.tm_isdst;
@ -2220,7 +2218,7 @@ time_init_1(int argc, VALUE *argv, VALUE time)
vtm.wday = VTM_WDAY_INITVAL;
vtm.yday = 0;
vtm.zone = rb_fstring_usascii("");
vtm.zone = rb_fstring_lit("");
/* year mon mday hour min sec off */
rb_scan_args(argc, argv, "16", &v[0],&v[1],&v[2],&v[3],&v[4],&v[5],&v[6]);
@ -2837,7 +2835,7 @@ time_arg(int argc, const VALUE *argv, struct vtm *vtm)
vtm->wday = 0;
vtm->yday = 0;
vtm->isdst = 0;
vtm->zone = rb_fstring_usascii("");
vtm->zone = rb_fstring_lit("");
if (argc == 10) {
v[0] = argv[5];
@ -3721,7 +3719,7 @@ time_gmtime(VALUE time)
time_modify(time);
}
vtm.zone = rb_fstring_usascii("UTC");
vtm.zone = rb_fstring_lit("UTC");
GMTIMEW(tobj->timew, &vtm);
tobj->vtm = vtm;
@ -4935,7 +4933,7 @@ time_mload(VALUE time, VALUE str)
vtm.utc_offset = INT2FIX(0);
vtm.yday = vtm.wday = 0;
vtm.isdst = 0;
vtm.zone = rb_fstring_usascii("");
vtm.zone = rb_fstring_lit("");
usec = (long)(s & 0xfffff);
nsec = usec * 1000;
@ -5149,7 +5147,7 @@ rb_time_zone_abbreviation(VALUE zone, VALUE time)
goto found;
}
#endif
strftime_args[0] = rb_fstring_cstr("%Z");
strftime_args[0] = rb_fstring_lit("%Z");
strftime_args[1] = tm;
abbr = rb_check_funcall(zone, rb_intern("strftime"), 2, strftime_args);
if (abbr != Qundef) {

4
vm.c
View file

@ -976,7 +976,7 @@ rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const I
iseq = rb_iseq_new(&ast, base_iseq->body->location.label, path, realpath, base_iseq, ISEQ_TYPE_EVAL);
}
else {
VALUE tempstr = rb_fstring_cstr("<temp>");
VALUE tempstr = rb_fstring_lit("<temp>");
iseq = rb_iseq_new_top(&ast, tempstr, tempstr, tempstr, NULL);
}
tmp_node.nd_tbl = 0; /* reset table */
@ -3098,7 +3098,7 @@ Init_VM(void)
{
rb_vm_t *vm = ruby_current_vm_ptr;
rb_thread_t *th = GET_THREAD();
VALUE filename = rb_fstring_cstr("<main>");
VALUE filename = rb_fstring_lit("<main>");
const rb_iseq_t *iseq = rb_iseq_new(0, filename, filename, Qnil, 0, ISEQ_TYPE_TOP);
volatile VALUE th_self;

View file

@ -644,7 +644,7 @@ rb_make_no_method_exception(VALUE exc, VALUE format, VALUE obj,
VALUE name = argv[0];
if (!format) {
format = rb_fstring_cstr("undefined method `%s' for %s%s%s");
format = rb_fstring_lit("undefined method `%s' for %s%s%s");
}
if (exc == rb_eNoMethodError) {
VALUE args = rb_ary_new4(argc - 1, argv + 1);
@ -676,17 +676,17 @@ raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv, VA
stack_check(ec);
if (last_call_status & MISSING_PRIVATE) {
format = rb_fstring_cstr("private method `%s' called for %s%s%s");
format = rb_fstring_lit("private method `%s' called for %s%s%s");
}
else if (last_call_status & MISSING_PROTECTED) {
format = rb_fstring_cstr("protected method `%s' called for %s%s%s");
format = rb_fstring_lit("protected method `%s' called for %s%s%s");
}
else if (last_call_status & MISSING_VCALL) {
format = rb_fstring_cstr("undefined local variable or method `%s' for %s%s%s");
format = rb_fstring_lit("undefined local variable or method `%s' for %s%s%s");
exc = rb_eNameError;
}
else if (last_call_status & MISSING_SUPER) {
format = rb_fstring_cstr("super: no superclass method `%s' for %s%s%s");
format = rb_fstring_lit("super: no superclass method `%s' for %s%s%s");
}
{