[#70257] [Ruby trunk - Feature #11420] [Open] Introduce ID key table into MRI — ko1@...

Issue #11420 has been reported by Koichi Sasada.

11 messages 2015/08/06

[ruby-core:70541] [Ruby trunk - Feature #8976] file-scope freeze_string directive

From: merch-redmine@...
Date: 2015-08-22 01:22:44 UTC
List: ruby-core #70541
Issue #8976 has been updated by Jeremy Evans.


Colin Kelley wrote:
> Akira Tanaka wrote:
> 
> > We can't update all libraries at once.
> > There are too many libraries and authors.
> > Some libraries will be updated soon and other libraries don't.
> 
> I completely concur.  We will need to work through many projects and libraries and gems to make this change.  The comment directive is perfect for that because we can mark files/projects/gems that have been converted and tested while not disrupting others.  This is exactly how we managed the transition to utf-8 and that went very smoothly.

Adding support for this magic comment will ensure that this problem will remain a problem far in the future.  We should encourage people to fix the problem, not sweep the problem under the rug.

This is not like the encoding magic comment, which is necessary because the ruby source file could be in any encoding.  In general, frozen string literal issues are easy to fix, unlike encoding issues. I'd guess that 80% of exceptions caused by this change will be on the line after the string literal.

No library should be mutating a string they don't own as a side effect, and this change will make it much easier to detect libraries that do.  Adding a magic comment to such a library will make it more likely that the problem isn't discovered even after ruby 3.0 is released.  We should not encourage behavior that will hide bugs.

Any time spent adding magic comments to existing libraries would be better spent just making sure that libraries work with frozen string literals.  Assuming we have at least 2 years between 2.3 and 3.0, that should be enough time to test gems for compatibility with --enable-frozen-string-literal before such behavior becomes the default.

It needs to be painful to use libraries that don't support frozen string literals, in order to encourage people to fix those libraries.  For those people who must use libraries that still don't support frozen string literals when ruby 3 is released, they can use --disable-frozen-string-literals and be no worse off than they are now.

----------------------------------------
Feature #8976: file-scope freeze_string directive
https://bugs.ruby-lang.org/issues/8976#change-53948

* Author: Akira Tanaka
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Yesterday, we had a face-to-face developer meeting.
https://bugs.ruby-lang.org/projects/ruby/wiki/DevelopersMeeting20131001Japan
Several committers attended.
matz didn't attended, though.  (This means this issue is not concluded.)

We believe we found a better way to freeze static string literals for
less GC pressure.
"static string literal" is a string literal without dynamic expression.

Currently, f-suffix, "..."f, is used to freeze a string literal to avoid
String object allocation.

There are several problems for f-suffix:

* The notation is ugly.
* Syntax error on Ruby 2.0.
  We cannot use the feature in version independent libraries.
  So, it is difficult to deploy.
* Need to modify for each string literal.
  This is cumbersome.

The new way we found is a file-scope directive as follows

  # freeze_string: true

The above comment at top of a file changes semantics of
static string literals in the file.
The static string literals will be frozen and always returns same object.
(The semantics of dynamic string literals is not changed.)

This way has following benefits:

* No ugly f-suffix.
* No syntax error on older Ruby.
* We need only a line for each file.

We can write version independent library using frozen static string literals as follows.

* Use the directive at top of the file: # freeze_string: true
  Older Ruby ignore this as a comment.
* Use "...".dup for strings to be modified.
  Older Ruby has small disadvantage: useless dup is called.

Note that the directive effects all static string literals regardless of
single quotes, double quotes, %q-string, %qq-string and here documents.
The reason that the directive is effective not only single quotes is
we want to use escape sequences such as \n in frozen string literals.

Also note that similar directive is already exist:

  % ruby -w -e '
  def m
    end
  '
  -e:3: warning: mismatched indentations at 'end' with 'def' at 2
  % ruby -w -e '# -*- warn_indent: false -*-
  def m
    end
  '

The directive, warn_indent: false, disables "mismatched indentations" warning.

nobu implemented this feature in the meeting.
Please attach the patch, nobu.




-- 
https://bugs.ruby-lang.org/

In This Thread