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

ruby-changes:6845

From: nobu <ko1@a...>
Date: Tue, 5 Aug 2008 03:30:15 +0900 (JST)
Subject: [ruby-changes:6845] Ruby:r18360 (trunk): * array.c (rb_ary_sort_bang): respect overridden <=> for String and

nobu	2008-08-05 03:29:55 +0900 (Tue, 05 Aug 2008)

  New Revision: 18360

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

  Log:
    * array.c (rb_ary_sort_bang): respect overridden <=> for String and
      Fixnum.  [ruby-core:17708]
    
    * include/ruby/node.h (NOEX_BASIC): basic definition method flag.
    
    * include/ruby/intern.h, vm_method.c (rb_method_basic_definition_p):
      new function to check if the method is not redefined after the
      initialization.
    
    * vm_method.c (rb_obj_respond_to): use rb_method_basic_definition_p.

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/include/ruby/intern.h
    trunk/include/ruby/node.h
    trunk/vm_method.c

Index: array.c
===================================================================
--- array.c	(revision 18359)
+++ array.c	(revision 18360)
@@ -1452,10 +1452,32 @@
     return rb_ary_reverse(rb_ary_dup(ary));
 }
 
+struct ary_sort_data {
+    VALUE ary;
+    int opt_methods;
+    int opt_inited;
+};
+
+enum {
+    sort_opt_Fixnum,
+    sort_opt_String,
+    sort_optimizable_count
+};
+
+#define STRING_P(s) (TYPE(s) == T_STRING && CLASS_OF(s) == rb_cString)
+
+#define SORT_OPTIMIZABLE_BIT(type) (1U << TOKEN_PASTE(sort_opt_,type))
+#define SORT_OPTIMIZABLE(data, type) \
+    ((data->opt_inited & SORT_OPTIMIZABLE_BIT(type)) ? \
+     (data->opt_methods & SORT_OPTIMIZABLE_BIT(type)) : \
+     ((data->opt_inited |= SORT_OPTIMIZABLE_BIT(type)), \
+      rb_method_basic_definition_p(TOKEN_PASTE(rb_c,type), id_cmp) && \
+      (data->opt_methods |= SORT_OPTIMIZABLE_BIT(type))))
+
 static VALUE
-sort_reentered(VALUE *klass)
+sort_reentered(VALUE ary)
 {
-    if (*klass) {
+    if (RBASIC(ary)->klass) {
 	rb_raise(rb_eRuntimeError, "sort reentered");
     }
     return Qnil;
@@ -1464,35 +1486,37 @@
 static int
 sort_1(const void *ap, const void *bp, void *dummy)
 {
-    VALUE retval = sort_reentered(dummy);
+    struct ary_sort_data *data = dummy;
+    VALUE retval = sort_reentered(data->ary);
     VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
     int n;
 
     retval = rb_yield_values(2, a, b);
     n = rb_cmpint(retval, a, b);
-    sort_reentered(dummy);
+    sort_reentered(data->ary);
     return n;
 }
 
 static int
 sort_2(const void *ap, const void *bp, void *dummy)
 {
-    VALUE retval = sort_reentered(dummy);
+    struct ary_sort_data *data = dummy;
+    VALUE retval = sort_reentered(data->ary);
     VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
     int n;
 
-    if (FIXNUM_P(a) && FIXNUM_P(b)) {
+    if (FIXNUM_P(a) && FIXNUM_P(b) && SORT_OPTIMIZABLE(data, Fixnum)) {
 	if ((long)a > (long)b) return 1;
 	if ((long)a < (long)b) return -1;
 	return 0;
     }
-    if (TYPE(a) == T_STRING) {
-	if (TYPE(b) == T_STRING) return rb_str_cmp(a, b);
+    if (STRING_P(a) && STRING_P(b) && SORT_OPTIMIZABLE(data, String)) {
+	return rb_str_cmp(a, b);
     }
 
     retval = rb_funcall(a, id_cmp, 1, b);
     n = rb_cmpint(retval, a, b);
-    sort_reentered(dummy);
+    sort_reentered(data->ary);
 
     return n;
 }
@@ -1519,10 +1543,14 @@
     rb_ary_modify(ary);
     if (RARRAY_LEN(ary) > 1) {
 	VALUE tmp = ary_make_shared(ary);
+	struct ary_sort_data data;
 
 	RBASIC(tmp)->klass = 0;
+	data.ary = tmp;
+	data.opt_methods = 0;
+	data.opt_inited = 0;
 	ruby_qsort(RARRAY_PTR(tmp), RARRAY_LEN(tmp), sizeof(VALUE),
-		   rb_block_given_p()?sort_1:sort_2, &RBASIC(tmp)->klass);
+		   rb_block_given_p()?sort_1:sort_2, &data);
 	if (RARRAY(ary)->ptr != RARRAY(tmp)->ptr) {
 	    if (!ARY_SHARED_P(ary)) xfree(RARRAY(ary)->ptr);
 	    RARRAY(ary)->ptr = RARRAY(tmp)->ptr;
Index: include/ruby/intern.h
===================================================================
--- include/ruby/intern.h	(revision 18359)
+++ include/ruby/intern.h	(revision 18360)
@@ -250,6 +250,7 @@
 void rb_alias(VALUE, ID, ID);
 void rb_attr(VALUE,ID,int,int,int);
 int rb_method_boundp(VALUE, ID, int);
+int rb_method_basic_definition_p(VALUE, ID);
 VALUE rb_eval_cmd(VALUE, VALUE, int);
 int rb_obj_respond_to(VALUE, ID, int);
 int rb_respond_to(VALUE, ID);
Index: include/ruby/node.h
===================================================================
--- include/ruby/node.h	(revision 18359)
+++ include/ruby/node.h	(revision 18360)
@@ -463,7 +463,8 @@
 #define NOEX_NOSUPER   0x01
 #define NOEX_PRIVATE   0x02
 #define NOEX_PROTECTED 0x04
-#define NOEX_MASK      0x06 /* 1110 */
+#define NOEX_MASK      0x06 /* 0110 */
+#define NOEX_BASIC     0x08
 
 #define NOEX_UNDEF     NOEX_NOSUPER
 
@@ -472,7 +473,7 @@
 #define NOEX_VCALL     0x40
 
 #define NOEX_SAFE(n) (((n) >> 8) & 0x0F)
-#define NOEX_WITH(n, s) ((s << 8) | n)
+#define NOEX_WITH(n, s) ((s << 8) | (n) | (ruby_running ? 0 : NOEX_BASIC))
 #define NOEX_WITH_SAFE(n) NOEX_WITH(n, rb_safe_level())
 
 #define CALL_PUBLIC 0
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18359)
+++ ChangeLog	(revision 18360)
@@ -1,3 +1,16 @@
+Tue Aug  5 03:29:52 2008  Nobuyoshi Nakada  <nobu@r...>
+
+	* array.c (rb_ary_sort_bang): respect overridden <=> for String and
+	  Fixnum.  [ruby-core:17708]
+
+	* include/ruby/node.h (NOEX_BASIC): basic definition method flag.
+
+	* include/ruby/intern.h, vm_method.c (rb_method_basic_definition_p):
+	  new function to check if the method is not redefined after the
+	  initialization.
+
+	* vm_method.c (rb_obj_respond_to): use rb_method_basic_definition_p.
+
 Mon Aug  4 20:39:06 2008  Yuki Sonoda (Yugui)  <yugui@y...>
 
 	* Makefile.in (update-rubyspec): renamed the rubyspec directory
Index: vm_method.c
===================================================================
--- vm_method.c	(revision 18359)
+++ vm_method.c	(revision 18360)
@@ -1044,6 +1044,15 @@
     return module;
 }
 
+int
+rb_method_basic_definition_p(VALUE klass, ID id)
+{
+    NODE *node = rb_method_node(klass, id);
+    if (node && (node->nd_noex & NOEX_BASIC))
+	return 1;
+    return 0;
+}
+
 /*
  *  call-seq:
  *     obj.respond_to?(symbol, include_private=false) => true or false
@@ -1053,14 +1062,12 @@
  *  optional second parameter evaluates to +true+.
  */
 
-static NODE *basic_respond_to = 0;
-
 int
 rb_obj_respond_to(VALUE obj, ID id, int priv)
 {
     VALUE klass = CLASS_OF(obj);
 
-    if (rb_method_node(klass, idRespond_to) == basic_respond_to) {
+    if (rb_method_basic_definition_p(klass, idRespond_to)) {
 	return rb_method_boundp(klass, id, !priv);
     }
     else {
@@ -1108,8 +1115,6 @@
 #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);
 
     rb_define_private_method(rb_cModule, "remove_method", rb_mod_remove_method, -1);
     rb_define_private_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);

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

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