Class: Cyrel::AST::HybridApproach

Inherits:
Object
  • Object
show all
Defined in:
lib/cyrel/ast/optimized_nodes.rb

Overview

Example of how to use Data nodes effectively

Class Method Summary collapse

Class Method Details

.benchmark_hybridObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cyrel/ast/optimized_nodes.rb', line 87

def self.benchmark_hybrid
  require 'benchmark'

  n = 10_000
  puts "\nHybrid Approach Benchmark:"
  puts '-' * 40

  Benchmark.bm(35) do |x|
    x.report('Create literals (Class)') do
      n.times { |i| LiteralNode.new(i) }
    end

    x.report('Create literals (Data)') do
      n.times { |i| OptimizedNodes::LiteralData.new(i) }
    end

    # Cache performance with Data nodes
    cache = OptimizedCache.instance
    literals = 100.times.map { |i| OptimizedNodes::LiteralData.new(i) }

    x.report('Cache lookups (Data as key)') do
      n.times do |i|
        node = literals[i % 100]
        cache.fetch(node) { "value_#{i}" }
      end
    end
  end
end

.create_literal(value) ⇒ Object

Use Data for simple, frequently created nodes Use Classes for complex nodes with behavior



77
78
79
80
# File 'lib/cyrel/ast/optimized_nodes.rb', line 77

def self.create_literal(value)
  # Literals are perfect for Data - immutable, simple
  OptimizedNodes::LiteralData.new(value)
end

.create_match(pattern, optional: false) ⇒ Object



82
83
84
85
# File 'lib/cyrel/ast/optimized_nodes.rb', line 82

def self.create_match(pattern, optional: false)
  # Complex nodes with optional parameters stay as classes
  MatchNode.new(pattern, optional: optional)
end