From: janosch84@... Date: 2019-04-28T14:29:50+00:00 Subject: [ruby-core:92463] [Ruby trunk Bug#15807] Range#minmax is slow and never returns for endless ranges 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: