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=-3.8 required=3.0 tests=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,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 7C9E520A1E for ; Mon, 10 Dec 2018 00:55:26 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 8054B120CC4; Mon, 10 Dec 2018 09:55:22 +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 B45E2120CA6 for ; Mon, 10 Dec 2018 09:55:19 +0900 (JST) Received: by filter0172p3mdw1.sendgrid.net with SMTP id filter0172p3mdw1-30880-5C0DB974-34 2018-12-10 00:55:16.857781913 +0000 UTC m=+434366.186807898 Received: from herokuapp.com (ec2-54-198-43-225.compute-1.amazonaws.com [54.198.43.225]) by ismtpd0006p1iad2.sendgrid.net (SG) with ESMTP id PPzieNETRSaflHa-s-a8-Q Mon, 10 Dec 2018 00:55:16.690 +0000 (UTC) Date: Mon, 10 Dec 2018 00:55:17 +0000 (UTC) From: shyouhei@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 65779 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15393 X-Redmine-Issue-Author: tenderlovemaking X-Redmine-Sender: shyouhei 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: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS4LmytnjT5rtFoA9O+VpR3mFFTJ/NRBRtPDWg j0gER7CpQ9xvZRL4J/1RJC27LMyGx+1z1ntjv5KuHHIitOHwnRLXzu3yGvlNgq5IzeUKiboD3rT2Ye FzFxcM/ULngKeQNk7ob4hfKlAtfsdgwq3QJmoP56Nd8nqkchVbxmNzhM+Q== X-ML-Name: ruby-core X-Mail-Count: 90387 Subject: [ruby-core:90387] [Ruby trunk Feature#15393] Add compilation flags to freeze Array and Hash literals 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 #15393 has been updated by shyouhei (Shyouhei Urabe). Off topic: Eregon (Benoit Daloze) wrote: > One alternative idea which would achieve a similar goal is having #deep_freeze on core types, and recognizing `{ 'a' => ['b', { 'c' => 'd' }] }.deep_freeze`. > This would be more general, since it would also work for freezing non-constant/non-literal data structures. Object#deep_freeze can be written without any extensions right now, like this: https://github.com/shyouhei/optdown/blob/master/lib/optdown/deeply_frozen.rb I heard this trick from @akr . ---------------------------------------- Feature #15393: Add compilation flags to freeze Array and Hash literals https://bugs.ruby-lang.org/issues/15393#change-75509 * Author: tenderlovemaking (Aaron Patterson) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Hi, I would like to add VM compilation options to freeze array and hash literals. For example: ~~~ ruby frozen = RubyVM::InstructionSequence.compile(<<-eocode, __FILE__, nil, 0, frozen_string_literal: true, frozen_hash_and_array_literal: true) { 'a' => ['b', { 'c' => 'd' }] } eocode puts frozen.disasm ~~~ Output is: ~~~ $ ./ruby thing.rb == disasm: #@thing.rb:0 (0,0)-(0,34)> (catch: FALSE) 0000 putobject {"a"=>["b", {"c"=>"d"}]} 0002 leave ~~~ Anything nested in the hash that can't be "frozen" will cause it to not be frozen. For example: ~~~ ruby not_frozen = RubyVM::InstructionSequence.compile(<<-eocode, __FILE__, nil, 0, frozen_string_literal: true, frozen_hash_and_array_literal: true) { 'a' => some_method } eocode puts not_frozen.disasm ~~~ Output: ~~~ $ ./ruby thing.rb == disasm: #@thing.rb:0 (0,0)-(0,24)> (catch: FALSE) 0000 putobject "a" 0002 putself 0003 opt_send_without_block , 0006 newhash 2 0008 leave ~~~ Eventually I would like to freeze array and hash literals in source code itself, but I think this is a good first step. The reason I want this feature is I think we can reduce some object allocations, and once Guilds are implemented, easily create immutable data. I've attached a patch that implements the above. (Also I think maybe "frozen_literals" would be a better name, but I don't want to imply that numbers or booleans are frozen too) Thanks! ---Files-------------------------------- 0001-Add-compile-options-for-freezing-hash-and-array-lite.patch (6.14 KB) -- https://bugs.ruby-lang.org/