ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: janosch84@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92463] [Ruby trunk Bug#15807] Range#minmax is slow and never returns for endless ranges
Date: Sun, 28 Apr 2019 14:29:50 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77812.20190428142949.6ac2ff5a745fb475@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15807.20190428121410@ruby-lang.org

Issue #15807 has been updated by janosch-x (Janosch Müller).


mame (Yusuke Endoh) wrote:
> `Range#max` is not consistent with `Enumerable#minmax`.

Thanks for pointing this out, I wasn't aware of that. Floats are another example:
``` ruby
(1..(5.5)).max # => 5.5
(1..(5.5)).minmax # => [1, 5]
```

Maybe we could fix / speedup `minmax` only for ranges where `begin` and `end` are `NIL_P`/`is_integer_p`/`Float::INFINITY`, and call super for all other cases?

A check for `Float::INFINITY` might be helpful here, too, while we're at it: https://github.com/ruby/ruby/blob/ae6c195f30f76b1dc4a32a0a91d35fe80f6f85d3/range.c#L808

My use case has to do with Regexp quantification. I can go into more detail, but to describe it quickly, I want to provide information about how many chars a Regexp can match in https://github.com/ammar/regexp_parser. Ranges, some ending with Infinity, are the most natural choice for this, but minmax would be useful in the related code. Also, I don't want to hand out "dangerous" Ranges to gem users. Maybe I will `#extend` the Ranges with a safe minmax.


----------------------------------------
Bug #15807: Range#minmax is slow and never returns for endless ranges
https://bugs.ruby-lang.org/issues/15807#change-77812

* Author: janosch-x (Janosch Müller)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.6.3p62
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
current situation:

- `(1..).minmax` runs forever
- `(1..).max` raises "cannot get the maximum of endless range"
- `(1..Float::INFINITY).minmax` runs forever
- `(1..Float::INFINITY).max` returns instantly
- `(1..1_000_000_000).minmax` takes one minute
- `(1..1_000_000_000).max` returns instantly

my suggestion:

- implement `minmax` in range.c, return [`range_min`, `range_max`]
- for endless ranges, this will trigger the same error as `max` does
- delegate to enum (rb_call_super) only if called with a block (?)

i could perhaps provide a PR if you can point me to some information on how to contribute.

cheers!



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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

  parent reply	other threads:[~2019-04-28 14:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15807.20190428121410@ruby-lang.org>
2019-04-28 12:14 ` [ruby-core:92459] [Ruby trunk Bug#15807] Range#minmax is slow and never returns for endless ranges janosch84
2019-04-28 12:32 ` [ruby-core:92461] " mame
2019-04-28 14:29 ` janosch84 [this message]
2019-05-31 19:31 ` [ruby-core:92916] " mohsen
2019-06-08  5:24 ` [ruby-core:93021] " merch-redmine
2019-06-15  9:50 ` [ruby-core:93156] " janosch84
2019-06-16 13:26 ` [ruby-core:93181] " janosch84
2019-06-18 14:08 ` [ruby-core:93224] " eregontp

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-77812.20190428142949.6ac2ff5a745fb475@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).