Sha256: 3156f2f55e41262ef56998f84a2abee2ac6b48767ac04e627dfae5bf24412987

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module HeapProfiler
  module Parser
    class Ruby
      def build_index(path)
        require 'json'
        classes_index = {}
        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
            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
      DEFAULT_BATCH_SIZE = 10_000_000 # 10MB

      def build_index(path, batch_size: DEFAULT_BATCH_SIZE)
        _build_index(path, batch_size)
      end

      def load_many(path, since: nil, batch_size: DEFAULT_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

2 entries across 2 versions & 1 rubygems

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