From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 08C9119C03E1 for ; Tue, 24 Nov 2015 08:54:23 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 8258CB5D98E for ; Sat, 21 Nov 2015 08:56:54 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id AC22618CC7F2 for ; Sat, 21 Nov 2015 08:56:54 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id DE4FD12046D; Sat, 21 Nov 2015 08:56:52 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id DDF2B120441 for ; Sat, 21 Nov 2015 08:56:49 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=KXYYLNeeLCtmyEX8j9BdVkvXerU=; b=ZDOH2Z67HLDjBPPgzP oKFgQvwkPvJURtVffZ6qZU0cNosOQoZAacPc5wHPjoejaq1l/UgrOVzJtTpkq5kJ vwv2yiIHYjkK3fxfxpyUgEXQGThNzNPzxeouOjIPvLIZAw6ARDdVkdOlcLf3aABX cbg2UyWDU5Ebrbbys5mf4wnPE= Received: by filter0546p1mdw1.sendgrid.net with SMTP id filter0546p1mdw1.1399.564FB33D44 2015-11-20 23:56:45.807784125 +0000 UTC Received: from herokuapp.com (ec2-54-162-88-28.compute-1.amazonaws.com [54.162.88.28]) by ismtpd0004p1iad1.sendgrid.net (SG) with ESMTP id mk8UTdcmQGGauOyp2oODeQ for ; Fri, 20 Nov 2015 23:56:45.702 +0000 (UTC) Date: Fri, 20 Nov 2015 23:56:45 +0000 From: nobu@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Redmine-MailingListIntegration-Message-Ids: 46287 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11725 X-Redmine-Issue-Author: ko1 X-Redmine-Issue-Assignee: ko1 X-Redmine-Sender: nobu 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS6fyq+dbDjsA2A+Xm7WEJko4O8RPXdzBPBCWV GRGPPviz9zhtegkBHneya/OT5MTwODGI0NohC4Uii25xb+J+hSnPOTUKayFOyxW7gDZsq1YiznNDOI cTqc6IErW73D5o5KN1l1WkfvWtUMZdfZcB5d X-ML-Name: ruby-core X-Mail-Count: 71618 Subject: [ruby-core:71618] [Ruby trunk - Feature #11725] debugging support for frozen string 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: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #11725 has been updated by Nobuyoshi Nakada. You run `static_test3` twice, not `static_test4`. ---------------------------------------- Feature #11725: debugging support for frozen string literal https://bugs.ruby-lang.org/issues/11725#change-55019 * Author: Koichi Sasada * Status: Closed * Priority: Normal * Assignee: Koichi Sasada ---------------------------------------- # Background Debug frozen string literal is difficult because frozen string can be modified at far from created locations. For example, library X creates one string and modify this string is common situation. Also library X can pass the created string to library Y and also Y passes it to library Z. Clients receives a string from Z, then clients can not understand where the created location and can not debug it. To help such situation, I introduced --enable-frozen-string-literal-debug option (a command line option and a compile option). This option enable to embed created location information into frozen string and to display RuntimeError message with this location information such as: ``` ../../trunk/test.rb:21:in `
': can't modify frozen String, created at ../../trunk/test.rb:17 (RuntimeError) ``` (maybe only few people knows about this feature because NEWS doesn't describe it) # Proposal: Make it as default behavior At first, I introduced this option as an optional feature because this option seems slow down applications. However, including this information for sting literals is not so big impact because adding information for each string literal only at compile time. Another drawback is that we can't de-duplicate same frozen strings, but it is trivial issue. One problem is string interpolation. Adding debug information for such dynamically created strings is not so easy. So I leave --enable-frozen-string-literal-debug for this type of strings. It is too long to use it, so -d or --debug option should enable this feature. Conclusion: * adding string literals as default. * --enable-frozen-string-literal-debug for string interpolation. -d or --debug also enable this feature. I hope we can change every string literals to frozen strings. Any comments are welcome. # Naming Now, I prefer --debug-frozen-string-literal than --enable-frozen-string-literal-debug. # Misc: benchmark script and result ```ruby require 'benchmark' N = 5_000_000 def static_test1 "foo" end def static_test2 "foo".freeze end line = __LINE__ + 1 RubyVM::InstructionSequence.compile(%q{ def static_test3 "foo" end }, __FILE__, __FILE__, line, {frozen_string_literal: true}).eval line = __LINE__ + 1 RubyVM::InstructionSequence.compile(%q{ def static_test4 "foo" end }, __FILE__, __FILE__, line, {frozen_string_literal: true, frozen_string_literal_debug: true}).eval S = 'baz' def dynamic_test1 "foo#{S}bar" end def dynamic_test2 "foo#{S}bar".freeze end line = __LINE__ + 1 RubyVM::InstructionSequence.compile(%q{ def dynamic_test3 "foo#{S}bar" end }, __FILE__, __FILE__, line, {frozen_string_literal: true}).eval line = __LINE__ + 1 RubyVM::InstructionSequence.compile(%q{ def dynamic_test4 "foo#{S}bar" end }, __FILE__, __FILE__, line, {frozen_string_literal: true, frozen_string_literal_debug: true}).eval Benchmark.bmbm{|x| x.report('STATIC: without freeze' ){N.times{static_test1}} x.report('STATIC: freeze by method'){N.times{static_test2}} x.report('STATIC: freeze by pragma'){N.times{static_test3}} x.report('STATIC: freeze by pragma/debug'){N.times{static_test3}} x.report('DYNAMIC:without freeze' ){N.times{dynamic_test1}} x.report('DYNAMIC:freeze by method'){N.times{dynamic_test2}} x.report('DYNAMIC:freeze by pragma'){N.times{dynamic_test3}} x.report('DYNAMIC:freeze by pragma/debug'){N.times{dynamic_test4}} } # error message check ['static', 'dynamic'].each{|feature| (1..4).each{|type| begin m = "#{feature}_test#{type}" send(m) << "str" rescue RuntimeError => e p [m, e] else p [m, nil] end } } ``` ``` user system total real STATIC: without freeze 0.550000 0.000000 0.550000 ( 0.545465) STATIC: freeze by method 0.330000 0.000000 0.330000 ( 0.337528) STATIC: freeze by pragma 0.360000 0.000000 0.360000 ( 0.366031) STATIC: freeze by pragma/debug 0.360000 0.000000 0.360000 ( 0.353534) DYNAMIC:without freeze 1.230000 0.000000 1.230000 ( 1.230027) DYNAMIC:freeze by method 1.580000 0.000000 1.580000 ( 1.585025) DYNAMIC:freeze by pragma 1.420000 0.000000 1.420000 ( 1.424006) DYNAMIC:freeze by pragma/debug 3.510000 0.000000 3.510000 ( 3.510279) # error message check results ["static_test1", nil] ["static_test2", #] ["static_test3", #] ["static_test4", #] ["dynamic_test1", nil] ["dynamic_test2", #] ["dynamic_test3", #] ["dynamic_test4", #] ``` -- https://bugs.ruby-lang.org/