Backport #8143

[patch] forwardable: reduce allocations in def_delegators wrappers

Added by Aman Gupta almost 2 years ago. Updated almost 2 years 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"

  • FILE_REGEXP = %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
  • $@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s} unless Forwardable::debug
  • $@.delete_if{|s| Forwardable::FILE_REGEXP =~ s} unless Forwardable::debug ::Kernel::raise end end @@ -262,7 +263,7 @@ module SingleForwardable begin #{accessor}.__send__(:#{method}, *args, &block) rescue Exception
  • $@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s} unless Forwardable::debug
  • $@.delete_if{|s| Forwardable::FILE_REGEXP =~ s} unless Forwardable::debug ::Kernel::raise end end

Associated revisions

Revision 39875
Added by Nobuyoshi Nakada almost 2 years ago

forwardable.rb: reduce allocations in def_delegators

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

History

#1 Updated by Nobuyoshi Nakada almost 2 years 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::FILE_REGEXP): create regexp object outside sources for eval, to reduce allocations in def_delegators wrappers. //o option does not make each regexps shared. patch by tmm1 (Aman Gupta) in [Bug #8143].

#2 Updated by Aman Gupta almost 2 years 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 almost 2 years ago

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

Also available in: Atom PDF