lib/heap_profiler/parser.rb in heap-profiler-0.2.1 vs lib/heap_profiler/parser.rb in heap-profiler-0.3.0
- old
+ new
@@ -1,22 +1,32 @@
# frozen_string_literal: true
module HeapProfiler
module Parser
+ CLASS_DEFAULT_PROC = ->(_hash, key) { "<Class#0x#{key.to_s(16)}>" }
+
class Ruby
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'
- if (name = object[:name])
- classes_index[parse_address(object[:address])] = name
+ 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
@@ -33,10 +43,12 @@
class Native
DEFAULT_BATCH_SIZE = 10_000_000 # 10MB
def build_index(path, batch_size: DEFAULT_BATCH_SIZE)
- _build_index(path, batch_size)
+ indexes = _build_index(path, batch_size)
+ indexes.first.default_proc = CLASS_DEFAULT_PROC
+ indexes
end
def load_many(path, since: nil, batch_size: DEFAULT_BATCH_SIZE, &block)
_load_many(path, since, batch_size, &block)
end