From: "headius (Charles Nutter)" Date: 2013-10-03T19:49:13+09:00 Subject: [ruby-core:57637] [ruby-trunk - Feature #8976] file-scope freeze_string directive Issue #8976 has been updated by headius (Charles Nutter). duerst (Martin D��rst) wrote: > From a more general perspective, it feels to me that introducing all > these frozen options will increase performance, but at the cost of > programmer effort. That would be the case also e.g. for something like > type hints,..., but that's not Ruby style. Personally, I think the more important benefit of having instantly-frozen literal strings, arrays, and hashes is for safer concurrency and data integrity. * If I pass you a reference to a string, I can create that string frozen and be sure you don't modify it. Same goes for arrays and hashes. * If I am initializing a global array or hash that should never be modified, I can create it frozen immediately. Of course these can all be done by calling .freeze on the object as well, but creating immutable structures right away avoids any mistakes. And then the potential VM optimizations are a bonus on top of that. But I stand by my opinion that a global pragma for frozen literal strings is a bad idea, because it makes all literal strings in the file start raising errors for half their methods, and it makes it impossible to copy/paste from one file to another without code potentially breaking. ---------------------------------------- Feature #8976: file-scope freeze_string directive https://bugs.ruby-lang.org/issues/8976#change-42251 Author: akr (Akira Tanaka) Status: Open Priority: Normal Assignee: Category: Target version: current: 2.1.0 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. -- http://bugs.ruby-lang.org/