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

ruby-changes:35776

From: nobu <ko1@a...>
Date: Thu, 9 Oct 2014 16:54:00 +0900 (JST)
Subject: [ruby-changes:35776] nobu:r47858 (trunk): internal.h: optimize rb_ary_new_from_args

nobu	2014-10-09 16:53:54 +0900 (Thu, 09 Oct 2014)

  New Revision: 47858

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

  Log:
    internal.h: optimize rb_ary_new_from_args
    
    * internal.h (rb_ary_new_from_args): optimization by expanding
      arguments in caller to get rid of va_list if possible.

  Modified files:
    trunk/array.c
    trunk/internal.h
Index: array.c
===================================================================
--- array.c	(revision 47857)
+++ array.c	(revision 47858)
@@ -499,7 +499,7 @@ rb_ary_new(void) https://github.com/ruby/ruby/blob/trunk/array.c#L499
 }
 
 VALUE
-rb_ary_new_from_args(long n, ...)
+(rb_ary_new_from_args)(long n, ...)
 {
     va_list ar;
     VALUE ary;
Index: internal.h
===================================================================
--- internal.h	(revision 47857)
+++ internal.h	(revision 47858)
@@ -63,7 +63,8 @@ extern "C" { https://github.com/ruby/ruby/blob/trunk/internal.h#L63
 
 #define numberof(array) ((int)(sizeof(array) / sizeof((array)[0])))
 
-#define STATIC_ASSERT(name, expr) typedef int static_assert_##name##_check[1 - 2*!(expr)]
+#define STATIC_ASSERT_TYPE(name) static_assert_##name##_check
+#define STATIC_ASSERT(name, expr) typedef int STATIC_ASSERT_TYPE(name)[1 - 2*!(expr)]
 
 #define GCC_VERSION_SINCE(major, minor, patchlevel) \
   (defined(__GNUC__) && !defined(__INTEL_COMPILER) && \
@@ -509,6 +510,17 @@ VALUE rb_ary_last(int, const VALUE *, VA https://github.com/ruby/ruby/blob/trunk/internal.h#L510
 void rb_ary_set_len(VALUE, long);
 void rb_ary_delete_same(VALUE, VALUE);
 VALUE rb_ary_tmp_new_fill(long capa);
+#ifdef __GNUC__
+#define rb_ary_new_from_args(n, ...) \
+    __extension__ ({ \
+	const VALUE args_to_new_ary[] = {__VA_ARGS__}; \
+	if (__builtin_constant_p(n)) { \
+	    STATIC_ASSERT(rb_ary_new_from_args, numberof(args_to_new_ary) == (n)); \
+	    (void)sizeof(STATIC_ASSERT_TYPE(rb_ary_new_from_args)); /* suppress warnings by gcc 4.8 or later */ \
+	} \
+	rb_ary_new_from_values(numberof(args_to_new_ary), args_to_new_ary); \
+    })
+#endif
 
 /* bignum.c */
 VALUE rb_big_fdiv(VALUE x, VALUE y);

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

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