ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: duerst@it.aoyama.ac.jp
To: ruby-core@ruby-lang.org
Subject: [ruby-core:102040] [Ruby master Feature#16989] Sets: need ♥️
Date: Wed, 13 Jan 2021 00:13:14 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-89892.20210113001314.182@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16989.20200626201817.182@ruby-lang.org

Issue #16989 has been updated by duerst (Martin Dürst).


marcandre (Marc-Andre Lafortune) wrote in #note-26:
> I still don't see the benefit of not simply having `Set` in core.

One direction that distinguishes Ruby from some other programming languages is big classes. A typical example is that there's no `Stack` class, the stack interface is just part of `Array`. I don't know the history, but I think the original idea was to make the set interface also be part of Array. Your `ary & set` example also alludes to that idea. Most of that interface (maybe all) is currently available, but it's not very efficient. Using `Hash` for sets eliminates the efficiency problem, as we know.

> Moreover, does this make `ary & set` possible? efficient?

I haven't checked the details, but it shouldn't be too difficult to implement this, in a reasonably efficient way. If there are any efficiency problems, they would be on the Array side, not on the Hash side.

Another advantage of using Hash may be that we can get to a syntax that is very close to the actual Mathematical set syntax. We would like to write `{1, 2, 3}`, which may even be possible. At the least, it should be possible to move to the following: `{1 => Hash::DummyValue, 2, 3}`, i.e. if the first key has a special value, then the remaining values can be left out. We may want to choose a better name for `Hash::DummyValue`, maybe something like `Hash::SetDummy`.



----------------------------------------
Feature #16989: Sets: need ♥️
https://bugs.ruby-lang.org/issues/16989#change-89892

* Author: marcandre (Marc-Andre Lafortune)
* Status: Assigned
* Priority: Normal
* Assignee: knu (Akinori MUSHA)
----------------------------------------
I am opening a series of feature requests on `Set`, all of them based on this usecase.

The main usecase I have in mind is my recent experience with `RuboCop`. I noticed a big number of frozen arrays being used only to later call `include?` on them. This is `O(n)` instead of `O(1)`.

Trying to convert them to `Set`s causes major compatibility issues, as well as very frustrating situations and some cases that would make them much less efficient.

Because of these incompatibilities, `RuboCop` is in the process of using a custom class based on `Array` with optimized `include?` and `===`. `RuboCop` runs multiple checks on Ruby code. Those checks are called cops. `RuboCop` performance is (IMO) pretty bad and some cops  currently are in `O(n^2)` where n is the size of the code being inspected. Even given these extremely inefficient cops, optimizing the 100+ such arrays (most of which are quite small btw) gave a 5% speed boost.

RuboCop PRs for reference: https://github.com/rubocop-hq/rubocop-ast/pull/29
https://github.com/rubocop-hq/rubocop/pull/8133

My experience tells me that there are many other opportunities to use `Set`s that are missed because `Set`s are not builtin, not known enough and have no shorthand notation.

In this issue I'd like to concentrate the discussion on the following request: `Set`s should be core objects, in the same way that `Complex` were not and are now. Some of the upcoming feature requests would be easier (or only possible) to implement were `Set`s builtin.



-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2021-01-13  0:13 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-26 20:18 [ruby-core:98964] [Ruby master Feature#16989] Sets: need ♥️ marcandre-ruby-core
2020-06-27  2:14 ` [ruby-core:98973] " nobu
2020-08-24  4:13 ` [ruby-core:99677] " mame
2020-08-24 12:55 ` [ruby-core:99680] " marcandre-ruby-core
2020-08-24 15:12 ` [ruby-core:99683] " daniel
2020-09-02  9:52 ` [ruby-core:99834] " eregontp
2020-09-02 13:44 ` [ruby-core:99838] " marcandre-ruby-core
2020-09-02 14:45 ` [ruby-core:99843] " daniel
2020-09-02 21:00 ` [ruby-core:99853] " eregontp
2020-09-02 21:01 ` [ruby-core:99854] " eregontp
2020-09-02 21:15 ` [ruby-core:99855] " marcandre-ruby-core
2020-09-03  1:14 ` [ruby-core:99859] " matz
2020-09-03  7:48 ` [ruby-core:99865] " knu
2020-09-03 21:44 ` [ruby-core:99901] " marcandre-ruby-core
2020-09-03 21:44 ` [ruby-core:99902] " marcandre-ruby-core
2020-12-27 10:24 ` [ruby-core:101736] " mame
2021-01-12  4:46 ` [ruby-core:102013] " akr
2021-01-12  4:57 ` [ruby-core:102014] " knu
2021-01-12 16:05 ` [ruby-core:102033] " marcandre-ruby-core
2021-01-12 16:26 ` [ruby-core:102034] " akr
2021-01-12 16:42 ` [ruby-core:102035] " marcandre-ruby-core
2021-01-13  0:13 ` duerst [this message]
2021-01-13  1:26 ` [ruby-core:102042] " akr
2021-01-13  1:26 ` [ruby-core:102043] " knu
2021-01-13  1:48 ` [ruby-core:102044] " knu
2021-01-13  3:31 ` [ruby-core:102046] " marcandre-ruby-core
2021-01-13  5:42 ` [ruby-core:102047] " matz
2021-02-22 23:48 ` [ruby-core:102574] " blogger
2021-02-23 12:39 ` [ruby-core:102577] " eregontp
2021-04-28  6:05 ` [ruby-core:103638] " grzegorz.jakubiak
2021-10-23 21:04 ` [ruby-core:105760] " greggzst (Grzegorz Jakubiak)
2022-02-17 10:12 ` [ruby-core:107623] " knu (Akinori MUSHA)
2022-02-17 12:13 ` [ruby-core:107628] " matz (Yukihiro Matsumoto)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-89892.20210113001314.182@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).