ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: Ruby developers <ruby-core@ruby-lang.org>
Subject: [ruby-core:71820] Re: [Ruby trunk - Bug #11759] URI breaks with frozen strings
Date: Thu, 3 Dec 2015 23:41:03 +0000	[thread overview]
Message-ID: <20151203234103.GA30841@dcvr.yhbt.net> (raw)
In-Reply-To: <redmine.journal-55216.20151203052009.a5e0fe7c1b5704b6@ruby-lang.org>

colin@invoca.com wrote:
> Compared to the surrounding code in the full method, would the extra
> constant lookup make a measurable difference in code size?

64 bytes on ruby 2.3.0dev (2015-12-03 trunk 52872) [x86_64-linux].

require 'objspace'
iseq = RubyVM::InstructionSequence
p ObjectSpace.memsize_of(iseq.compile("str = String.new"))
p ObjectSpace.memsize_of(iseq.compile("str = ''.dup"))
p ObjectSpace.memsize_of(iseq.compile("str = ''.freeze.dup"))

Outputs:
480
416
416

> because the `.freeze` is temporary for Ruby 2.1 and 2.2, right?  When
> would this copy of generic.rb ever be run with Ruby versions earlier
> than 2.3?

No, we shouldn't worry about performance with older versions of Ruby
with the stdlib.

> Assuming it will just be used for Ruby 2.3 and later, the
> magic comment included in the patch will implicitly freeze the string
> literal.  Hence this would also be sufficient (and in my opinion,
> nearly as clear in intention as `String.new`):
> 
> ~~~
> ''.dup
> ~~~

I prefer that if we go for "frozen_string_literal: true" in the comment.

I've also added this test for to_s.  However, checking more closely,
the "path" accessor also gets frozen changes because we call
set_path with literal strings.

There may be code out in the wild which relies on "path" being mutable.

--- a/test/uri/test_generic.rb
+++ b/test/uri/test_generic.rb
@@ -14,6 +14,13 @@ def uri_to_ary(uri)
     uri.class.component.collect {|c| uri.send(c)}
   end
 
+  def test_to_s
+    exp = 'http://example.com/'.freeze
+    str = URI(exp).to_s
+    assert_equal exp, str
+    refute_predicate str, :frozen?, '[ruby-core:71785] [Bug #11759]'
+  end
+
   def test_parse
     # 0
     assert_kind_of(URI::HTTP, @base_url)

...  So perhaps at least one additional change is needed:

--- a/lib/uri/generic.rb
+++ b/lib/uri/generic.rb
@@ -786,7 +786,7 @@ def check_path(v)
     # see also URI::Generic.path=
     #
     def set_path(v)
-      @path = v
+      @path = v.frozen? ? v.dup : v
     end
     protected :set_path
 

All these frozen literal changes will require going every single
method and and call site with a very fine tooth comb....

  reply	other threads:[~2015-12-03 23:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-11759.20151202000114@ruby-lang.org>
2015-12-02  0:01 ` [ruby-core:71785] [Ruby trunk - Bug #11759] [Open] URI breaks with frozen strings mperham
2015-12-02  1:18 ` [ruby-core:71786] [Ruby trunk - Bug #11759] " me
2015-12-02  1:30 ` [ruby-core:71787] " me
2015-12-02  1:47   ` [ruby-core:71788] " Eric Wong
2015-12-02  3:56 ` [ruby-core:71790] " me
2015-12-02  4:10 ` [ruby-core:71791] " akr
2015-12-02  7:11 ` [ruby-core:71797] " colin
2015-12-02 21:36   ` [ruby-core:71804] " Eric Wong
2015-12-03  5:20 ` [ruby-core:71812] " colin
2015-12-03 23:41   ` Eric Wong [this message]
2015-12-05 16:20 ` [ruby-core:71846] " colin
2015-12-07  7:19 ` [ruby-core:71879] " matz
2015-12-07 15:59 ` [ruby-core:71910] " colin
2015-12-08 21:35   ` [ruby-core:71957] " Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151203234103.GA30841@dcvr.yhbt.net \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).