Class: HeapProfiler::Parser::Ruby

Inherits:
Object
  • Object
show all
Defined in:
lib/heap_profiler/parser.rb

Instance Method Summary collapse

Instance Method Details

#build_index(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/heap_profiler/parser.rb', line 13

def build_index(path)
  require 'json'
  classes_index = {}
  classes_index.default_proc = CLASS_DEFAULT_PROC
  strings_index = {}

  File.open(path).each_line do |line|
    object = JSON.parse(line, symbolize_names: true)
    case object[:type]
    when 'MODULE', 'CLASS'
      address = parse_address(object[:address])

      name = object[:name]
      name ||= if object[:file] && object[:line]
        "<Class #{object[:file]}:#{object[:line]}>"
      end

      if name
        classes_index[address] = name
      end
    when 'STRING'
      next if object[:shared]
      if (value = object[:value])
        strings_index[parse_address(object[:address])] = value
      end
    end
  end

  [classes_index, strings_index]
end

#parse_address(address) ⇒ Object



44
45
46
# File 'lib/heap_profiler/parser.rb', line 44

def parse_address(address)
  address.to_i(16)
end