Class: Hone::Patterns::KernelLoop

Inherits:
Base
  • Object
show all
Defined in:
lib/hone/patterns/kernel_loop.rb

Overview

Pattern: Kernel#loop { break if ... } -> while true ... end

Kernel#loop has method call overhead vs the while construct.

From sqids-ruby commit 8a74142

Instance Attribute Summary

Attributes inherited from Base

#findings

Instance Method Summary collapse

Methods inherited from Base

#add_finding, inherited, #initialize, scan_file

Constructor Details

This class inherits a constructor from Hone::Patterns::Base

Instance Method Details

#visit_call_node(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hone/patterns/kernel_loop.rb', line 14

def visit_call_node(node)
  super
  # Look for: loop { ... } (implicit receiver, block present)
  return unless node.name == :loop && node.receiver.nil? && node.block.is_a?(Prism::BlockNode)

  add_finding(
    node,
    message: "Use `while true ... end` instead of `loop { }` in hot paths to avoid method call overhead",
    speedup: "Minor, but adds up in tight loops"
  )
end