Class: AllocationStats::Allocation

Inherits:
Object
  • Object
show all
Defined in:
lib/allocation_stats/allocation.rb

Constant Summary collapse

PWD =

a convenience constants

Dir.pwd
HELPERS =

a list of helper methods that Allocation provides on top of the object that was allocated.

[:class_plus, :gem]
ATTRIBUTES =

a list of attributes that Allocation has on itself; inquiries in this list should just use Allocation's attributes, rather than the internal object's.

[:sourcefile, :sourceline, :class_path, :method_id, :memsize]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Allocation



39
40
41
42
43
44
45
46
# File 'lib/allocation_stats/allocation.rb', line 39

def initialize(object)
  @object = object
  @memsize    = ObjectSpace.memsize_of(object)
  @sourcefile = ObjectSpace.allocation_sourcefile(object)
  @sourceline = ObjectSpace.allocation_sourceline(object)
  @class_path = ObjectSpace.allocation_class_path(object)
  @method_id  = ObjectSpace.allocation_method_id(object)
end

Instance Attribute Details

#class_pathObject (readonly)

the classpath of where the object was allocated



25
26
27
# File 'lib/allocation_stats/allocation.rb', line 25

def class_path
  @class_path
end

#memsizeObject

the memsize of the object which was allocated



21
22
23
# File 'lib/allocation_stats/allocation.rb', line 21

def memsize
  @memsize
end

#method_idObject (readonly)

the method ID of where the object was allocated



29
30
31
# File 'lib/allocation_stats/allocation.rb', line 29

def method_id
  @method_id
end

#objectObject (readonly)

the actual object that was allocated



33
34
35
# File 'lib/allocation_stats/allocation.rb', line 33

def object
  @object
end

#sourcelineObject (readonly) Also known as: line

the line in the sourcefile where the object was allocated



37
38
39
# File 'lib/allocation_stats/allocation.rb', line 37

def sourceline
  @sourceline
end

Instance Method Details

#as_jsonObject

Convert into a JSON string, which can be used in rack-allocation_stats's interactive mode.



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/allocation_stats/allocation.rb', line 109

def as_json
  {
    "memsize"      => @memsize,
    "class_path"   => @class_path,
    "method_id"    => @method_id,
    "file"         => sourcefile_alias,
    "file (raw)"   => @sourcefile,
    "line"         => @sourceline,
    "class"        => @object.class.name,
    "class_plus"   => class_plus
  }
end

#class_plusObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/allocation_stats/allocation.rb', line 80

def class_plus
  case @object
  when Array
    object_classes = element_classes(@object.map {|e| e.class }.uniq)
    if object_classes
      "Array<#{object_classes}>"
    else
      "Array"
    end
  else
    @object.class.name
  end
end

#fileObject



48
# File 'lib/allocation_stats/allocation.rb', line 48

def file; @sourcefile; end

#gemString?

Override Rubygems' Kernel#gem



98
99
100
101
102
103
104
105
# File 'lib/allocation_stats/allocation.rb', line 98

def gem
  gem_regex = /#{AllocationStats::GEMDIR}#{File::SEPARATOR}
    gems#{File::SEPARATOR}
    (?<gem_name>[^#{File::SEPARATOR}]+)#{File::SEPARATOR}
  /x
  match = gem_regex.match(sourcefile)
  match && match[:gem_name]
end

#sourcefile(alias_path = false) ⇒ Object

Either the full source file (via @sourcefile), or the aliased source file, via #sourcefile_alias



76
77
78
# File 'lib/allocation_stats/allocation.rb', line 76

def sourcefile(alias_path = false)
  alias_path ? sourcefile_alias : @sourcefile
end

#sourcefile_aliasObject

If the source file has recognized paths in it, those portions of the full path will be aliased like so:

  • the present work directory is aliased to ""
  • the Ruby lib directory (where the standard library lies) is aliased to ""
  • the Gem directory (where all gems lie) is aliased to ""


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/allocation_stats/allocation.rb', line 59

def sourcefile_alias
  case
  when @sourcefile[PWD]
    @sourcefile.sub(PWD, "<PWD>")
  when @sourcefile[AllocationStats::RUBYLIBDIR]
    @sourcefile.sub(AllocationStats::RUBYLIBDIR, "<RUBYLIBDIR>")
  when @sourcefile[AllocationStats::GEMDIR]
    @sourcefile.sub(/#{AllocationStats::GEMDIR}\/gems\/([^\/]+)\//, '<GEM:\1>/')
  else
    @sourcefile
  end
end

#to_json(*a) ⇒ Object



122
123
124
# File 'lib/allocation_stats/allocation.rb', line 122

def to_json(*a)
  as_json.to_json(*a)
end