Class: AllocationStats::Allocation

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

Overview

Information about an individual allocation is captured in this class.

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

Returns a new instance of Allocation.



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

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



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

def class_path
  @class_path
end

#memsizeObject

the memsize of the object which was allocated



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

def memsize
  @memsize
end

#method_idObject (readonly)

the method ID of where the object was allocated



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

def method_id
  @method_id
end

#objectObject (readonly)

the actual object that was allocated



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

def object
  @object
end

#sourcelineObject (readonly) Also known as: line

the line in the sourcefile where the object was allocated



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

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.



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/allocation_stats/allocation.rb', line 120

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

Returns class name, plus, for Arrays, extended information. When all of the elements of the Array are instances of a total of three or fewer classes, then those classes are listed in brackets. For example:

Examples:

Array with only Fixnum and Bignum elements

allocation.class_plus  #=> "Array<Fixnum,Bignum>"

Array with elements of class A, B, C, and D

allocation.class_plus  #=> "Array"

String (not an Array)

allocation.class_plus  #=> "String"


91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/allocation_stats/allocation.rb', line 91

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



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

def file; @sourcefile; end

#gemString?

Override Rubygems' Kernel#gem

Returns:

  • (String)

    the name of the Rubygem where this allocation occurred.

  • (nil)

    if this allocation did not occur in a Rubygem.



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

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

Parameters:

  • alias_path (TrueClass) (defaults to: false)

    whether or not to alias the path



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

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 ""

Returns:

  • the source file, aliased.



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

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

#to_json(*a) ⇒ Object

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



135
136
137
# File 'lib/allocation_stats/allocation.rb', line 135

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