ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: 223-322-223-322@mail.ru
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90606] [Ruby trunk Feature#15393] Add compilation flags to freeze Array and Hash literals
Date: Tue, 18 Dec 2018 17:15:00 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-75765.20181218171459.a7976f8adbf032ca@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15393.20181208004237@ruby-lang.org

Issue #15393 has been updated by sergey-posokhov (Sergey Posokhov).

File frequency-1.rb added
File frequency-2.rb added

> I would like to add VM compilation options to freeze array and hash literals.  For example:

My two samples:

~~~ ruby
#!/usr/local/bin/ruby -w

def frequency(text)
  def prepare_string(text)
    text.gsub!(",", "")                            
        .split(" ")                              #
  end                                            # <-- freeze the result and return it
                                                 #
  def sort_frequency_report(fq)	
    fq.to_h
      .sort_by{|k, v| v }
      .reverse
      .to_h                                      #
  end                                            # <-- freeze the result and return it
                                                 #
  word_list = prepare_string(text)
  
  fq = Hash.new()
  
  for word in word_list
    if fq.has_key?(word)
      fq[word] += 1
    else
      fq[word] = 1
    end  
  end
                                                 #  
  sort_frequency_report(fq)                      # <-- freeze the result and return it
end                                              #

p frequency("text text text text
             text txet text text
             text text text")
~~~

And:

~~~ ruby
#!/usr/local/bin/ruby -w

def frequency(text)
  def prepare_string(text)
    text.gsub!(",", "")                            
        .split(" ")
  end

  def sort_frequency_report(fq)	
    #                                            #
    # fq = Hash.new()                            # Why?!.. is it correct?..
    #                                            #

    fq.to_h
      .sort_by{|k, v| v }
      .reverse
      .to_h
  end

  word_list = prepare_string(text)
  
  fq = Hash.new()
  
  for word in word_list
    if fq.has_key?(word)
      fq[word] += 1
    else
      fq[word] = 1
    end  
  end

  sort_frequency_report(fq)
end

p frequency("text text text text
             text txet text text
             text text text")
~~~


----------------------------------------
Feature #15393: Add compilation flags to freeze Array and Hash literals
https://bugs.ruby-lang.org/issues/15393#change-75765

* 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: #<ISeq:<compiled>@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: #<ISeq:<compiled>@thing.rb:0 (0,0)-(0,24)> (catch: FALSE)
0000 putobject                    "a"
0002 putself
0003 opt_send_without_block       <callinfo!mid:some_method, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
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--------------------------------
frequency-1.rb (1.06 KB)
frequency-2.rb (775 Bytes)


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

  parent reply	other threads:[~2018-12-18 17:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15393.20181208004237@ruby-lang.org>
2018-12-08  0:42 ` [ruby-core:90375] [Ruby trunk Feature#15393] Add compilation flags to freeze Array and Hash literals tenderlove
2018-12-08 13:58 ` [ruby-core:90379] " shevegen
2018-12-08 17:58 ` [ruby-core:90380] " eregontp
2018-12-10  0:55 ` [ruby-core:90387] " shyouhei
2018-12-10 13:07   ` [ruby-core:90404] " Benoit Daloze
2018-12-10 20:10 ` [ruby-core:90408] " tenderlove
2018-12-12 21:35 ` [ruby-core:90457] " shannonskipper
2018-12-13  8:47 ` [ruby-core:90500] " janfri26
2018-12-14 19:51 ` [ruby-core:90530] " nardonykolyszyn
2018-12-15 13:27 ` [ruby-core:90549] " eregontp
2018-12-18 17:08 ` [ruby-core:90605] " 223-322-223-322
2018-12-18 17:15 ` 223-322-223-322 [this message]
2019-01-15  0:15 ` [ruby-core:91092] " tenderlove

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=redmine.journal-75765.20181218171459.a7976f8adbf032ca@ruby-lang.org \
    --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).