Slide 1

Slide 1 text

Pattern Matching

Slide 2

Slide 2 text

omg

Slide 3

Slide 3 text

Steak Holder

Slide 4

Slide 4 text

Aaron Patterson

Slide 5

Slide 5 text

@tenderlove

Slide 6

Slide 6 text

Ruby core team

Slide 7

Slide 7 text

Rails core team

Slide 8

Slide 8 text

! WARNING !

Slide 9

Slide 9 text

Stage II: Advanced Beginner

Slide 10

Slide 10 text

0. Excited to be in England 1. English country, I have accent. 2. Fear and guilt 3. Fear of plugs and outlets 4. Can't stop thinking of strange 5. Romance Ebi

Slide 11

Slide 11 text

Manchester Agile Rails

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

! WARNING !

Slide 17

Slide 17 text

Graphviz http://graphviz.org

Slide 18

Slide 18 text

world hello greeting

Slide 19

Slide 19 text

digraph nfa { rankdir=LR; world [shape = doublecircle]; hello [shape = circle]; hello -> world [label="greeting"]; } graph.dot

Slide 20

Slide 20 text

digraph nfa { rankdir=LR; world [shape = doublecircle]; hello [shape = circle]; goodbye [shape = circle]; hello -> world [label="greeting"]; goodbye -> world } graph.dot

Slide 21

Slide 21 text

world hello greeting goodbye

Slide 22

Slide 22 text

2 3 . 4 / /articles(.:format) 5 6 /articles/new(.:format) 0 1 / articles (?-mix:[^./?]+) new

Slide 23

Slide 23 text

Pattern Matching Move between theory and practice

Slide 24

Slide 24 text

Journey Yes, it's named after the '70s rock sensation

Slide 25

Slide 25 text

What is a router? Provides 1: URL generation 2: Path Recognition 3: Route parsing

Slide 26

Slide 26 text

Why a new router?

Slide 27

Slide 27 text

Maintenance

Slide 28

Slide 28 text

0 700 1400 2100 2800 Journey Journey-2 Rack-Mount LOC

Slide 29

Slide 29 text

Known algorithms

Slide 30

Slide 30 text

References • Compilers: Principles, Techniques, & Tools (Aho, Lam, Sethi, Ullman) • Intro to Formal Languages and Automata (Linz)

Slide 31

Slide 31 text

Predictability

Slide 32

Slide 32 text

CPU Time

Slide 33

Slide 33 text

Memory Usage

Slide 34

Slide 34 text

Current Performance url generation path recognition route parsing rack-mount Journey

Slide 35

Slide 35 text

Patterns

Slide 36

Slide 36 text

Why patterns matter

Slide 37

Slide 37 text

Regular Expressions /om(g)!/

Slide 38

Slide 38 text

Rails Routes

Slide 39

Slide 39 text

resource :articles

Slide 40

Slide 40 text

/articles(.:format) /articles/new(.:format) /articles/:id/edit(.:format) /articles/:id(.:format)

Slide 41

Slide 41 text

Parens don't capture

Slide 42

Slide 42 text

:whatever => /([^./?]+)/

Slide 43

Slide 43 text

/articles(.:format) /\/articles(?:\.([^.\/?]+))?$/

Slide 44

Slide 44 text

/\/articles(?:.([^.\/?]+))?$/ /\/articles\/new(?:.([^.\/?]+))?$/ /\/articles\/([^.\/?]+)\/edit(?:.([^.\/?]+))?$/ /\/articles\/([^.\/?]+)(?:.([^.\/?]+))?$/ GET /articles/new 200 OK!

Slide 45

Slide 45 text

/\/articles(?:.([^.\/?]+))?$/ /\/articles\/new(?:.([^.\/?]+))?$/ /\/articles\/([^.\/?]+)\/edit(?:.([^.\/?]+))?$/ /\/articles\/([^.\/?]+)(?:.([^.\/?]+))?$/ GET /foos/new 404 Not Found

Slide 46

Slide 46 text

How long does this take?

Slide 47

Slide 47 text

r = routes.length

Slide 48

Slide 48 text

x = regexp compare

Slide 49

Slide 49 text

O(r ⨉ x)

Slide 50

Slide 50 text

Can we do better?

Slide 51

Slide 51 text

Finite State Machines Parse Trees Automata

Slide 52

Slide 52 text

Parse Trees

Slide 53

Slide 53 text

Parsing regexp

Slide 54

Slide 54 text

(a|b)*abb

Slide 55

Slide 55 text

○ ○ b ○ b a * a b () | Describe node types

Slide 56

Slide 56 text

Parsing rails routes

Slide 57

Slide 57 text

Journey::Parser parser = Journey::Parser.new ast = parser.parse '/articles(.:format)' puts ast.to_dot

Slide 58

Slide 58 text

○ ○ () / articles ○ . :format

Slide 59

Slide 59 text

To String! parser = Journey::Parser.new ast = parser.parse '/articles(.:format)' puts ast.to_s puts ast.to_regexp

Slide 60

Slide 60 text

OR AST parser = Journey::Parser.new trees = [ '/articles(.:format)', '/articles/new(.:format)', ].map { |s| parser.parse s } ast = Journey::Nodes::Or.new trees puts ast.to_dot

Slide 61

Slide 61 text

| ○ ○ ○ () / articles ○ . :format ○ () ○ new ○ / / articles ○ . :format

Slide 62

Slide 62 text

SECRET FEATURE parser = Journey::Parser.new ast = parser.parse '/articles|books(.:format)' SUPER SECRET!

Slide 63

Slide 63 text

Automata

Slide 64

Slide 64 text

(a|b)*abb

Slide 65

Slide 65 text

3 0 b 2 a b 1 a a b b a Double circle is acceptance state

Slide 66

Slide 66 text

baabb

Slide 67

Slide 67 text

3 0 b 2 a b 1 a a b b a

Slide 68

Slide 68 text

3 0 b 2 a b 1 a a b b a b

Slide 69

Slide 69 text

3 0 b 2 a b 1 a a b b a ba

Slide 70

Slide 70 text

3 0 b 2 a b 1 a a b b a baa

Slide 71

Slide 71 text

3 0 b 2 a b 1 a a b b a baabb

Slide 72

Slide 72 text

aab 3 0 b 2 a b 1 a a b b a

Slide 73

Slide 73 text

Deterministic Finite Automaton

Slide 74

Slide 74 text

Only one path

Slide 75

Slide 75 text

Storage class DFA attr_reader :table def initialize @table = Hash.new { |h, from| h[from] = {} } @table[0]['a'] = 1 @table[0]['b'] = 0 @table[1]['a'] = 1 @table[1]['b'] = 2 @table[2]['a'] = 1 @table[2]['b'] = 3 @table[3]['b'] = 0 @table[3]['a'] = 2 end end

Slide 76

Slide 76 text

FSM Simulation

Slide 77

Slide 77 text

class Simulator def initialize(dfa) @dfa = dfa end def simulate(symbols) state = 0 until symbols.empty? state = @dfa.move(state, symbols.shift) end state end end

Slide 78

Slide 78 text

class DFA ... def move(from, symbol) @table[from][symbol] end end move function

Slide 79

Slide 79 text

irb> sim = Simulator.new(DFA.new) => # irb> sim.simulate %w{ b a a b b } => 3 irb> sim.simulate %w{ a a b } => 2

Slide 80

Slide 80 text

Time: O(n) n = string.length

Slide 81

Slide 81 text

Space: S + T states.length + transitions.length

Slide 82

Slide 82 text

Nondeterministic Finite Automaton

Slide 83

Slide 83 text

Has nil edges

Slide 84

Slide 84 text

Can't tell direction

Slide 85

Slide 85 text

Simulation of NFA

Slide 86

Slide 86 text

6 0 2 ε 4 ε 3 a 5 b ε ε a|b

Slide 87

Slide 87 text

6 0 2 ε 4 ε 3 a 5 b ε ε nil-closure

Slide 88

Slide 88 text

6 0 2 ε 4 ε 3 a ε 5 b ε a

Slide 89

Slide 89 text

Storage class NFA def initialize @table = Hash.new { |h, from| h[from] = Hash.new { |i,sym| i[sym] = [] } } @table[0][nil] << 2 @table[0][nil] << 4 @table[2]['a'] << 3 @table[4]['b'] << 5 @table[3][nil] << 6 @table[5][nil] << 6 end def nil_closure(states) states.map { |s| @table[s][nil] }.flatten end end

Slide 90

Slide 90 text

FSM Simulation class Simulator def initialize(nfa) @nfa = nfa end def simulate(symbols) states = @nfa.nil_closure([0]) until symbols.empty? next_s = @nfa.move(states, symbols.shift) states = @nfa.nil_closure(next_s) end states end end

Slide 91

Slide 91 text

Move function class NFA ... def move(states, symbol) states.map { |s| @table[s][symbol] }.flatten end end

Slide 92

Slide 92 text

irb> sim = Simulator.new(NFA.new) => # irb> sim.simulate %w{ a } => [6] irb> sim.simulate %w{ b } => [6] irb> sim.simulate %w{ b b } => [] irb> sim.simulate %w{ c } => []

Slide 93

Slide 93 text

Time: O(r ⨉ x) r = operators.length, x = string.length

Slide 94

Slide 94 text

NFA Construction

Slide 95

Slide 95 text

○ ○ () / articles ○ . :format /articles(.:format)

Slide 96

Slide 96 text

cat nodes 1 0 /

Slide 97

Slide 97 text

/articles 2 0 1 / articles

Slide 98

Slide 98 text

Optional 3 0 ε 1 ε 2 ????? ε

Slide 99

Slide 99 text

6 0 1 / 2 articles ε 3 ε 4 . 5 :format ε

Slide 100

Slide 100 text

parser = Journey::Parser.new ast = parser.parse '/articles(.:format)' nfa = Journey::NFA::Builder.new ast tt = nfa.transition_table puts tt.to_dot Journey::NFA

Slide 101

Slide 101 text

Converting NFA to DFA

Slide 102

Slide 102 text

Eliminate nil transitions

Slide 103

Slide 103 text

Collapse duplicate edges

Slide 104

Slide 104 text

6 0 1 / 2 articles ε 3 ε 4 . 5 :format ε /articles(.:format)

Slide 105

Slide 105 text

2 3 . 4 0 1 / articles :format /articles(.:format)

Slide 106

Slide 106 text

CODES! parser = Journey::Parser.new ast = parser.parse '/articles(.:format)' nfa = Journey::NFA::Builder.new ast tt = nfa.transition_table.generalized_table puts tt.to_dot

Slide 107

Slide 107 text

SHORTER CODES! parser = Journey::Parser.new ast = parser.parse '/articles(.:format)' dfa = Journey::GTG::Builder.new ast tt = dfa.transition_table puts tt.to_dot

Slide 108

Slide 108 text

ANY NFA converts to DFA

Slide 109

Slide 109 text

O(r ⨉ x) => O(x) r = operations.length, x = string.length

Slide 110

Slide 110 text

Converting Automata to Regexp

Slide 111

Slide 111 text

2 3 . 4 0 1 / articles :format /articles(.:format)

Slide 112

Slide 112 text

2 3 . 4 0 /articles :format /articles(.:format)

Slide 113

Slide 113 text

/articles(.:format) 2 4 .:format 0 /articles

Slide 114

Slide 114 text

/articles(.:format) 4 0 /articles(.:format)?

Slide 115

Slide 115 text

Generalized Transition Graph

Slide 116

Slide 116 text

/users(.:format) /users/new(.:format) /users/:id/edit(.:format) /users/:id(.:format) resource :users

Slide 117

Slide 117 text

2 3 . 4 / 5 6 8 . 7 9 / 10 . 11 12 14 . 13 15 0 1 / users (?-mix:[^./?]+) new (?-mix:[^./?]+) (?-mix:[^./?]+) edit (?-mix:[^./?]+) (?-mix:[^./?]+) resource :users

Slide 118

Slide 118 text

The Plan?

Slide 119

Slide 119 text

Combine All Routes

Slide 120

Slide 120 text

Produce DFA

Slide 121

Slide 121 text

Simulate in O(n) Time

Slide 122

Slide 122 text

The Future

Slide 123

Slide 123 text

JS Simulation

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

Rails Console irb> File.open('out.html', 'wb') { |f| irb* f.write( irb* Wot::Application.routes.router.visualizer irb> )} => 69074

Slide 126

Slide 126 text

Table => JSON parser = Journey::Parser.new ast = parser.parse '/articles(.:format)' dfa = Journey::GTG::Builder.new ast tt = dfa.transition_table puts tt.to_json

Slide 127

Slide 127 text

Table => SVG parser = Journey::Parser.new ast = parser.parse '/articles(.:format)' dfa = Journey::GTG::Builder.new ast tt = dfa.transition_table puts tt.to_svg

Slide 128

Slide 128 text

JS Tokenizer function tokenize(input, callback) { while(input.length > 0) { callback(input.match(/^[\/\.\?]|[^\/\.\?]+/)[0]); input = input.replace(/^[\/\.\?]|[^\/\.\?]+/, ''); } }

Slide 129

Slide 129 text

JS Simulator tokenize(input, function(token) { var new_states = []; for(var key in states) { var state = states[key]; if(string_states[state] && string_states[state][token]) { var new_state = string_states[state][token]; highlight_edge(state, new_state); highlight_state(new_state); new_states.push(new_state); } } if(new_states.length == 0) { return; } states = new_states; });

Slide 130

Slide 130 text

routes.rb marshalling

Slide 131

Slide 131 text

Test suggestions

Slide 132

Slide 132 text

Test coverage

Slide 133

Slide 133 text

Usage Heat Maps

Slide 134

Slide 134 text

????

Slide 135

Slide 135 text

Open Questions

Slide 136

Slide 136 text

Is our GTG deterministic?

Slide 137

Slide 137 text

/users/new|/users/:id 4 5 0 1 / 2 users 3 / new (?-mix:[^./?]+)

Slide 138

Slide 138 text

"new" =~ /new|[^.\/?]+/

Slide 139

Slide 139 text

Can we make it deterministic?

Slide 140

Slide 140 text

L1 = {new} L2 = {[^./?]+}

Slide 141

Slide 141 text

/users/new|/users/:id 4 5 0 1 / 2 users 3 / L1 L2 - L1

Slide 142

Slide 142 text

Is it worth our effort?

Slide 143

Slide 143 text

REGEX AST NFA DFA

Slide 144

Slide 144 text

DFA => YACC

Slide 145

Slide 145 text

DFA => RACC

Slide 146

Slide 146 text

DFA => Ragel

Slide 147

Slide 147 text

Is it worth our effort?

Slide 148

Slide 148 text

Thanks Tatsuya Ono!

Slide 149

Slide 149 text

Thank You!!

Slide 150

Slide 150 text

<3<3<3<3<3