Sha256: 3657d11258071a987f4369246f77c111fcc2299b62f7d771ce22dbf51db4eacc

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module HeapProfiler
  module Parser
    CLASS_DEFAULT_PROC = ->(_hash, key) { "<Class#0x#{key.to_s(16)}>" }

    class << self
      attr_accessor :batch_size
    end
    self.batch_size = 10_000_000 # 10MB

    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'
            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

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

    class Native
      def build_index(path, batch_size: Parser.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: Parser.batch_size, &block)
        _load_many(path, since, batch_size, &block)
      end
    end

    class << self
      def build_index(path)
        current.build_index(path)
      end

      def load_many(path, **kwargs, &block)
        current.load_many(path, **kwargs, &block)
      end

      private

      def current
        Thread.current[:HeapProfilerParser] ||= Native.new
      end
    end
  end
  require "heap_profiler/heap_profiler"
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
heap-profiler-0.7.0 lib/heap_profiler/parser.rb
heap-profiler-0.6.0 lib/heap_profiler/parser.rb
heap-profiler-0.5.0 lib/heap_profiler/parser.rb
heap-profiler-0.4.0 lib/heap_profiler/parser.rb