Permalink
Browse files

caching dispatch lookup table

  • Loading branch information...
1 parent 70d101b commit deff06adef677992f89558403c42e6e0450ba105 @tenderlove tenderlove committed Feb 16, 2012
Showing with 21 additions and 11 deletions.
  1. +7 −4 lib/journey/gtg/transition_table.rb
  2. +5 −2 lib/journey/path/pattern.rb
  3. +5 −1 lib/journey/visitors.rb
  4. +4 −4 test/gtg/test_builder.rb
@@ -39,8 +39,7 @@ def eclosure t
end
def move t, a
- t = Array(t)
- move_string(t, a) + move_regexp(t, a)
+ move_string(t, a).concat move_regexp(t, a)
end
def to_json
@@ -137,12 +136,16 @@ def transitions
private
def move_regexp t, a
+ return [] if t.empty?
+
t.map { |s|
- @regexp_states[s].find_all { |re,_| re === a }.map(&:last)
- }.flatten.uniq
+ @regexp_states[s].map { |re,v| re === a ? v : nil }
+ }.flatten.compact.uniq
end
def move_string t, a
+ return [] if t.empty?
+
t.map { |s| @string_states[s][a] }.compact
end
end
@@ -25,7 +25,8 @@ def initialize strexp
@names = nil
@optional_names = nil
@required_names = nil
- @re = nil
+ @re = nil
+ @offsets = nil
end
def ast
@@ -182,8 +183,10 @@ def regexp_visitor
end
def offsets
+ return @offsets if @offsets
+
viz = RegexpOffsets.new @requirements
- viz.accept spec
+ @offsets = viz.accept spec
end
end
end
@@ -2,13 +2,17 @@
module Journey
module Visitors
class Visitor # :nodoc:
+ DISPATCH_CACHE = Hash.new { |h,k|
+ h[k] = "visit_#{k}"
+ }
+
def accept node
visit node
end
private
def visit node
- send "visit_#{node.type}", node
+ send DISPATCH_CACHE[node.type], node
end
def binary node
@@ -5,13 +5,13 @@ module GTG
class TestBuilder < MiniTest::Unit::TestCase
def test_following_states_multi
table = tt ['a|a']
- assert_equal 1, table.move(0, 'a').length
+ assert_equal 1, table.move([0], 'a').length
end
def test_following_states_multi_regexp
table = tt [':a|b']
- assert_equal 1, table.move(0, 'fooo').length
- assert_equal 2, table.move(0, 'b').length
+ assert_equal 1, table.move([0], 'fooo').length
+ assert_equal 2, table.move([0], 'b').length
end
def test_multi_path
@@ -22,7 +22,7 @@ def test_multi_path
[2, 'b'],
[2, '/'],
[1, 'c'],
- ].inject(0) { |state, (exp, sym)|
+ ].inject([0]) { |state, (exp, sym)|
new = table.move(state, sym)
assert_equal exp, new.length
new

0 comments on commit deff06a

Please sign in to comment.