Method: MemoryProfiler::Results#string_report

Defined in:
lib/memory_profiler/results.rb

#string_report(data, top) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/memory_profiler/results.rb', line 85

def string_report(data, top)
  grouped_strings = Hash.new { |hash, key| hash[key] = [] }
  data.each_value do |stat|
    if stat.string_value
      grouped_strings[stat.string_value.object_id] << stat
    end
  end

  grouped_strings = grouped_strings.values

  if grouped_strings.size > top
    grouped_strings.sort_by!(&:size)
    grouped_strings = grouped_strings.drop(grouped_strings.size - top)
  end

  grouped_strings
    .sort! { |a, b| a.size == b.size ? a[0].string_value <=> b[0].string_value : b.size <=> a.size }
    .map! do |list|
      # Return array of [string, [[location, count], [location, count], ...]
      [
        list[0].string_value,
        list.group_by { |stat| stat.location }
          .map { |location, stat_list| [location, stat_list.size] }
          .sort_by!(&:last)
          .reverse!
      ]
    end
end