Created
May 26, 2020 22:50
-
-
Save tenderlove/d4ed807448f02cb27a4bd135f5813b7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Method tables and constant tables are lazily allocated. When a module is | |
# mixed in to a class, it creates an ICLASS which is the object used to | |
# represent the module in the inheritance hierarchy. The ICLASS object shares | |
# some tables with the source module, so when it gets created Ruby will | |
# allocate these tables to share even if they didn't exist previously. | |
# | |
# You can see the tables get allocated in this function: | |
# https://github.com/ruby/ruby/blob/962c302a1ae8e50738c36adb61c8ec9c9fa5a49b/class.c#L832-L862 | |
# | |
# Output on my machine: | |
# | |
# [aaron@tc-lan-adapter ~/g/ruby (auto-compact)]$ ./ruby -v x.rb | |
# ruby 2.8.0dev (2020-05-22T14:54:34Z master 8d798e7c53) [x86_64-darwin19] | |
# Memsize before inclusion | |
# {:Maximal=>168} | |
# {:None=>560} | |
# {:Partial=>472} | |
# ######### | |
# Memsize after inclusion | |
# {:Maximal=>496} | |
# {:None=>560} | |
# {:Partial=>496} | |
# [aaron@tc-lan-adapter ~/g/ruby (master)]$ | |
require "objspace" | |
class Bar | |
# Maximal laziness | |
maximal = Module.new { } | |
# No laziness | |
module None | |
BAR = 10 | |
end | |
# Partial laziness | |
module Partial | |
end | |
puts "Memsize before inclusion" | |
p Maximal: ObjectSpace.memsize_of(maximal) | |
p None: ObjectSpace.memsize_of(None) | |
p Partial: ObjectSpace.memsize_of(Partial) | |
puts "#########" | |
include maximal | |
include None | |
include Partial | |
puts "Memsize after inclusion" | |
p Maximal: ObjectSpace.memsize_of(maximal) | |
p None: ObjectSpace.memsize_of(None) | |
p Partial: ObjectSpace.memsize_of(Partial) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment