summaryrefslogtreecommitdiff
path: root/hash.c
AgeCommit message (Collapse)Author
2017-10-26common conversion functionsnobu
* array.c (rb_to_array_type): make public to share common code internally. * hash.c (rb_to_hash_type): make public to share common code internally. * symbol.c (rb_to_symbol_type): make public to share common code internally. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60438 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-22Update doc of Hash#slice [ci skip]kazu
- Add arguments to call-seq - Add sample of multiple keys - Add sample: hash does not contain key git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60353 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-22hash.c: optimize Hash#compare_by_identityglass
hash.c (rb_hash_compare_by_id): avoid unnecessary allocation of st_table. formerly, st_table created in rb_hash_modify() was not used and replaced immediately. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60337 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-22Add arity check into Hash#flattenglass
* hash.c (rb_hash_flatten): add arity check git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60335 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-21Added sample code of merge! method in hash.c.hsbt
https://github.com/ruby/ruby/pull/1652 Patch by @selmertsx [fix GH-1652] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60273 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-21hash.c: Add Hash#sliceglass
* hash.c (rb_hash_slice): add Hash#slice [Feature #8499] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60229 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-02Revert "vm_eval.c: add rb_yield_assoc_or_values()"glass
This reverts commit r60095 to prevent performance degradation. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60098 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-02vm_eval.c: add rb_yield_assoc_or_values()glass
The new function rb_yield_assoc_or_values() will reduce branching. * vm_eval.c: add rb_yield_assoc_or_values() * internal.h: ditto * hash.c: use rb_yield_assoc_or_values() * struct.c: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60095 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-01use rb_hash_new_with_size()glass
* hash.c: use rb_hash_new_with_size(). * marshal.c: ditto * struct.c: ditto * vm_args.c: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60085 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-30hash.c: remove special treatments on deletionglass
st.c was improved in r56650 that it permits deletion during iteration. In this commit, special treatments for previous implementation are removed. * hash.c: don't use *_check and *_safe functions in st.c * internal.h: remove HASH_DELETED flag git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60076 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-18error.c: KeyError#receiver and KeyError#keynobu
* error.c: new method KeyError#receiver and KeyError#key. [Feature #12063] * hash.c: make KeyError object with receiver and key. * sprintf.c: ditto. Author: ksss <co000ri@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59955 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-05add rb_hash_new_with_size()shyouhei
Sometimes, size of a hash can be calcluated a priori. By providing such info to the constructor we can avoid unnecessary internal re- allocations. This can boost for instance creation of hash literals. [Bug #13861] Signed-off-by: Urabe, Shyouhei <shyouhei@ruby-lang.org> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59744 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-02Update Hash#compact! documentation [ci skip]nobu
* hash.c (rb_hash_compact_bang): [DOC] update the case if no changes were made. [ruby-core:82591] [Bug #13855] [Fix GH-1692] Author: Lucas Buchala <lucasbuchala@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59719 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-28[DOC] Fix typo in rdoc of `transform_values!` [ci skip]kazu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59674 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-30hash.c: refactor env_enc_str_newnobu
* hash.c (env_enc_str_new): convert to the expected encoding without intermediate string, and set econv flags if default internal encoding is set too. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59449 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-19revert r59359, r59356, r59355, r59354normal
These caused numerous CI failures I haven't been able to reproduce [ruby-core:82102] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59364 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-18hash: keep fstrings of tainted strings for string keysnormal
The same hash keys may be loaded from tainted data sources frequently (e.g. parsing headers from socket or loading YAML data from a file). If a non-tainted fstring already exists (because the application expects the hash key), cache and deduplicate the tainted version in the new tainted_frozen_strings table. For non-embedded strings, this also allows sharing with the underlying malloc-ed data. * vm_core.h (rb_vm_struct): add tainted_frozen_strings * vm.c (ruby_vm_destruct): free tainted_frozen_strings (Init_vm_objects): initialize tainted_frozen_strings (rb_vm_tfstring_table): accessor for tainted_frozen_strings * internal.h: declare rb_fstring_existing, rb_vm_tfstring_table * hash.c (fstring_existing_str): remove (moved to string.c) (hash_aset_str): use rb_fstring_existing * string.c (rb_fstring_existing): new, based on fstring_existing_str (tainted_fstr_update): new (rb_fstring_existing0): new, based on fstring_existing_str (rb_tainted_fstring_existing): new, special case for tainted strings (rb_str_free): delete from tainted_frozen_strings table * test/ruby/test_optimization.rb (test_hash_reuse_fstring): new test [ruby-core:82012] [Bug #13737] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59354 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-14hash.c: fix possible crash in Hash#transform_keys!rhe
Fix up r59328. It is possible that the given block abuses ObjectSpace.each_object to shrink the temporary array. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59334 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-14hash.c: Add Hash#transform_keys and Hash#transform_keys!mrkn
* hash.c (transform_keys_i, rb_hash_transform_keys): Add Hash#transform_keys. [Feature #13583] [ruby-core:81290] * hash.c (rb_hash_transform_keys_bang): Add Hash#transform_keys!. [Feature #13583] [ruby-core:81290] * test/ruby/test_hash.rb: Add tests for above changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59328 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-11tainted string should be tainted.ko1
* hash.c (hash_aset_str): create frozen string for tainted objects. (should not use fsting table on this case). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59310 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-10hash.c: prefer value cast to pointer castnobu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59306 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-09Hash#[]= deduplicates string keys if (and only if) fstring existsnormal
In typical applications, hash entries are read after being written to. Blindly writing to hashes which are never read makes little sense. So, for any hash which is read from, an fstring entry for the key should already exist for the key. We no longer blindly create fstrings if the code is blindly setting random hash keys, preventing the performance regression in the reverted r43870. Regarding <https://bugs.ruby-lang.org/issues/9188>, this has a minimum impact on the bm_so_k_nucleotide where hash keys are set and not reused, performance is within 1-2% of existing cases. * hash.c: #include gc.h for rb_objspace_garbage_object_p (hash_aset_str): do read-only check of fstring table and reuse fstring if it exists and is still alive (not garbage) [ruby-core:81942] [Feature #13725] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59304 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-31Improve performance of implicit type conversionwatson1978
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
2017-05-25hash.c: docs for Hash#transform_valuesstomar
* 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
2017-05-22hash.c: [DOC] fix docs for Hash#transform_values!rhe
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
2017-05-21Add missing word in transform_values methods descriptionkazu
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
2017-05-20Improve Hash#merge performancewatson1978
* 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
2017-05-12Encoding.default_internal should affect ENV on Windows like other platformsusa
* 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
2017-04-27refactor newhash (revision 58463 another try) [fix GH-1600]shyouhei
* 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
2017-04-24revert newhash refactoringshyouhei
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
2017-04-23mark Hash keys correctly.ko1
* 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
2017-04-22adjust indentnobu
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58446 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-21insert WB correctly.ko1
* 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
2017-04-21mark created frozen strings.ko1
* 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
2017-04-21typo fix (sorry!)shyouhei
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58428 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-21refactor hash literalshyouhei
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
2017-03-01hash.c: [DOC] fix book title in examplestomar
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57753 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-27Fix doc for Hash#dig and Struct#digstomar
* 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
2017-02-21backward.h: 2.2 deprecated featuresnobu
* 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
2017-01-22Hash#fetch: fix grammar in documentation.kazu
[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
2017-01-19ENV#fetch: fix documentation of raised exceptionnobu
[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
2017-01-07fix optimization for hash aset/aref with fstringeregon
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
2016-12-24hash.c: split long long literalsnobu
* 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
2016-12-10Updated documentation examples for Hash#value?naruse
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
2016-12-07hash.c: hash should be longnobu
* 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
2016-12-06switching hash removalnobu
* 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
2016-12-06remove unnecessary variablenobu
* 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
2016-12-03Cleanup ruby method definitions in hash.ckazu
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
2016-11-07add castnobu
* 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
2016-11-07 Introduce table improvement by Vladimir Makarov <vmakarov@redhat.com>.ko1
[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