aboutsummaryrefslogtreecommitdiffstats
path: root/hash.c
Commit message (Collapse)AuthorAgeFilesLines
* Improve performance of implicit type conversionwatson19782017-05-311-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | To convert the object implicitly, it has had two parts in convert_type() which are 1. lookink up the method's id 2. calling the method Seems that strncmp() and strcmp() in convert_type() are slightly heavy to look up the method's id for type conversion. This patch will add and use internal APIs (rb_convert_type_with_id, rb_check_convert_type_with_id) to call the method without looking up the method's id when convert the object. Array#flatten -> 19 % up Array#+ -> 3 % up [ruby-dev:50024] [Bug #13341] [Fix GH-1537] ### Before Array#flatten 104.119k (± 1.1%) i/s - 525.690k in 5.049517s Array#+ 1.993M (± 1.8%) i/s - 10.010M in 5.024258s ### After Array#flatten 124.005k (± 1.0%) i/s - 624.240k in 5.034477s Array#+ 2.058M (± 4.8%) i/s - 10.302M in 5.019328s ### Test Code require 'benchmark/ips' class Foo def to_ary [1,2,3] end end Benchmark.ips do |x| ary = [] 100.times { |i| ary << i } array = [ary] x.report "Array#flatten" do |i| i.times { array.flatten } end x.report "Array#+" do |i| obj = Foo.new i.times { array + obj } end end git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58978 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: docs for Hash#transform_valuesstomar2017-05-251-2/+3
| | | | | | | * hash.c: [DOC] fix return value in call-seq of Hash#transform_values; other small fixes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: [DOC] fix docs for Hash#transform_values!rhe2017-05-221-3/+4
| | | | | | | Hash#transform_values! returns the receiver rather than a new Hash object. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58845 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Add missing word in transform_values methods descriptionkazu2017-05-211-2/+2
| | | | | | | | | | | Explicitly says that the methods return a new hash rather than just stating it return a new something we don't know. [ci skip] [Fix GH-1619] Author: Nicolas Cavigneaux <nico@bounga.org> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58830 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Improve Hash#merge performancewatson19782017-05-201-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * hash.c (rb_hash_merge): use rb_hash_dup() instead of rb_obj_dup() to duplicate Hash object. rb_hash_dup() is faster duplicating function for Hash object which got rid of Hash#initialize_dup method calling. Hash#merge will be faster around 60%. [ruby-dev:50026] [Bug #13343] [Fix GH-1533] ### Before user system total real Hash#merge 0.160000 0.020000 0.180000 ( 0.182357) ### After user system total real Hash#merge 0.110000 0.010000 0.120000 ( 0.114404) ### Test code require 'benchmark' Benchmark.bmbm do |x| hash1 = {} 100.times { |i| hash1[i.to_s] = i } hash2 = {} 100.times { |i| hash2[(i*2).to_s] = i*2 } x.report "Hash#merge" do 10000.times do hash1.merge(hash2) end end end git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58811 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Encoding.default_internal should affect ENV on Windows like other platformsusa2017-05-121-2/+9
| | | | | | | | * hash.c (env_str_transcode): call rb_external_str_with_enc() if default_internal is available. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58687 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* refactor newhash (revision 58463 another try) [fix GH-1600]shyouhei2017-04-271-7/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * st.c (rb_hash_bulk_insert): new API to bulk insert entries into a hash. Given arguments are first inserted into the table at once, then reindexed. This is faster than inserting things using rb_hash_aset() one by one. This arrangement (rb_ prefixed function placed in st.c) is unavoidable because it both touches table internal and write barrier at once. * internal.h: delcare the new function. * hash.c (rb_hash_s_create): use the new function. * vm.c (core_hash_merge): ditto. * insns.def (newhash): ditto. * test/ruby/test_hash.rb: more coverage on hash creation. * test/ruby/test_literal.rb: ditto. ----------------------------------------------------------- benchmark results: minimum results in each 7 measurements. Execution time (sec) name before after loop_whileloop2 0.136 0.137 vm2_bighash* 1.249 0.623 Speedup ratio: compare with the result of `before' (greater is better) name after loop_whileloop2 0.996 vm2_bighash* 2.004 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58492 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* revert newhash refactoringshyouhei2017-04-241-108/+62
| | | | | | | | We need to fix GC bug before merging this. Revert revisions 58452, 58435, 58434, 58428, 58427 in this order. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58463 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* mark Hash keys correctly.ko12017-04-231-6/+6
| | | | | | | | * hash.c (rb_hash_new_from_object): same as r58434. Newly created frozen objects are not referred from any roots/objects. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58452 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* adjust indentnobu2017-04-221-3/+3
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58446 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* insert WB correctly.ko12017-04-211-5/+9
| | | | | | | * hash.c (hash_insert_raw): should insert WB. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58435 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* mark created frozen strings.ko12017-04-211-3/+6
| | | | | | | | | | | * hash.c (rb_hash_new_from_values_with_klass): before this fix, only a st table are filled with passed values. However, newly created frozen strings are not marked correctly only reference from st table. This patch marks such created frozen strings by Hash object which refers to the st table. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58434 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* typo fix (sorry!)shyouhei2017-04-211-1/+1
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58428 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* refactor hash literalshyouhei2017-04-211-62/+101
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Same as rb_ary_tmp_new_from_values(), it reduces vm_exec_core binary size from 26,176 bytes to 26,080 bytes. But this time, also with a bit of optimizations: - Because we are allocating a new hash and no back references are introduced at all, we can safely skip write barriers. - Also, the iteration never recurs. We can avoid complicated function callbacks by using st_insert instead of st_update. ---- * hash.c (rb_hash_new_from_values): refactor extract the bulk insert into a function. * hash.c (rb_hash_new_from_object): also refactor. * hash.c (rb_hash_s_create): use the new functions. * insns.def (newhash): ditto. * vm.c (core_hash_from_ary): ditto. * iternal.h: export the new function. ----------------------------------------------------------- benchmark results: minimum results in each 7 measurements. Execution time (sec) name before after loop_whileloop2 0.135 0.134 vm2_bighash* 1.236 0.687 Speedup ratio: compare with the result of `before' (greater is better) name after loop_whileloop2 1.008 vm2_bighash* 1.798 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58427 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: [DOC] fix book title in examplestomar2017-03-011-1/+1
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57753 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Fix doc for Hash#dig and Struct#digstomar2017-02-271-6/+6
| | | | | | | | | | * hash.c (rb_hash_dig): [DOC] correct argument name in method description; fix formatting in examples. * struct.c (rb_struct_dig): ditto. [ruby-core:79221] [Bug #13148] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57735 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* backward.h: 2.2 deprecated featuresnobu2017-02-211-12/+0
| | | | | | | * include/ruby/backward.h (DECLARE_DEPRECATED_FEATURE): move features deprecated at 2.2. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57671 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Hash#fetch: fix grammar in documentation.kazu2017-01-221-3/+3
| | | | | | | [Fix GH-1515][ci skip] Author: Alyssa Ross <hi+services.github@alyssa.is> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57390 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* ENV#fetch: fix documentation of raised exceptionnobu2017-01-191-1/+1
| | | | | | | [Fix GH-1514] Author: Misty De Meo <mistydemeo@github.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57371 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* fix optimization for hash aset/aref with fstringeregon2017-01-071-3/+1
| | | | | | | | | | | Patch by Eric Wong [ruby-core:78797]. I don't like the idea of making insns.def any bigger to support a corner case, and "test_hash_aref_fstring_identity" shows how contrived this is. [ruby-core:78783] [Bug #12855] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57278 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: split long long literalsnobu2016-12-241-2/+2
| | | | | | | * hash.c (prime1, prime2): split long long literals for platforms where LL suffix is not supported, e.g., VC6. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57174 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Updated documentation examples for Hash#value?naruse2016-12-101-2/+2
| | | | | | | | | | The same code is used for has_value? and value?, but according to http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/43765 has_value is deprecated. Use the non-deprecated syntax in the examples. by Herwin Weststrate <herwin@snt.utwente.nl> fix https://github.com/ruby/ruby/pull/1491 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57048 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: hash should be longnobu2016-12-071-3/+3
| | | | | | | * hash.c (any_hash): should return `long', because ruby assumes the hash value of the object id of an object is `long'. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57012 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* switching hash removalnobu2016-12-061-32/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | * st.h (struct st_hash_type): Remove strong_hash. (struct st_table): Remove inside_rebuild_p and curr_hash. * st.c (do_hash): Use type->hash instead of curr_hash. (make_tab_empty): Remove setting up curr_hash. (st_init_table_with_size): Remove setting up inside_rebuild_p. (rebuild_table): Remove clearing inside_rebuild_p. (reset_entry_hashes, HIT_THRESHOULD_FOR_STRONG_HASH): Remove code recognizing a denial attack and switching to strong hash. * hash.c (rb_dbl_long_hash, rb_objid_hash, rb_ident_hash): Use rb_hash_start to randomize the hash. (str_seed): Remove. (any_hash): Remove strong_p and use always rb_str_hash for strings. (any_hash_weak, rb_any_hash_weak): Remove. (st_hash_type objhash): Remove rb_any_hash_weak. based on the patch by Vladimir N Makarov <vmakarov@redhat.com> at [ruby-core:78490]. [Bug #13002] * test/ruby/test_hash.rb (test_wrapper): objects other than special constants should be able to be wrapped. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56992 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* remove unnecessary variablenobu2016-12-061-6/+10
| | | | | | | | * st.c (do_hash): remove unnecessary variable and cast. * hash.c, numeric.c, st.c: adjust style and indent. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56991 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Cleanup ruby method definitions in hash.ckazu2016-12-031-102/+102
| | | | | | | | | | | Always add a space between a comma and the next element. These spaces were there sometimes, but not always. This keeps to code consistent. Patch by: Herwin Weststrate <herwin@snt.utwente.nl> [ruby-core:78297] [Misc #12977] [GH-1492] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56971 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* add castnobu2016-11-071-1/+1
| | | | | | * hash.c (rb_objid_hash): need to cast down. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56657 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* Introduce table improvement by Vladimir Makarov <vmakarov@redhat.com>.ko12016-11-071-31/+77
| | | | | | | | | | | | | | | | | | | | | | | [Feature #12142] See header of st.c for improvment details. You can see all of code history here: <https://github.com/vnmakarov/ruby/tree/hash_tables_with_open_addressing> This improvement is discussed at <https://bugs.ruby-lang.org/issues/12142> with many people, especially with Yura Sokolov. * st.c: improve st_table. * include/ruby/st.h: ditto. * internal.h, numeric.c, hash.c (rb_dbl_long_hash): extract a function. * ext/-test-/st/foreach/foreach.c: catch up this change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56650 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* [DOC] replace Fixnum with Integer [ci skip]nobu2016-10-261-1/+1
| | | | | | * numeric.c: [DOC] update document for Integer class. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56492 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: fix Hash#compact! return valuenobu2016-10-221-1/+4
| | | | | | | * hash.c (rb_hash_compact_bang): should return nil if no elements is deleted. [ruby-core:77709] [Bug #12863] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: add compact and compact! methodsnobu2016-10-131-0/+64
| | | | | | | | * hash.c (rb_hash_compact, rb_hash_compact_bang): Removes nil values from the original hash, to port Active Support behavior. [Feature #11818] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56414 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* * internal.h (ST2FIX): new macro to convert st_index_t to Fixnum.usa2016-10-041-2/+2
| | | | | | | | | | | | | | | | | | | | | | | a hash value of Object might be Bignum, but it causes many troubles expecially the Object is used as a key of a hash. so I've gave up to do so. * array.c (rb_ary_hash): use above macro. * bignum.c (rb_big_hash): ditto. * hash.c (rb_obj_hash, rb_hash_hash): ditto. * numeric.c (rb_dbl_hash): ditto. * proc.c (proc_hash): ditto. * re.c (rb_reg_hash, match_hash): ditto. * string.c (rb_str_hash_m): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56340 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* * hash.c (each_pair_i_fast): Fix compile error with old version ofngoto2016-09-161-1/+3
| | | | | | | fcc on Solaris 10. [Bug #12768] [ruby-dev:49808] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56171 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* * hash.c (each_pair_i_fast): use rb_yield_values2 to avoid var args.naruse2016-09-131-1/+2
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56158 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* replace fixnum by integer in documents.akr2016-09-081-3/+3
| | | | git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56102 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: map_v -> transform_valuesmrkn2016-09-081-19/+19
| | | | | | | | | | * hash.c (rb_hash_transform_values, rb_hash_transform_values_bang): Rename map_v to transform_values. [Feature #12512] [ruby-core:76095] * test/ruby/test_hash.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56099 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* util.h: POSIX-noncompliant setenvnobu2016-09-071-2/+0
| | | | | | | * include/ruby/util.h (setenv): remove POSIX-noncompliant definition with 2 arguments. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56091 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: implement Hash#map_v and Hash#map_v!mrkn2016-08-091-0/+67
| | | | | | | | | * hash.c (rb_hash_map_v, rb_hash_map_v_bang): impelement Hash#map_v and Hash#map_v! [Feature #12512] [ruby-core:76095] * test/ruby/test_hash.rb: add tests for above change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55847 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: env_name_newnobu2016-08-051-21/+32
| | | | | | | | | * hash.c (env_enc_str_new): make string for an environment variable name or value. * hash.c (env_name_new): make environment value string with the encoding for its name. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55819 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* * hasn.c (env_str_new): taint the string. get rid of a test failureusa2016-08-051-0/+1
| | | | | | | introduced at r55811. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55818 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: call w32_getenv pointernobu2016-08-051-8/+12
| | | | | | | * hash.c (w32_getenv): call rb_w32_getenv and rb_w32_ugetenv via this pointer without further comparisons. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55813 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: set encodingnobu2016-08-041-1/+1
| | | | | | | * hash.c (env_assoc): the encoding of the value should be the locale, as well as other methods, [], fetch, values, etc. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55811 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* rb_funcallvnobu2016-07-291-2/+2
| | | | | | | * *.c: rename rb_funcall2 to rb_funcallv, except for extensions which are/will be/may be gems. [Fix GH-1406] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55773 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: rb_hash_add_new_elementnobu2016-07-201-0/+24
| | | | | | | | | * hash.c (rb_hash_add_new_element): add new element or do nothing if it is contained already. * array.c (ary_add_hash, ary_add_hash_by): use rb_hash_add_new_element. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55707 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: ensure NUL-terminated for ENVnobu2016-06-101-1/+1
| | | | | | | | | * hash.c (get_env_cstr): ensure NUL-terminated. [ruby-dev:49655] [Bug #12475] * string.c (rb_str_fill_terminator): return the pointer to the NUL-terminated content. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55345 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* * cont.c, hash.c, random.c, win32/win32.c: cleanup some Win9x/ME/NT4usa2016-05-011-1/+1
| | | | | | | | support leftovers. [fix GH-1328] patched by @cremno git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54863 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: dry up codenobu2016-04-281-4/+2
| | | | | | | * hash.c (rb_hash_update_{block,func}_callback): dry up hash update callback code. [Fix GH-1338] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54810 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: init table with sizenobu2016-03-301-0/+3
| | | | | | | * hash.c (rb_hash_s_create): allocate internal table with the given size. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54435 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* * bignum.c (rb_big_hash): make it public function to be available inmrkn2016-03-181-0/+4
| | | | | | | | | | | | | other source files, and remove documentation comment for Bignum#hash. * bignum.c (Bignum#hash): remove its definition because it is unified with Object#hash. * include/ruby/intern.h (rb_big_hash): add a prototype declaration. * hash.c (any_hash): treat Bignum values directly. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54168 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* hash.c: COPY_DEFAULTnobu2016-03-091-8/+12
| | | | | | * hash.c (COPY_DEFAULT): new macro to copy the default value/proc. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54059 b2dd03c8-39d4-4d8f-98ff-823fe69b080e