From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.7 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, T_RP_MATCHES_RCVD shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: spew@80x24.org Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 5CB9A205EF for ; Fri, 4 Dec 2015 00:48:08 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] introduce rb_autoload_str to replace rb_autoload Date: Fri, 4 Dec 2015 00:48:08 +0000 Message-Id: <20151204004808.3288-1-e@80x24.org> List-Id: rb_autoload_str may be safer by preventing premature GC. It can also be more efficient by passing a pre-frozen string that can be deduped using rb_fstring. Common autoload callers (e.g. rubygems, rdoc) already use string literals as the file argument. There seems to be no reason to expose rb_autoload_str to the public C API since autoload is not performance-critical. Applications may declare autoloads in Ruby code or via rb_funcall; so merely deprecate rb_autoload without exposing rb_autoload_str to new users. Running: valgrind -v ruby -rrdoc -rubygems -e exit shows a minor memory reduction (32-bit userspace) before: in use at exit: 1,600,621 bytes in 28,819 blocks total heap usage: 55,786 allocs, 26,967 frees, 6,693,790 bytes allocated after: in use at exit: 1,599,778 bytes in 28,789 blocks total heap usage: 55,739 allocs, 26,950 frees, 6,692,973 bytes allocated --- include/ruby/intern.h | 2 +- internal.h | 1 + load.c | 2 +- variable.c | 24 ++++++++++++++++++------ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 3fb1637..d43dc67 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -937,7 +937,7 @@ VALUE rb_path_to_class(VALUE); VALUE rb_path2class(const char*); void rb_name_class(VALUE, ID); VALUE rb_class_name(VALUE); -void rb_autoload(VALUE, ID, const char*); +DEPRECATED(void rb_autoload(VALUE, ID, const char*)); VALUE rb_autoload_load(VALUE, ID); VALUE rb_autoload_p(VALUE, ID); VALUE rb_f_trace_var(int, const VALUE*); diff --git a/internal.h b/internal.h index 1018d0b..df594c1 100644 --- a/internal.h +++ b/internal.h @@ -1205,6 +1205,7 @@ size_t rb_generic_ivar_memsize(VALUE); VALUE rb_search_class_path(VALUE); VALUE rb_attr_delete(VALUE, ID); VALUE rb_ivar_lookup(VALUE obj, ID id, VALUE undef); +void rb_autoload_str(VALUE mod, ID id, VALUE file); /* version.c */ extern const char ruby_engine[]; diff --git a/load.c b/load.c index 3f5e143..96b92fc 100644 --- a/load.c +++ b/load.c @@ -1099,7 +1099,7 @@ rb_mod_autoload(VALUE mod, VALUE sym, VALUE file) ID id = rb_to_id(sym); FilePathValue(file); - rb_autoload(mod, id, RSTRING_PTR(file)); + rb_autoload_str(mod, id, file); return Qnil; } diff --git a/variable.c b/variable.c index 18a70a0..f6dd3ee 100644 --- a/variable.c +++ b/variable.c @@ -1916,8 +1916,17 @@ static const rb_data_type_t autoload_data_i_type = { void rb_autoload(VALUE mod, ID id, const char *file) { + if (!file || !*file) { + rb_raise(rb_eArgError, "empty file name"); + } + rb_autoload_str(mod, id, rb_fstring_cstr(file)); +} + +void +rb_autoload_str(VALUE mod, ID id, VALUE file) +{ st_data_t av; - VALUE ad, fn; + VALUE ad; struct st_table *tbl; struct autoload_data_i *ele; rb_const_entry_t *ce; @@ -1926,7 +1935,9 @@ rb_autoload(VALUE mod, ID id, const char *file) rb_raise(rb_eNameError, "autoload must be constant name: %"PRIsVALUE"", QUOTE_ID(id)); } - if (!file || !*file) { + + Check_Type(file, T_STRING); + if (!RSTRING_LEN(file)) { rb_raise(rb_eArgError, "empty file name"); } @@ -1947,12 +1958,13 @@ rb_autoload(VALUE mod, ID id, const char *file) RB_OBJ_WRITTEN(mod, Qnil, av); DATA_PTR(av) = tbl = st_init_numtable(); } - fn = rb_str_new2(file); - FL_UNSET(fn, FL_TAINT); - OBJ_FREEZE(fn); ad = TypedData_Make_Struct(0, struct autoload_data_i, &autoload_data_i_type, ele); - ele->feature = fn; + if (OBJ_TAINTED(file)) { + file = rb_str_dup(file); + FL_UNSET(file, FL_TAINT); + } + ele->feature = rb_fstring(file); ele->safe_level = rb_safe_level(); ele->value = Qundef; ele->state = 0; -- EW