Class: Rinline::Optimizer

Inherits:
Object
  • Object
show all
Defined in:
lib/rinline/optimizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, method_name) ⇒ Optimizer

Returns a new instance of Optimizer.



14
15
16
17
18
# File 'lib/rinline/optimizer.rb', line 14

def initialize(klass, method_name)
  @klass = klass
  @method_name = method_name
  @method = klass.instance_method(method_name)
end

Class Method Details

.optimize(klass, method_name) ⇒ String?

Returns optimized code if optimized.

Parameters:

  • klass (Class)
  • method_name (Symbol)

    an instance method name

Returns:

  • (String, nil)

    optimized code if optimized



10
11
12
# File 'lib/rinline/optimizer.rb', line 10

def self.optimize(klass, method_name)
  self.new(klass, method_name).optimize
end

Instance Method Details

#optimizeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rinline/optimizer.rb', line 20

def optimize
  return unless method.ruby_method?
  return if method.eval?
  ast = method.to_ast
  return unless ast
  return if ast.has_child?(:CONST)
  path = method.absolute_path
  replacements = []

  ast.traverse do |node, opt|
    case node.type
    when :VCALL
      target_method_name = node.children[0]
      next if method_name == target_method_name
      target_method =
        begin
          klass.instance_method(target_method_name)
        rescue NameError
          next
        end
      next unless target_method.expandable?

      to_ast = target_method.to_ast
      next unless to_ast.expandable_method?(0)

      to_path = target_method.absolute_path
      body = to_ast.method_body
      to_code =
        if body
          "((#{replace_lvar(body, to_path)}))"
        else
          "()"
        end
      replacements << {
        from: node.location(path),
        to: to_code,
      }
    when :FCALL
      target_method_name = node.children[0]
      next if method_name == target_method_name
      target_method =
        begin
          klass.instance_method(target_method_name)
        rescue NameError
          next
        end
      next unless target_method.expandable?

      to_ast = target_method.to_ast
      args = node.fcall_args
      next unless args&.type == :ARRAY
      next unless to_ast.expandable_method?(args.array_size)

      to_path = target_method.absolute_path
      lvar_suffix = gen_lvar_suffix

      args = assign_args(to_ast, node, path, lvar_suffix)
      body =
        if to_ast.method_body
          replace_lvar(to_ast.method_body, to_path, lvar_suffix: lvar_suffix)
        else
          "()"
        end
      to_code = "((#{args}#{body}))"
      replacements << {
        from: node.location(path),
        to: to_code
      }
      opt[:ignore_index] = 1 # Ignore arguments
    end
  end

  return if replacements.empty?
  return replace(ast, path, replacements).force_encoding(Encoding::UTF_8) # TODO: Support other encodings
end