ruby-changes:31806
From: glass <ko1@a...>
Date: Thu, 28 Nov 2013 01:07:18 +0900 (JST)
Subject: [ruby-changes:31806] glass:r43885 (trunk): * st.c (st_keys): define st_keys(). it writes each key to buffer.
glass 2013-11-28 01:07:10 +0900 (Thu, 28 Nov 2013) New Revision: 43885 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43885 Log: * st.c (st_keys): define st_keys(). it writes each key to buffer. * hash.c (rb_hash_keys): use st_keys() for performance improvement if st_data_t and VALUE are compatible. * st.h: define macro ST_DATA_COMPATIBLE_P() to predicate whether st_data_t and passed type are compatible. * configure.in: check existence of builtin function to use in ST_DATA_COMPATIBLE_P(). Modified files: trunk/ChangeLog trunk/configure.in trunk/hash.c trunk/include/ruby/st.h trunk/st.c Index: include/ruby/st.h =================================================================== --- include/ruby/st.h (revision 43884) +++ include/ruby/st.h (revision 43885) @@ -59,6 +59,13 @@ struct st_hash_type { https://github.com/ruby/ruby/blob/trunk/include/ruby/st.h#L59 #define ST_INDEX_BITS (sizeof(st_index_t) * CHAR_BIT) +#if defined(HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR) && defined(HAVE_BUILTIN___BUILTIN_TYPES_COMPATIBLE_P) +# define ST_DATA_COMPATIBLE_P(type) \ + __builtin_choose_expr(__builtin_types_compatible_p(type, st_data_t), 1, 0) +#else +# define ST_DATA_COMPATIBLE_P(type) 0 +#endif + struct st_table { const struct st_hash_type *type; st_index_t num_bins; @@ -112,6 +119,7 @@ int st_update(st_table *table, st_data_t https://github.com/ruby/ruby/blob/trunk/include/ruby/st.h#L119 int st_foreach(st_table *, int (*)(ANYARGS), st_data_t); int st_foreach_check(st_table *, int (*)(ANYARGS), st_data_t, st_data_t); int st_reverse_foreach(st_table *, int (*)(ANYARGS), st_data_t); +st_index_t st_keys(st_table *table, st_data_t *keys, st_index_t size); void st_add_direct(st_table *, st_data_t, st_data_t); void st_free_table(st_table *); void st_cleanup_safe(st_table *, st_data_t); Index: configure.in =================================================================== --- configure.in (revision 43884) +++ configure.in (revision 43885) @@ -1937,6 +1937,8 @@ RUBY_CHECK_BUILTIN_FUNC(__builtin_bswap6 https://github.com/ruby/ruby/blob/trunk/configure.in#L1937 RUBY_CHECK_BUILTIN_FUNC(__builtin_clz, [__builtin_clz(0)]) RUBY_CHECK_BUILTIN_FUNC(__builtin_clzl, [__builtin_clzl(0)]) RUBY_CHECK_BUILTIN_FUNC(__builtin_clzll, [__builtin_clzll(0)]) +RUBY_CHECK_BUILTIN_FUNC(__builtin_choose_expr, [__builtin_choose_expr(0, 0, 0)]) +RUBY_CHECK_BUILTIN_FUNC(__builtin_types_compatible_p, [__builtin_types_compatible_p(int, int)]) # Some platform need -lrt for clock_gettime, but the other don't. if test x"$ac_cv_func_clock_gettime" != xyes; then Index: ChangeLog =================================================================== --- ChangeLog (revision 43884) +++ ChangeLog (revision 43885) @@ -1,3 +1,16 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Nov 28 00:36:52 2013 Masaki Matsushita <glass.saga@g...> + + * st.c (st_keys): define st_keys(). it writes each key to buffer. + + * hash.c (rb_hash_keys): use st_keys() for performance improvement + if st_data_t and VALUE are compatible. + + * st.h: define macro ST_DATA_COMPATIBLE_P() to predicate whether + st_data_t and passed type are compatible. + + * configure.in: check existence of builtin function to use in + ST_DATA_COMPATIBLE_P(). + Thu Nov 28 00:07:28 2013 Masaki Matsushita <glass.saga@g...> * ruby_atomic.h: remove duplicate definisions between ATOMIC_XXX Index: st.c =================================================================== --- st.c (revision 43884) +++ st.c (revision 43885) @@ -1091,6 +1091,35 @@ st_foreach(st_table *table, int (*func)( https://github.com/ruby/ruby/blob/trunk/st.c#L1091 return 0; } +st_index_t +st_keys(st_table *table, st_data_t *keys, st_index_t size) +{ + st_data_t key, never = (st_data_t)Qundef; + st_data_t *keys_start = keys; + + if (table->entries_packed) { + st_index_t i; + + if (size > table->real_entries) size = table->real_entries; + for (i = 0; i < size; i++) { + key = PKEY(table, i); + if (key == never) continue; + *keys++ = key; + } + } + else { + st_table_entry *ptr = table->head; + st_data_t *keys_end = keys + size; + while (ptr && keys < keys_end) { + key = ptr->key; + if (key != never) *keys++ = key; + ptr = ptr->fore; + } + } + + return keys - keys_start; +} + #if 0 /* unused right now */ int st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) Index: hash.c =================================================================== --- hash.c (revision 43884) +++ hash.c (revision 43885) @@ -1702,12 +1702,26 @@ keys_i(VALUE key, VALUE value, VALUE ary https://github.com/ruby/ruby/blob/trunk/hash.c#L1702 VALUE rb_hash_keys(VALUE hash) { - VALUE ary; + VALUE keys; + int size = RHASH_SIZE(hash); - ary = rb_ary_new_capa(RHASH_SIZE(hash)); - rb_hash_foreach(hash, keys_i, ary); + keys = rb_ary_new_capa(size); + if (size == 0) return keys; - return ary; + if (ST_DATA_COMPATIBLE_P(VALUE)) { + st_table *table = RHASH(hash)->ntbl; + + if (OBJ_PROMOTED(keys)) rb_gc_writebarrier_remember_promoted(keys); + RARRAY_PTR_USE(keys, ptr, { + size = st_keys(table, ptr, size); + }); + rb_ary_set_len(keys, size); + } + else { + rb_hash_foreach(hash, keys_i, keys); + } + + return keys; } static int -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/