ruby/tool
Jeremy Evans e4f85bfc31 Implement Set as a core class
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 <jean.boussier@gmail.com>
Co-authored-by: Oliver Nutter <mrnoname1000@riseup.net>
2025-04-26 10:31:11 +09:00
..
annocheck Use ghcr.io/ruby/fedora:latest instead of docker.io for avoiding rate limit 2025-03-13 13:08:15 +09:00
bundler Update the all of gemfile deps 2025-03-27 16:56:59 +09:00
lib Assign the result of format_changelog to the changelog writer 2025-04-22 21:02:29 +09:00
lrama Lrama v0.7.0 2025-01-21 10:16:05 +09:00
m4 Support RUBY_MODULAR_GC with LOAD_RELATIVE 2024-12-22 22:10:26 +09:00
releng
ruby_vm Disable ZJIT profiling at call-threshold (https://github.com/Shopify/zjit/pull/99) 2025-04-18 21:53:01 +09:00
test Remove test_retry_workers 2025-03-28 14:19:33 +09:00
asm_parse.rb
auto-style.rb Fix auto-style target reporting 2025-04-18 09:02:15 +09:00
bisect.sh
build-transcode
checksum.rb
colors
darwin-ar
darwin-cc
disable_ipv6.sh
downloader.rb tool/downloader.rb: Stop caching already existing files 2025-02-13 15:35:31 +09:00
enc-case-folding.rb
enc-emoji-citrus-gen.rb
enc-emoji4unicode.rb
enc-unicode.rb Support for Indic_Conjunct_Break in Unicode 15.1 2025-03-18 21:18:12 +09:00
eval.rb
expand-config.rb
extlibs.rb downloader.rb: Use keyword arguments 2025-01-16 15:35:50 +09:00
fake.rb Update rubyarchdir and rubylibdir as well for tests 2024-11-15 18:28:16 +09:00
fetch-bundled_gems.rb Allow to run 'make test-bundled-gems BUNDLED_GEMS=csv,rexml' for only testing csv and rexml 2024-11-27 16:45:10 +09:00
file2lastrev.rb Ignore broken git work directory at creating revision.h [ci skip] 2025-02-17 18:25:07 +09:00
format-release increase diff.renameLimit 2024-12-12 22:58:44 +09:00
gen-github-release.rb
gen-mailmap.rb
gen_dummy_probes.rb
gen_ruby_tapset.rb
generic_erb.rb Fix a typo in a local variable name 2024-11-04 23:32:25 +09:00
git-refresh
gperf.sed Use gperf 3.1 to generate ANSI-C code 2024-06-24 23:43:45 +09:00
id2token.rb
ifchange Fix --empty option of ifchange 2024-11-24 18:34:01 +09:00
insns2vm.rb
install-sh
intern_ids.rb
leaked-globals [Bug #20910] dtrace related symbols are not considered leaked 2024-11-30 14:04:53 +09:00
ln_sr.rb
make-snapshot Fix lrama for snapshot 2025-01-21 23:12:38 +09:00
make_hgraph.rb
mdoc2man.rb
merger.rb Add fail safe to check uncommitted changes 2024-12-25 16:55:27 +09:00
missing-baseruby.bat [Bug #20687] Check if base ruby provides necessary libraries 2024-08-21 16:56:44 +09:00
mk_builtin_loader.rb YJIT: Replace Array#each only when YJIT is enabled (#11955) 2024-11-04 11:14:28 -05:00
mk_rbbin.rb Fix prelude to use IBF 2024-09-08 23:40:17 +09:00
mkconfig.rb [Feature #21116] Extract RJIT as a third-party gem 2025-02-13 18:01:03 +09:00
mkrunnable.rb [Bug #20800] Move executable binary file path 2024-11-09 19:58:44 +09:00
node_name.rb
outdate-bundled-gems.rb
parse.rb
prereq.status
probes_to_wiki.rb
rbinstall.rb rbinstall.rb: Note about no_write [ci skip] 2025-04-16 15:45:08 +09:00
rbs_skip_tests Also skip test_load(JSONSingletonTest) 2025-03-31 14:06:27 +09:00
rbuninstall.rb
rdoc-srcdir rdoc-srcdir can refer srcdir by itself 2025-01-15 16:52:56 +09:00
redmine-backporter.rb redmine-backporter: Support matching multiple revs 2024-09-02 02:25:29 -07:00
release.sh Revert "Experiment with removing --profile ruby for now" 2024-09-02 23:37:15 -07:00
rmdirs
run-gcov.rb Support LCOV 2.0 2024-06-21 14:48:44 +09:00
run-lcov.rb Support LCOV 2.0 2024-06-21 14:48:44 +09:00
runruby.rb
search-cgvars.rb
strip-rdoc.rb
sync_default_gems.rb Sync Bundler and adapt to new spec setup 2025-04-10 19:21:51 +09:00
test-annocheck.sh tool/test-annocheck.sh: Refactor with double-quotes. 2024-08-07 18:51:20 +02:00
test-bundled-gems.rb Implement Set as a core class 2025-04-26 10:31:11 +09:00
test-coverage.rb
transcode-tblgen.rb
travis_retry.sh
travis_wait.sh
update-bundled_gems.rb Ignore DEFAULT_SOURCE_DATE_EPOCH [ci skip] 2025-04-22 21:45:42 +09:00
update-deps
update-NEWS-gemlist.rb Support The following bundled gems are added. 2024-10-09 12:55:28 +09:00
update-NEWS-refs.rb Support Misc tracker [ci skip] 2024-04-18 08:10:23 +09:00
vtlh.rb
wasm-clangw