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-Status: No, score=-3.9 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY 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 B2DCC1F4B4 for ; Mon, 28 Dec 2020 16:52:57 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 217C7120A53; Tue, 29 Dec 2020 01:52:07 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 8D5D2120A51 for ; Tue, 29 Dec 2020 01:52:05 +0900 (JST) Received: by filterdrecv-p3mdw1-7474cd8bfd-8hgbm with SMTP id filterdrecv-p3mdw1-7474cd8bfd-8hgbm-20-5FEA0D63-7 2020-12-28 16:52:51.108936661 +0000 UTC m=+1533039.129702037 Received: from herokuapp.com (unknown) by ismtpd0035p1iad2.sendgrid.net (SG) with ESMTP id b9MaxrJzQ5Gu1R4iQ6p6fQ for ; Mon, 28 Dec 2020 16:52:51.051 +0000 (UTC) Date: Mon, 28 Dec 2020 16:52:51 +0000 (UTC) From: chris@chrisseaton.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 77689 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17474 X-Redmine-Issue-Author: jzakiya X-Redmine-Sender: chrisseaton 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?NC3uwXThXFpCxeI+EU+QkxuM4rZrp+UkiX2tCBF1HHsKidrVAmRQUIH7xU08mO?= =?us-ascii?Q?Kw3it=2FkFxwMmUris63ab4mRKSYR4hvI9mCwKr59?= =?us-ascii?Q?PNVARt9fXI5rfQyhEUBU1IEASfTI2Vwh3FW7nx+?= =?us-ascii?Q?vbUP8Tf94SppenP8f2Fza8YDQh98Js7+z9o9wwz?= =?us-ascii?Q?mFbV8=2FxubDLe4T5ZadNdipnZJ=2FSRVUWKb+VvR2g?= =?us-ascii?Q?UmrEinK3Qrsx294qw=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 101787 Subject: [ruby-core:101787] [Ruby master Feature#17474] Interpreting constants at compile time 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="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17474 has been updated by chrisseaton (Chris Seaton). > Agree but... can a JIT optimize `Set["x","y","z"].include?(v)` so that the Set is only allocated once? Absolutely it could - TruffleRuby will today already optimise `['x', 'y', 'z'].include?('y')` to be `true` and it doesn't allocate *anything*. It doesn't optimise your exact example - I didn't look into why but if it's important to people I'm sure it could be done. ---------------------------------------- Feature #17474: Interpreting constants at compile time https://bugs.ruby-lang.org/issues/17474#change-89615 * Author: jzakiya (Jabari Zakiya) * Status: Open * Priority: Normal ---------------------------------------- Ruby has borrowed concepts/idioms from allot of languages. I am proposing borrowing a feature from Forth to provide for compile time interpretation of Constants. This should make executed code faster|efficient, while maintaining source code brevity|clarity. Below is actual code used in a large rubygem I have. To develop this method, I had to do allot of test runs to determine the range values. Once found, these values don't change, but I just kept the computed forms of the values, in case I want to upgrade them. In Forth I can interpret those expressions that result in constants, which will be compiled as single values for run time. See wikeipedia article on Forth below starting at **Mixing states of compiling and interpreting**. https://en.wikipedia.org/wiki/Forth_(programming_language) Forth was designed for, and is still used most frequently, in hardware controllers, and with microprocessors. IMHO this feature would also make MRuby more code efficient and faster for this domain too, and IOT devices. Below is an example of real code that would benefit from this. While this example would result in numerical constant, string constants could also be interpreted. ``` def select_pg(endnum, startnum) start_num = end_num end_num = endnum; start_num = startnum range = end_num - start_num pg = 5 if start_num <= Integer.sqrt(end_num) # for one array of primes upto N pg = 7 if end_num > 50 * 10**4 pg = 11 if end_num > 305 * 10**5 else # for split array cases pg = 7 if ((10**6 ... 10**7).include?(range) && start_num < 10**8) || ((10**7 ... 10**8).include?(range) && start_num < 46 * 10**8) || ((10**8 ... 10**9).include?(range) && start_num < 16 * 10**10) || (range >= 10**9 && start_num < 26 * 10**12) pg = 11 if ((10**8 ... 10**9).include?(range) && start_num < 55 * 10**7) || (range >= 10**9 && start_num < 45 * 10**9) end primes = [2, 3, 5, 7, 11, 13].select { |p| p <= pg } {primes, primes.reduce(:*)} # [excluded primes, modpg] for PG end ``` Allowing for compile time interpretation, the code could be rewritten as below. ``` def select_pg(endnum, startnum) start_num = end_num end_num = endnum; start_num = startnum range = end_num - start_num pg = 5 if start_num <= Integer.sqrt(end_num) # for one array of primes upto N pg = 7 if end_num > [50 * 10**4] pg = 11 if end_num > [305 * 10**5] else # for split array cases pg = 7 if (([10**6] ... [10**7]).include?(range) && start_num < [10**8]) || (([10**7] ... [10**8]).include?(range) && start_num < [46 * 10**8]) || (([10**8] ... [10**9]).include?(range) && start_num < [16 * 10**10])|| (range >= [10**9] && start_num < [26 * 10**12]) pg = 11 if (([10**8] ... [10**9]).include?(range) && start_num < [55 * 10**7]) || (range >= [10**9] && start_num < [45 * 10**9]) end primes = [2, 3, 5, 7, 11, 13].select { |p| p <= pg } {primes, primes.reduce(:*)} # [excluded primes, modpg] for PG end ``` This maintains the original form, so if I need/want to change the range limits again I can just change the calculation inline, without having to remember where those values came from. As 3.0 has introduced many new features and idioms, this could be introduced with no breaking change too. Old code would work as before, while new code could take advantage of this feature. Thanks is advance of giving this proposal serious consideration. -- https://bugs.ruby-lang.org/