class ObjectSpace::AllocationSampler::Result

Attributes

frames[R]
samples[R]

Public Class Methods

new(samples, frames) click to toggle source
# File lib/allocation_sampler.rb, line 69
def initialize samples, frames
  @samples = samples.sort_by! { |s| s[1] }.reverse!
  @frames = frames
end

Public Instance Methods

allocations_by_type() click to toggle source
# File lib/allocation_sampler.rb, line 74
def allocations_by_type
  @samples.each_with_object(Hash.new(0)) do |(type, count, _), h|
    h[type] += count
  end
end
allocations_with_top_frame() click to toggle source
# File lib/allocation_sampler.rb, line 80
def allocations_with_top_frame
  @samples.each_with_object({}) do |(type, count, stack), h|
    top_frame_id, line = stack.first
    frame = @frames[top_frame_id]
    ((h[type] ||= {})[frame.path] ||= {})[line] = count
  end
end
by_type_with_call_tree() click to toggle source
# File lib/allocation_sampler.rb, line 95
def by_type_with_call_tree
  types_with_stacks = @samples.group_by(&:first)
  types_with_stacks.transform_values do |stacks|
    frame_delegates = {}
    stacks.map { |_, count, stack|
      build_tree(stack, count, frame_delegates)
    }.uniq.first
  end
end
calltree() click to toggle source
# File lib/allocation_sampler.rb, line 88
def calltree
  frame_delegates = {}
  @samples.map { |type, count, stack|
    build_tree(stack, count, frame_delegates)
  }.uniq.first
end

Private Instance Methods

build_frame(id, line, samples) click to toggle source
# File lib/allocation_sampler.rb, line 128
def build_frame id, line, samples
  Frame.new @frames[id], line, samples
end
build_tree(stack, count, frame_delegates) click to toggle source
# File lib/allocation_sampler.rb, line 107
def build_tree stack, count, frame_delegates
  top_down = stack.reverse
  last_caller = nil
  seen = Set.new
  root = nil
  top_frame_id, top_line = stack.first
  top = frame_delegates[top_frame_id] ||= build_frame(top_frame_id, top_line, 0)
  top.samples += count
  top_down.each do |frame_id, line|
    frame = frame_delegates[frame_id] ||= build_frame(frame_id, line, 0)
    root ||= frame
    if last_caller
      last_caller.children << frame
    end
    last_caller = frame
    last_caller.total_samples += count unless seen.include?(frame_id)
    seen << frame_id
  end
  root
end