Sha256: d987114ccaba521eeffd52787fe1f4b05e926be143aaed742749919af17762f3

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module HeapProfiler
  class Index
    def initialize(heap)
      @heap = heap
      @classes = {}
      @strings = {}
      @gems = {}
      build!
    end

    def build!
      @classes, @strings = Parser.build_index(@heap.path)
      self
    end

    BUILTIN_CLASSES = {
      FILE: "File",
      ICLASS: "ICLASS",
      COMPLEX: "Complex",
      RATIONAL: "Rational",
      BIGNUM: "Bignum",
      FLOAT: "Float",
      ARRAY: "Array",
      STRING: "String",
      HASH: "Hash",
      SYMBOL: "Symbol",
      MODULE: "Module",
      CLASS: "Class",
      REGEXP: "Regexp",
      MATCH: "MatchData",
      ROOT: "<VM Root>",
    }.freeze

    IMEMO_TYPES = Hash.new { |h, k| h[k] = "<#{k || 'unknown'}> (IMEMO)" }
    DATA_TYPES = Hash.new { |h, k| h[k] = "<#{(k || 'unknown')}> (DATA)" }

    def guess_class(object)
      type = object[:type]
      if (class_name = BUILTIN_CLASSES[type])
        return class_name
      end

      return IMEMO_TYPES[object[:imemo_type]] if type == :IMEMO

      class_address = object[:class]
      return unless class_address

      @classes.fetch(class_address) do
        return DATA_TYPES[object[:struct]] if type == :DATA

        $stderr.puts("WARNING: Couldn't infer class name of: #{object.inspect}")
        nil
      end
    end

    def string_value(object)
      value = object[:value]
      return value if value

      if object[:shared]
        @strings[Native.parse_address(object[:references].first)]
      end
    end

    def guess_gem(object)
      path = object[:file]
      @gems[path] ||=
        if %r{(/gems/.*)*/gems/(?<gemname>[^/]+)} =~ path
          gemname
        elsif %r{/rubygems[\./]}.match?(path)
          "rubygems"
        elsif %r{ruby/2\.[^/]+/(?<stdlib>[^/\.]+)} =~ path
          stdlib
        elsif %r{(?<app>[^/]+/(bin|app|lib))} =~ path
          app
        else
          "other"
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
heap-profiler-0.2.1 lib/heap_profiler/index.rb
heap-profiler-0.2.0 lib/heap_profiler/index.rb