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.8 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 EAA101F5AE for ; Sat, 27 Jun 2020 17:55:26 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id EB0C0120A14; Sun, 28 Jun 2020 02:54:55 +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 B49AD120A13 for ; Sun, 28 Jun 2020 02:54:53 +0900 (JST) Received: by filterdrecv-p3iad2-5b55dcd864-xnspw with SMTP id filterdrecv-p3iad2-5b55dcd864-xnspw-17-5EF78802-31 2020-06-27 17:55:14.50381068 +0000 UTC m=+89150.928388676 Received: from herokuapp.com (unknown) by geopod-ismtpd-1-0 (SG) with ESMTP id DnXOTwFETwaSdoPWNf0T0w for ; Sat, 27 Jun 2020 17:55:14.369 +0000 (UTC) Date: Sat, 27 Jun 2020 17:55:14 +0000 (UTC) From: ko1@atdot.net Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 74775 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 16986 X-Redmine-Issue-Author: ko1 X-Redmine-Issue-Assignee: matz X-Redmine-Sender: ko1 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?fVTMYOBjtdvXNcWwrscBhLsHItUXVK5L4mtnq0mdcRchLe7MJqwz5rlslkHaBC?= =?us-ascii?Q?iuaefEseyqwurAceedE+oElnd1Irxeqyu9kNF0V?= =?us-ascii?Q?1xqoKdzTGvSCIfea8lzXQ+TwudHz5Xq2cNm8N9O?= =?us-ascii?Q?6S65+vUaqBTXsHNDliJMvPs4drZOdFgPXE33Pxc?= =?us-ascii?Q?nzxckKQ6g7xUmgIb+GLLvDUSSxFZtC5CiiA=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 98985 Subject: [ruby-core:98985] [Ruby master Feature#16986] Anonymous Struct literal 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 #16986 has been updated by ko1 (Koichi Sasada). # Q&A ## Splat like Hash literal and method arguments https://bugs.ruby-lang.org/issues/16986#note-3 > I was wondering if ${} can do the following things compared to Hash literals: Not allowed because of vulnerability concerns (please read a ticket for more details). ## Syntax * (1) `${a:1, b:2} # original` * (2) `{|a:1, b:2|} # matz's idea` ... conflict with block parameters. * (3) `struct a: 1, b: 2 # #1` ... introducing new `struct` keyword can introduce incompatibility. * (4) `%o{a:1, b:2} # #6` * (5) `(a:1, b:2) # #9` * (6) Methods * (6-1) `Struct.anonymous(a:1, b:2) # #4` * (6-2) `Struct(a:1, b:2) # #4, #16938 * (6-3) `Struct[a:1, b:2] # #10` Some support comments on `$(...)`: * I can recognize it is not a global variables. * `$` is seems as an initial letter of `Struct` ... `S` !!! (50% joking) * If we can introduce `${ ... }`, we can also consider about `$[...]` and `$(...)`. I agree it can introduce further chaotic. * We can replace `$` with `@`, but no reason to choose `@` ... ah, not a support comment. I thought similar idea on "(4) `%o{a:1, b:2} # #6`", and my idea was `%t` (S*t*ruct). However, there is no `%` notation which allows Ruby's expression in it. In other words, existing `%` notation defines different language in `%` (`%w, %i` for example). This is why I gave up this idea. But I don't against it (new language can accept Ruby's expression). For "(6) Methods", my first proposal was `Struct(a;1, b:2)`. https://twitter.com/_ko1/status/1276055259241046016?s=20 However, there are several advantages by introducing new syntax which are described in a ticket. And real reason I make a demonstrate moving code https://github.com/ruby/ruby/pull/3259 is I want to modify parse.y to escape from Ractor's debugging. The biggest advantage of choosing a method approach is simplicity. No new syntax and only a few learning cost. It is easy to introduce same method for older versions (~2.7). (For performance of object creation, we can introduce specialization for `Struct()` method to prepare an anonymous class at compile time) "(5) `(a:1, b:2)" seems interesting, but I agree there are issues which are pointed in the comment 9. ---------------------------------------- Feature #16986: Anonymous Struct literal https://bugs.ruby-lang.org/issues/16986#change-86365 * Author: ko1 (Koichi Sasada) * Status: Open * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) ---------------------------------------- # Abstract How about to introduce anonymous Struct literal such as `${a: 1, b: 2}`? It is almost same as `Struct.new(:a, :b).new(1, 2)`. # Proposal ## Background In many cases, people use hash objects to represents a set of values such as `person = {name: "ko1", country: 'Japan'}` and accesses it with `person[:name]` and so on. It is not easy to write (3 letters `[:]`!), and easy to introduce misspelling (`person[:nama]` doesn't raise an error). If we make a `Struct` objects such as `Person = Struct.new(:name, :age)` and `person = Person.new('ko1', 'Japan')`, we can access it with `person.name` naturally. However making new `Struct` is a cost of coding. Some cases we don't want to name (such as `Person`). Using `OpenStruct` (`person = OpenStruct.new(name: "ko1", country: "Japan")`), we can access it with `person.name`, but we can extend the fields and the performance is not good. Of course, we can define the class `Person` and attr_readers. But several lines we need. To summaries the issues: * Easy to Write * Don't need to declare the class * Accessible with `person.name` format * Limited fields * Better performance ## Idea Introduce new syntax to make an anonymous Struct literal such as: `${ a: 1, b: 2 }`. Similar to Hash syntax (with labels), but `$` prefix to recognize. Anonymous structs which has same member with same order share the class. ```ruby s1 = ${a: 1, b: 2, c: 3} s2 = ${a: 1, b: 2, c: 3} assert s1 == s2 s3 = ${a: 1, c: 3, b: 2} s4 = ${d: 4} assert_equal false, s1 == s3 assert_equal false, s1 == s4 ``` ## Note Unlike Hash literal syntax, this proposal only allows `label: expr` notation. No `${**h}` syntax. This is because if we allow to splat a Hash, it can be a vulnerability by splatting outer-input Hash. Thanks for this spec, we can specify the anonymous Struct class at compile time. We don't need to find or create Struct classes at runtime. ## Implementatation https://github.com/ruby/ruby/pull/3259 # Discussion ## Notation Matz said he thought about `{|a: 1, b: 2 |}` syntax. ## Performance Surprisingly, Hash is fast and Struct is slow. ```ruby Benchmark.driver do |r| r.prelude <<~PRELUDE st = Struct.new(:a, :b).new(1, 2) hs = {a: 1, b: 2} class C attr_reader :a, :b def initialize() = (@a = 1; @b = 2) end ob = C.new PRELUDE r.report "ob.a" r.report "hs[:a]" r.report "st.a" end __END__ Warming up -------------------------------------- ob.a 38.100M i/s - 38.142M times in 1.001101s (26.25ns/i, 76clocks/i) hs[:a] 37.845M i/s - 38.037M times in 1.005051s (26.42ns/i, 76clocks/i) st.a 33.348M i/s - 33.612M times in 1.007904s (29.99ns/i, 87clocks/i) Calculating ------------------------------------- ob.a 87.917M i/s - 114.300M times in 1.300085s (11.37ns/i, 33clocks/i) hs[:a] 85.504M i/s - 113.536M times in 1.327850s (11.70ns/i, 33clocks/i) st.a 61.337M i/s - 100.045M times in 1.631064s (16.30ns/i, 47clocks/i) Comparison: ob.a: 87917391.4 i/s hs[:a]: 85503703.6 i/s - 1.03x slower st.a: 61337463.3 i/s - 1.43x slower ``` I believe we can speed up `Struct` similar to ivar accesses, so we can improve the performance. BTW, OpenStruct (os.a) is slow. ``` Comparison: hs[:a]: 92835317.7 i/s ob.a: 85865849.5 i/s - 1.08x slower st.a: 53480417.5 i/s - 1.74x slower os.a: 12541267.7 i/s - 7.40x slower ``` For memory consumption, `Struct` is more lightweight because we don't need to keep key names. ## Naming If we name the anonymous class, the same member literals share the name. ```ruby s1 = ${a:1} s2 = ${a:2} p [s1, s2] #=> [#, #] A = s1.class p [s1, s2] #=> [#, #] ``` Maybe it is not good behavior. -- https://bugs.ruby-lang.org/