From: maciej@mensfeld.pl
To: ruby-core@ruby-lang.org
Subject: [ruby-core:94349] [Ruby master Feature#16103] Make the dot-colon method reference frozen
Date: Wed, 14 Aug 2019 12:23:10 +0000 (UTC) [thread overview]
Message-ID: <redmine.issue-16103.20190814122309.712fa39aa09848b5@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16103.20190814122309@ruby-lang.org
Issue #16103 has been reported by maciej.mensfeld (Maciej Mensfeld).
----------------------------------------
Feature #16103: Make the dot-colon method reference frozen
https://bugs.ruby-lang.org/issues/16103
* Author: maciej.mensfeld (Maciej Mensfeld)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
I made a PR to freeze the dot-colon method reference result object (https://github.com/ruby/ruby/pull/2267). Nobu asked to make an issue out of that. I initially discussed that with Matz and Ko1 during the hack challenge in Bristol.
Here are some of the reasons why I think it should be done before releasing this feature:
- It's worth keeping the bound methods frozen as one should not modify them in general
- Freezing keeps a window of opportunity for introducing method reference caching in case it would be needed because the method object is immutable.
- I want to work on the "last method" reference cache for that feature when we see more use-cases after 2.7 is released and without having it frozen, the cost and complexity are probably higher than the outcome.
Example of the misuse that makes GC life much harder (note, that freezing does not fix that, but allows further work related to this issue):
```ruby
# frozen_string_literal: true
GC.disable
class Parse
def self.call(string)
string.to_f
end
end
class Normalize
def self.call(number)
number.round
end
end
class Transform
def self.call(int)
int * 2
end
end
# Simulate a long running data producing source with batch results
stream = Array.new(10_000) { Array.new(100) { '100.2' } }
before = GC.stat[:total_allocated_objects]
# This will provide batches to be processed, let's assume an endless data-source
stream.each do |batch|
batch
.map(&Parse.:call)
.map(&Normalize.:call)
.map(&Transform.:call)
end
after = GC.stat[:total_allocated_objects]
p a = after - before # 150001
before = GC.stat[:total_allocated_objects]
# "cache"
parse = Parse.:call
normalize = Normalize.:call
transform = Transform.:call
stream.each do |batch|
batch
.map(&parse)
.map(&normalize)
.map(&transform)
end
after = GC.stat[:total_allocated_objects]
p b = after - before # 120004
p "Difference: #{a - b}" # "Difference: 29997"
```
Things get even more "problematic" when referencing like above is not used on batches but on each of the objects separately:
```ruby
# frozen_string_literal: true
GC.disable
class Parse
def self.call(string)
string.to_f
end
end
class Normalize
def self.call(number)
number.round
end
end
class Transform
def self.call(int)
int * 2
end
end
# Simulate a long running data producing source with batch results
stream = Array.new(10_000) { Array.new(100) { '100.2' } }
before = GC.stat[:total_allocated_objects]
# This will provide batches to be processed, let's assume an endless data-source
stream.each do |batch|
batch.map do |message|
message
.then(&Parse.:call)
.then(&Normalize.:call)
.then(&Transform.:call)
end
end
after = GC.stat[:total_allocated_objects]
p a = after - before # 12010002
before = GC.stat[:total_allocated_objects]
# This will provide batches to be processed, let's assume an endless data-source
parse = Parse.:call
normalize = Normalize.:call
transform = Transform.:call
stream.each do |batch|
batch
.map(&parse)
.map(&normalize)
.map(&transform)
end
after = GC.stat[:total_allocated_objects]
p b = after - before # 120004
p "Difference: #{a - b}" # "Difference: 11889998"
```
I am aware, that this won't help in case we don't reuse the same object method references multiple times but based on many use-cases from here https://bugs.ruby-lang.org/issues/13581 and my own when building data-intense applications, it's not uncommon to build "pipelines" of things applied to each of the batches as a set of functions.
Apart from that I would also vote on freezing the `.method(:method)` result object as well, but I know Matz was against.
--
https://bugs.ruby-lang.org/
next parent reply other threads:[~2019-08-14 12:23 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-16103.20190814122309@ruby-lang.org>
2019-08-14 12:23 ` maciej [this message]
2019-08-29 7:37 ` [ruby-core:94658] [Ruby master Feature#16103] Make the dot-colon method reference frozen ko1
2019-08-29 7:37 ` [ruby-core:94659] " ko1
2019-11-05 21:16 ` [ruby-core:95709] " eregontp
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.issue-16103.20190814122309.712fa39aa09848b5@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).