1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
require 'strscan'
require 'benchmark'
require 'thread'
class Scanner < StringScanner
def initialize code
super code
end
def tokenize encoder = Tokens.new
scan_tokens encoder
encoder
end
protected
def scan_tokens encoder
until eos?
if matched = scan(/\s+/)
encoder.text_token matched, :space
elsif matched = scan(/!/)
encoder.text_token matched, :not_going_to_happen
elsif matched = scan(/=/) #/
encoder.text_token matched, :not_going_to_happen
elsif matched = scan(/%/)
encoder.text_token matched, :not_going_to_happen
elsif matched = scan(/\w+/)
encoder.text_token matched, :word
elsif matched = scan(/[,.]/)
encoder.text_token matched, :op
elsif scan(/\(/)
encoder.begin_group :par
elsif scan(/\)/)
encoder.end_group :par
else
raise
end
end
end
end
class Tokens < Array
alias token push
alias text_token push
alias block_token push
def begin_group kind; push :begin_group, kind end
def end_group kind; push :end_group, kind end
end
class TokensQueue < Queue
def text_token text, kind
push [text, kind]
end
def begin_group kind
push [:begin_group, kind]
end
def end_group kind
push [:end_group, kind]
end
end
class Encoder
def setup
@out = ''
@opened = []
end
def finish
while kind = @opened.pop
close kind
end
@out
end
def encode_tokens tokens
setup
compile tokens
finish
end
def encode_stream scanner
setup
scanner.tokenize self
finish
end
def encode_queue scanner
setup
queue = TokensQueue.new
Thread.new do
scanner.tokenize queue
queue << nil # end
end.join
Thread.new do
while value = queue.pop
token(*value)
end
end.join
finish
end
def token content, kind
if content.is_a? ::String
text_token content, kind
elsif content.is_a? ::Symbol
block_token content, kind
else
raise 'Unknown token content type: %p' % [content]
end
end
def text_token text, kind
@out <<
if kind == :space
text
else
text.gsub!(/[)\\]/, '\\\\\0') # escape ) and \
"#{kind}(#{text})"
end
end
def block_token action, kind
case action
when :begin_group
begin_group kind
when :end_group
end_group kind
else
raise
end
end
def begin_group kind
@opened << kind
@out << "#{kind}<"
end
def end_group kind
@opened.pop
@out << '>'
end
protected
def compile tokens
content = nil
for item in tokens
if content
case content
when ::String
text_token content, item
content = nil
when :begin_group
begin_group item
content = nil
when :end_group
end_group item
content = nil
when ::Symbol
block_token content, item
content = nil
else
raise
end
else
content = item
end
end
raise if content
end
end
N = (10 ** (ARGV.first || 5).to_i)
code = " alpha, beta, (gamma).\n" * N
scanner = Scanner.new code
encoder = Encoder.new
# tokens = nil
# time_scanning = Benchmark.realtime do
# tokens = scanner.tokenize
# end
# puts 'Scanning: %0.2fs -- %0.0f kTok/s' % [time_scanning, tokens.size / 2 / time_scanning / 1000]
#
# time_encoding = Benchmark.realtime do
# encoder.encode_tokens tokens
# end
# puts 'Encoding: %0.2fs -- %0.0f kTok/s' % [time_encoding, tokens.size / 2 / time_encoding / 1000]
#
# time = time_scanning + time_encoding
# puts 'Together: %0.2fs -- %0.0f kTok/s' % [time, tokens.size / 2 / time / 1000]
# scanner.reset
time = Benchmark.realtime do
encoder.encode_stream scanner
end
puts 'Direct Streaming: %0.2fs -- %0.0f kTok/s' % [time, (N * 11 + 1) / time / 1000]
scanner.reset
time = Benchmark.realtime do
encoder.encode_queue scanner
end
puts 'Queue: %0.2fs -- %0.0f kTok/s' % [time, (N * 11 + 1) / time / 1000]
|