summaryrefslogtreecommitdiff
path: root/spec/ruby/library/set
AgeCommit message (Collapse)Author
4 daysImplement Set as a core classJeremy Evans
Set has been an autoloaded standard library since Ruby 3.2. The standard library Set is less efficient than it could be, as it uses Hash for storage, which stores unnecessary values for each key. Implementation details: * Core Set uses a modified version of `st_table`, named `set_table`. than `s/st_/set_/`, the main difference is that the stored records do not have values, making them 1/3 smaller. `st_table_entry` stores `hash`, `key`, and `record` (value), while `set_table_entry` only stores `hash` and `key`. This results in large sets using ~33% less memory compared to stdlib Set. For small sets, core Set uses 12% more memory (160 byte object slot and 64 malloc bytes, while stdlib set uses 40 for Set and 160 for Hash). More memory is used because the set_table is embedded and 72 bytes in the object slot are currently wasted. Hopefully we can make this more efficient and have it stored in an 80 byte object slot in the future. * All methods are implemented as cfuncs, except the pretty_print methods, which were moved to `lib/pp.rb` (which is where the pretty_print methods for other core classes are defined). As is typical for core classes, internal calls call C functions and not Ruby methods. For example, to check if something is a Set, `rb_obj_is_kind_of` is used, instead of calling `is_a?(Set)` on the related object. * Almost all methods use the same algorithm that the pure-Ruby implementation used. The exception is when calling `Set#divide` with a block with 2-arity. The pure-Ruby method used tsort to implement this. I developed an algorithm that only allocates a single intermediate hash and does not need tsort. * The `flatten_merge` protected method is no longer necessary, so it is not implemented (it could be). * Similar to Hash/Array, subclasses of Set are no longer reflected in `inspect` output. * RDoc from stdlib Set was moved to core Set, with minor updates. This includes a comprehensive benchmark suite for all public Set methods. As you would expect, the native version is faster in the vast majority of cases, and multiple times faster in many cases. There are a few cases where it is significantly slower: * Set.new with no arguments (~1.6x) * Set#compare_by_identity for small sets (~1.3x) * Set#clone for small sets (~1.5x) * Set#dup for small sets (~1.7x) These are slower as Set does not currently use the AR table optimization that Hash does, so a new set_table is initialized for each call. I'm not sure it's worth the complexity to have an AR table-like optimization for small sets (for hashes it makes sense, as small hashes are used everywhere in Ruby). The rbs and repl_type_completor bundled gems will need updates to support core Set. The pull request marks them as allowed failures. This passes all set tests with no changes. The following specs needed modification: * Modifying frozen set error message (changed for the better) * `Set#divide` when passed a 2-arity block no longer yields the same object as both the first and second argument (this seems like an issue with the previous implementation). * Set-like objects that override `is_a?` such that `is_a?(Set)` return `true` are no longer treated as Set instances. * `Set.allocate.hash` is no longer the same as `nil.hash` * `Set#join` no longer calls `Set#to_a` (it calls the underlying C function). * `Set#flatten_merge` protected method is not implemented. Previously, `set.rb` added a `SortedSet` autoload, which loads `set/sorted_set.rb`. This replaces the `Set` autoload in `prelude.rb` with a `SortedSet` autoload, but I recommend removing it and `set/sorted_set.rb`. This moves `test/set/test_set.rb` to `test/ruby/test_set.rb`, reflecting that switch to a core class. This does not move the spec files, as I'm not sure how they should be handled. Internally, this uses the st_* types and functions as much as possible, and only adds set_* types and functions as needed. The underlying set_table implementation is stored in st.c, but there is no public C-API for it, nor is there one planned, in order to keep the ability to change the internals going forward. For internal uses of st_table with Qtrue values, those can probably be replaced with set_table. To do that, include internal/set_table.h. To handle symbol visibility (rb_ prefix), internal/set_table.h uses the same macro approach that include/ruby/st.h uses. The Set class (rb_cSet) and all methods are defined in set.c. There isn't currently a C-API for the Set class, though C-API functions can be added as needed going forward. Implements [Feature #21216] Co-authored-by: Jean Boussier <[email protected]> Co-authored-by: Oliver Nutter <[email protected]>
2025-03-27Update to ruby/spec@5e579e2Andrew Konchin
Notes: Merged: https://github.com/ruby/ruby/pull/12984
2024-11-06Update to ruby/spec@54c391eBenoit Daloze
2024-03-14Update to ruby/spec@89175b2Benoit Daloze
2024-02-26Update to ruby/spec@3a510bbBenoit Daloze
2023-12-23Fix for older set versionsNobuyoshi Nakada
`Set::VERSION` was not defined in old set.rb bundled with ruby 3.2 or earlier. Also add comment for spec/mspec/tool/remove_old_guards.rb.
2023-12-23Set 1.1 now checks subclass-ness stricterNobuyoshi Nakada
2023-09-04Update to ruby/spec@96d1072Benoit Daloze
2023-06-26Update to ruby/spec@30e1c35Benoit Daloze
2023-04-25Update to ruby/spec@7f69c86Benoit Daloze
2023-02-27Update to ruby/spec@e7dc804Benoit Daloze
2021-10-20Update to ruby/spec@d6921efBenoit Daloze
2021-09-28Followed up behavior change of setHiroshi SHIBATA
https://github.com/ruby/ruby/commit/f360ebb30606a4143029996073d29d007069428d
2021-07-29Update to ruby/spec@b65d01fBenoit Daloze
2021-02-27Update to ruby/spec@37e52e5Benoit Daloze
2020-12-05Wrap SortedSet with `ruby_version_is ""..."3.0"`Benoit Daloze
* Using $ spec/mspec/tool/wrap_with_guard.rb 'ruby_version_is ""..."3.0"' spec/ruby/library/set/sortedset/**/*_spec.rb
2020-12-05Revert "SortedSet was removed at a3db08d7b6ff119223f77e3df00b4f6deac971e2"Benoit Daloze
* This reverts commit b06ffce4aef002dc47c3c5968181230e7ab8d7cc. * Do not revert specs, wrap them with `ruby_version_is` (tool for that in next commit).
2020-12-04Skip subclass spec with SortedSetHiroshi SHIBATA
2020-12-04SortedSet was removed at a3db08d7b6ff119223f77e3df00b4f6deac971e2Hiroshi SHIBATA
2020-05-03Update to ruby/spec@032ee74Benoit Daloze
2020-04-01Drop support for ruby 2.4 from ruby/specNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/2892
2020-04-01Use FrozenError instead of frozen_error_classNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/2892
2019-07-27Update to ruby/spec@875a09eBenoit Daloze
2019-04-27Update to ruby/spec@15c9619Benoit Daloze
2019-02-07Update to ruby/spec@6cf8ebeeregon
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-20Update to ruby/spec@35a9fbaeregon
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-27Update to ruby/spec@cdd6ff7eregon
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66041 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-27Update to ruby/spec@a454137eregon
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63768 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-04Update to ruby/spec@c1b568beregon
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62656 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-27Update to ruby/spec@cbe855ceregon
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-25Add a new #filter alias for #selecteregon
* In Enumerable, Enumerator::Lazy, Array, Hash and Set [Feature #13784] [ruby-core:82285] * Share specs for the various #select#select! methods and reuse them for #filter/#filter!. * Add corresponding filter tests for select tests. * Update NEWS. [Fix GH-1824] From: Alexander Patrick <[email protected]> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62575 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-27Update to ruby/spec@0fe33aceregon
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61504 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-15Update to ruby/spec@595645feregon
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61285 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-28Update to ruby/spec@a6b8805eregon
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60525 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-28Update to ruby/spec@691755deregon
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60051 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-20Move spec/rubyspec to spec/ruby for consistencyeregon
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e