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

ruby-changes:54171

From: ko1 <ko1@a...>
Date: Fri, 14 Dec 2018 11:27:27 +0900 (JST)
Subject: [ruby-changes:54171] ko1:r66392 (trunk): separte NULL and EMPTY check.

ko1	2018-12-14 11:27:23 +0900 (Fri, 14 Dec 2018)

  New Revision: 66392

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

  Log:
    separte NULL and EMPTY check.
    
    * hash.c: separate NULL and EMPTY check functions.
      `RHASH_TABLE_EMPTY` function checks NULL table or not,
      but it should be named "NULL_P".
      Introduce `RHASH_TABLE_EMPTY_P` function to check size == 0.
      There are cases that hash has table data even if data is not NULL
      (in case removed after inserted elements).

  Modified files:
    trunk/hash.c
Index: hash.c
===================================================================
--- hash.c	(revision 66391)
+++ hash.c	(revision 66392)
@@ -446,7 +446,7 @@ hash_verify_(VALUE hash, const char *fil https://github.com/ruby/ruby/blob/trunk/hash.c#L446
 #endif
 
 static inline int
-RHASH_TABLE_EMPTY(VALUE hash)
+RHASH_TABLE_NULL_P(VALUE hash)
 {
     if (RHASH(hash)->as.ar == NULL) {
         HASH_ASSERT(RHASH_AR_TABLE_P(hash));
@@ -457,6 +457,12 @@ RHASH_TABLE_EMPTY(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L457
     }
 }
 
+static inline int
+RHASH_TABLE_EMPTY_P(VALUE hash)
+{
+    return RHASH_SIZE(hash) == 0;
+}
+
 MJIT_FUNC_EXPORTED int
 rb_hash_ar_table_p(VALUE hash)
 {
@@ -661,7 +667,7 @@ ar_force_convert_table(VALUE hash, const https://github.com/ruby/ruby/blob/trunk/hash.c#L667
 static ar_table *
 hash_ar_table(VALUE hash)
 {
-    if (RHASH_TABLE_EMPTY(hash)) {
+    if (RHASH_TABLE_NULL_P(hash)) {
         ar_alloc_table(hash);
     }
     return RHASH_AR_TABLE(hash);
@@ -1214,7 +1220,7 @@ rb_hash_foreach(VALUE hash, int (*func)( https://github.com/ruby/ruby/blob/trunk/hash.c#L1220
 {
     struct hash_foreach_arg arg;
 
-    if (RHASH_TABLE_EMPTY(hash))
+    if (RHASH_TABLE_EMPTY_P(hash))
         return;
     RHASH_ITER_LEV(hash)++;
     arg.hash = hash;
@@ -2176,7 +2182,7 @@ rb_hash_delete_if(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L2182
 {
     RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
     rb_hash_modify_check(hash);
-    if (!RHASH_TABLE_EMPTY(hash)) {
+    if (!RHASH_TABLE_EMPTY_P(hash)) {
         rb_hash_foreach(hash, delete_if_i, hash);
     }
     return hash;
@@ -2424,7 +2430,7 @@ rb_hash_keep_if(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L2430
 {
     RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
     rb_hash_modify_check(hash);
-    if (!RHASH_TABLE_EMPTY(hash)) {
+    if (!RHASH_TABLE_EMPTY_P(hash)) {
         rb_hash_foreach(hash, keep_if_i, hash);
     }
     return hash;
@@ -2537,7 +2543,7 @@ rb_hash_aset(VALUE hash, VALUE key, VALU https://github.com/ruby/ruby/blob/trunk/hash.c#L2543
 
     rb_hash_modify(hash);
 
-    if (RHASH_TABLE_EMPTY(hash)) {
+    if (RHASH_TABLE_NULL_P(hash)) {
 	if (iter_lev > 0) no_new_key();
         ar_alloc_table(hash);
     }
@@ -2859,7 +2865,7 @@ rb_hash_transform_keys_bang(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L2865
 {
     RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
     rb_hash_modify_check(hash);
-    if (!RHASH_TABLE_EMPTY(hash)) {
+    if (!RHASH_TABLE_EMPTY_P(hash)) {
         long i;
         VALUE pairs = rb_hash_flatten(0, NULL, hash);
         rb_hash_clear(hash);
@@ -2933,7 +2939,7 @@ rb_hash_transform_values_bang(VALUE hash https://github.com/ruby/ruby/blob/trunk/hash.c#L2939
 {
     RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
     rb_hash_modify_check(hash);
-    if (!RHASH_TABLE_EMPTY(hash))
+    if (!RHASH_TABLE_EMPTY_P(hash))
         rb_hash_foreach(hash, transform_values_i, hash);
     return hash;
 }
@@ -3325,13 +3331,15 @@ hash_equal(VALUE hash1, VALUE hash2, int https://github.com/ruby/ruby/blob/trunk/hash.c#L3331
     }
     if (RHASH_SIZE(hash1) != RHASH_SIZE(hash2))
 	return Qfalse;
-    if (!RHASH_TABLE_EMPTY(hash1) && !RHASH_TABLE_EMPTY(hash2)) {
-        if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2))
+    if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
+        if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
             return Qfalse;
-
-        data.hash = hash2;
-        data.eql = eql;
-        return rb_exec_recursive_paired(recursive_eql, hash1, hash2, (VALUE)&data);
+        }
+        else {
+            data.hash = hash2;
+            data.eql = eql;
+            return rb_exec_recursive_paired(recursive_eql, hash1, hash2, (VALUE)&data);
+        }
     }
 
 #if 0
@@ -4293,25 +4301,27 @@ ar_bulk_insert(VALUE hash, long argc, co https://github.com/ruby/ruby/blob/trunk/hash.c#L4301
 MJIT_FUNC_EXPORTED void
 rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash)
 {
-    st_index_t size;
-
     HASH_ASSERT(argc % 2 == 0);
-    if (! argc)
-        return;
-    size = argc / 2;
-    if (RHASH_TABLE_EMPTY(hash)) {
-        if (size <= RHASH_AR_TABLE_MAX_SIZE)
-            hash_ar_table(hash);
-        else
-            RHASH_TBL_RAW(hash);
-    }
-    if (RHASH_AR_TABLE_P(hash) &&
-        (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
-        ar_bulk_insert(hash, argc, argv);
-        return;
-    }
+    if (argc > 0) {
+        st_index_t size = argc / 2;
 
-    rb_hash_bulk_insert_into_st_table(argc, argv, hash);
+        if (RHASH_TABLE_NULL_P(hash)) {
+            if (size <= RHASH_AR_TABLE_MAX_SIZE) {
+                hash_ar_table(hash);
+            }
+            else {
+                RHASH_TBL_RAW(hash);
+            }
+        }
+
+        if (RHASH_AR_TABLE_P(hash) &&
+            (RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
+            ar_bulk_insert(hash, argc, argv);
+        }
+        else {
+            rb_hash_bulk_insert_into_st_table(argc, argv, hash);
+        }
+    }
 }
 
 static int path_tainted = -1;

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

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