Backport #8143

[patch] forwardable: reduce allocations in def_delegators wrappers

Added by Aman Gupta about 1 year ago. Updated 12 months ago.

[ruby-core:53620]
Status:Assigned
Priority:Normal
Assignee:Tomoyuki Chikanaga

Description

$ ./ruby -Ilib -rpp -rforwardable -e '
module Test
extend Forwardable
def_delegators :@test, *("a".."z")
end

GC.start
strings = ObjectSpace.each_object(String).select{ |s| s.frozen? && s =~ /forwardable/ }
p strings.size
pp strings.inject(Hash.new 0){ |h,s| h[s]+=1; h }
'

before:

73
[["/Users/test/code/ruby\\-trunk/lib/forwardable\\.rb", 26],
 ["/Users/test/code/ruby-trunk/lib/forwardable.rb", 33]]

after:

46
[["/Users/test/code/ruby-trunk/lib/forwardable.rb", 31],
 ["/Users/test/code/ruby-trunk/lib/forwardable", 1]]

diff --git a/lib/forwardable.rb b/lib/forwardable.rb
index d7116ed..2bd67f6 100644
--- a/lib/forwardable.rb
+++ b/lib/forwardable.rb
@@ -102,6 +102,7 @@ module Forwardable
# Version of +forwardable.rb+
FORWARDABLE_VERSION = "1.1.0"

  • FILEREGEXP = %r"#{Regexp.quote(FILE)}"o @debug = nil class << self # If true, FILE will remain in the backtrace in the event an @@ -170,7 +171,7 @@ module Forwardable begin #{accessor}.send_(:#{method}, *args, &block) rescue Exception
  • $@.deleteif{|s| %r"#{Regexp.quote(FILE_)}"o =~ s} unless Forwardable::debug
  • $@.deleteif{|s| Forwardable::FILEREGEXP =~ s} unless Forwardable::debug ::Kernel::raise end end @@ -262,7 +263,7 @@ module SingleForwardable begin #{accessor}.send(:#{method}, *args, &block) rescue Exception
  • $@.deleteif{|s| %r"#{Regexp.quote(FILE_)}"o =~ s} unless Forwardable::debug
  • $@.deleteif{|s| Forwardable::FILEREGEXP =~ s} unless Forwardable::debug ::Kernel::raise end end

History

#1 Updated by Nobuyoshi Nakada about 1 year ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r39875.
Aman, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


forwardable.rb: reduce allocations in def_delegators

  • lib/forwardable.rb (Forwardable::FILEREGEXP): create regexp object outside sources for eval, to reduce allocations in defdelegators wrappers. //o option does not make each regexps shared. patch by tmm1 (Aman Gupta) in [Bug #8143].

#2 Updated by Aman Gupta 12 months ago

  • Tracker changed from Bug to Backport
  • Project changed from ruby-trunk to Backport200
  • Status changed from Closed to Open

#3 Updated by Aman Gupta 12 months ago

  • Status changed from Open to Assigned
  • Assignee set to Tomoyuki Chikanaga
  • % Done changed from 100 to 0

Also available in: Atom PDF