[前][次][番号順一覧][スレッド一覧]

ruby-changes:52847

From: nobu <ko1@a...>
Date: Sat, 13 Oct 2018 18:59:28 +0900 (JST)
Subject: [ruby-changes:52847] nobu:r65059 (trunk): Prefer `rb_fstring_lit` over `rb_fstring_cstr`

nobu	2018-10-13 18:59:22 +0900 (Sat, 13 Oct 2018)

  New Revision: 65059

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65059

  Log:
    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.

  Modified files:
    trunk/compile.c
    trunk/error.c
    trunk/eval_error.c
    trunk/file.c
    trunk/io.c
    trunk/iseq.c
    trunk/load.c
    trunk/proc.c
    trunk/strftime.c
    trunk/string.c
    trunk/time.c
    trunk/vm.c
    trunk/vm_eval.c
Index: string.c
===================================================================
--- string.c	(revision 65058)
+++ string.c	(revision 65059)
@@ -4294,7 +4294,7 @@ rb_str_upto_each(VALUE beg, VALUE end, i https://github.com/ruby/ruby/blob/trunk/string.c#L4294
 	}
 	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 https://github.com/ruby/ruby/blob/trunk/string.c#L4337
     /* 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)) {
Index: io.c
===================================================================
--- io.c	(revision 65058)
+++ io.c	(revision 65059)
@@ -13034,7 +13034,7 @@ Init_IO(void) https://github.com/ruby/ruby/blob/trunk/io.c#L13034
     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;
Index: error.c
===================================================================
--- error.c	(revision 65058)
+++ error.c	(revision 65059)
@@ -1840,7 +1840,7 @@ syntax_error_initialize(int argc, VALUE https://github.com/ruby/ruby/blob/trunk/error.c#L1840
 {
     VALUE mesg;
     if (argc == 0) {
-	mesg = rb_fstring_cstr("compile error");
+	mesg = rb_fstring_lit("compile error");
 	argc = 1;
 	argv = &mesg;
     }
Index: file.c
===================================================================
--- file.c	(revision 65058)
+++ file.c	(revision 65059)
@@ -6276,7 +6276,7 @@ Init_File(void) https://github.com/ruby/ruby/blob/trunk/file.c#L6276
     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 */
Index: compile.c
===================================================================
--- compile.c	(revision 65058)
+++ compile.c	(revision 65059)
@@ -7153,7 +7153,7 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK https://github.com/ruby/ruby/blob/trunk/compile.c#L7153
       }
       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 *real https://github.com/ruby/ruby/blob/trunk/compile.c#L8281
 	return line;
     }
     else {
-	*path = rb_fstring_cstr("<compiled>");
+	*path = rb_fstring_lit("<compiled>");
 	*realpath = *path;
 	return 1;
     }
Index: vm_eval.c
===================================================================
--- vm_eval.c	(revision 65058)
+++ vm_eval.c	(revision 65059)
@@ -644,7 +644,7 @@ rb_make_no_method_exception(VALUE exc, V https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L644
     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_contex https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L676
     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");
     }
 
     {
Index: time.c
===================================================================
--- time.c	(revision 65058)
+++ time.c	(revision 65059)
@@ -866,8 +866,6 @@ timegmw_noleapsecond(struct vtm *vtm) https://github.com/ruby/ruby/blob/trunk/time.c#L866
     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) https://github.com/ruby/ruby/blob/trunk/time.c#L875
     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, st https://github.com/ruby/ruby/blob/trunk/time.c#L992
     }
 
     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 *res https://github.com/ruby/ruby/blob/trunk/time.c#L1260
     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, https://github.com/ruby/ruby/blob/trunk/time.c#L1380
     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, https://github.com/ruby/ruby/blob/trunk/time.c#L1424
 
     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 https://github.com/ruby/ruby/blob/trunk/time.c#L2218
 
     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, st https://github.com/ruby/ruby/blob/trunk/time.c#L2835
     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) https://github.com/ruby/ruby/blob/trunk/time.c#L3719
 	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) https://github.com/ruby/ruby/blob/trunk/time.c#L4933
         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, VA https://github.com/ruby/ruby/blob/trunk/time.c#L5147
         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) {
Index: vm.c
===================================================================
--- vm.c	(revision 65058)
+++ vm.c	(revision 65059)
@@ -976,7 +976,7 @@ rb_binding_add_dynavars(VALUE bindval, r https://github.com/ruby/ruby/blob/trunk/vm.c#L976
 	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) https://github.com/ruby/ruby/blob/trunk/vm.c#L3098
     {
 	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;
 
Index: iseq.c
===================================================================
--- iseq.c	(revision 65058)
+++ iseq.c	(revision 65059)
@@ -668,7 +668,7 @@ rb_iseq_new_top(const rb_ast_body_t *ast https://github.com/ruby/ruby/blob/trunk/iseq.c#L668
 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, V https://github.com/ruby/ruby/blob/trunk/iseq.c#L894
     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, V https://github.com/ruby/ruby/blob/trunk/iseq.c#L1076
       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 *ar https://github.com/ruby/ruby/blob/trunk/iseq.c#L1139
 
     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 * https://github.com/ruby/ruby/blob/trunk/iseq.c#L1702
 	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;
Index: load.c
===================================================================
--- load.c	(revision 65058)
+++ load.c	(revision 65059)
@@ -604,7 +604,7 @@ rb_load_internal0(rb_execution_context_t https://github.com/ruby/ruby/blob/trunk/load.c#L604
 	    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);
 	}
Index: proc.c
===================================================================
--- proc.c	(revision 65058)
+++ proc.c	(revision 65059)
@@ -1643,7 +1643,7 @@ method_owner(VALUE obj) https://github.com/ruby/ruby/blob/trunk/proc.c#L1643
 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) https://github.com/ruby/ruby/blob/trunk/proc.c#L2819
 	    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) https://github.com/ruby/ruby/blob/trunk/proc.c#L2852
     }
     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;
     }
 
Index: strftime.c
===================================================================
--- strftime.c	(revision 65058)
+++ strftime.c	(revision 65059)
@@ -826,7 +826,7 @@ rb_strftime_with_timespec(VALUE ftime, c https://github.com/ruby/ruby/blob/trunk/strftime.c#L826
                                         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;
                                 }
Index: eval_error.c
===================================================================
--- eval_error.c	(revision 65058)
+++ eval_error.c	(revision 65059)
@@ -292,7 +292,7 @@ rb_ec_error_print(rb_execution_context_t https://github.com/ruby/ruby/blob/trunk/eval_error.c#L292
     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 na https://github.com/ruby/ruby/blob/trunk/eval_error.c#L320
     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") : \

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]