From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 84C3F1F462 for ; Mon, 17 Jun 2019 18:49:01 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 41F011209C2; Tue, 18 Jun 2019 03:48:55 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 912A8120A66 for ; Tue, 18 Jun 2019 03:48:52 +0900 (JST) Received: by filter0076p3las1.sendgrid.net with SMTP id filter0076p3las1-6917-5D07E095-E 2019-06-17 18:48:53.291571444 +0000 UTC m=+353809.917921479 Received: from herokuapp.com (unknown [3.95.136.124]) by ismtpd0035p1iad2.sendgrid.net (SG) with ESMTP id E2OGsjJXR6eahqucOIo5Mw for ; Mon, 17 Jun 2019 18:48:53.096 +0000 (UTC) Date: Mon, 17 Jun 2019 18:48:53 +0000 (UTC) From: merch-redmine@jeremyevans.net Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68692 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15929 X-Redmine-Issue-Author: janosch-x X-Redmine-Sender: jeremyevans0 X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?RVE3t853K5scBhbmJHUzZTFFeVC=2FZSUmHZ0Dc+26wcEi2CTgsF1oz0wTSSxGGN?= =?us-ascii?Q?BI1NWhRqg5YmXpMtJUat8YyBQFBtox8toisOVzb?= =?us-ascii?Q?8=2F4pJnaQIiEZgPuhjlspHPmpqJLX8e3kze1nO1Q?= =?us-ascii?Q?hv8G2QSWQwZTOifuwaIk1P=2FNtW1+WxbhyJvy0R7?= =?us-ascii?Q?6HHryYOJOPHp4z4RKJv84zbWr1BNV6FW9gg=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 93205 Subject: [ruby-core:93205] [Ruby trunk Bug#15929] Array#minmax is much slower than calling both #min and #max X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #15929 has been updated by jeremyevans0 (Jeremy Evans). File array-minmax.patch added janosch-x (Janosch M=FCller) wrote: > possible solutions: > - a) change `Enumerable#minmax` and let it `rb_funcall` `min` and `max` a= s suggested [here](https://bugs.ruby-lang.org/issues/15807#note-7) (will al= so fix 15807) We cannot use this approach. `Enumerable#each` can have side effects, and = you cannot iterate twice for `minmax` in the general case. Consider: ```ruby File.open('...', &:minmax) ``` > - b) implement minmax in array.c to call `rb_ary_min` and `rb_ary_max` I think this is the correct approach in Array and any other class that over= rides `min`/`max`. Attached is a patch that implements it. ---------------------------------------- Bug #15929: Array#minmax is much slower than calling both #min and #max https://bugs.ruby-lang.org/issues/15929#change-78653 * Author: janosch-x (Janosch M=FCller) * Status: Open * Priority: Normal * Assignee: = * Target version: = * ruby -v: 2.7.0dev * Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- this is similar to [issue 15807 about Ranges](https://bugs.ruby-lang.org/is= sues/15807) and maybe also to [13917](https://bugs.ruby-lang.org/issues/139= 17) current situation: - calling `Array#minmax` incurs a performance penalty of almost 50% compare= d to calling both `#min` and `#max` ```ruby require 'benchmark/ips' arr =3D (1..1000).map { rand } Benchmark.ips do |x| x.report('min, max') { [arr.min, arr.max] } x.report('minmax') { arr.minmax } end ``` ``` min, max 53.832k (=B1 1.8%) i/s - 270.861k in 5.033263s minmax 30.093k (=B1 1.2%) i/s - 151.980k in 5.051078s ``` background: - `#minmax` is included via `Enumerable` - `Enumerable#minmax` does not call array's optimized `#min` and `#max` imp= lementations possible solutions: - a) change `Enumerable#minmax` and let it `rb_funcall` `min` and `max` as = suggested [here](https://bugs.ruby-lang.org/issues/15807#note-7) (will also= fix 15807) - b) implement minmax in array.c to call `rb_ary_min` and `rb_ary_max` ---Files-------------------------------- array-minmax.patch (2.65 KB) -- = https://bugs.ruby-lang.org/ Unsubscribe: