Skip to content

Commit 2436941

Browse files
committed
Remove the goroutines from the shortcode lexer
It was clever, but storing the items in a slice is faster -- and it gives room to more goroutines in other places. ```bash benchmark old ns/op new ns/op delta BenchmarkShortcodeLexer-4 180173 79614 -55.81% benchmark old allocs new allocs delta BenchmarkShortcodeLexer-4 309 328 +6.15% benchmark old bytes new bytes delta BenchmarkShortcodeLexer-4 35456 47008 +32.58% ```
1 parent 3153526 commit 2436941

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

hugolib/shortcodeparser.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ type pagelexer struct {
159159
openShortcodes map[string]bool // set of shortcodes in open state
160160

161161
// items delivered to client
162-
items chan item
162+
items []item
163163
}
164164

165165
// note: the input position here is normally 0 (start), but
@@ -172,9 +172,9 @@ func newShortcodeLexer(name, input string, inputPosition pos) *pagelexer {
172172
currRightDelimItem: tRightDelimScNoMarkup,
173173
pos: inputPosition,
174174
openShortcodes: make(map[string]bool),
175-
items: make(chan item),
175+
items: make([]item, 0, 5),
176176
}
177-
go lexer.runShortcodeLexer()
177+
lexer.runShortcodeLexer()
178178
return lexer
179179
}
180180

@@ -184,8 +184,6 @@ func (l *pagelexer) runShortcodeLexer() {
184184
for l.state = lexTextOutsideShortcodes; l.state != nil; {
185185
l.state = l.state(l)
186186
}
187-
188-
close(l.items)
189187
}
190188

191189
// state functions
@@ -227,7 +225,7 @@ func (l *pagelexer) backup() {
227225

228226
// sends an item back to the client.
229227
func (l *pagelexer) emit(t itemType) {
230-
l.items <- item{t, l.start, l.input[l.start:l.pos]}
228+
l.items = append(l.items, item{t, l.start, l.input[l.start:l.pos]})
231229
l.start = l.pos
232230
}
233231

@@ -239,7 +237,7 @@ func (l *pagelexer) ignoreEscapesAndEmit(t itemType) {
239237
}
240238
return r
241239
}, l.input[l.start:l.pos])
242-
l.items <- item{t, l.start, val}
240+
l.items = append(l.items, item{t, l.start, val})
243241
l.start = l.pos
244242
}
245243

@@ -260,13 +258,14 @@ func (l *pagelexer) lineNum() int {
260258

261259
// nil terminates the parser
262260
func (l *pagelexer) errorf(format string, args ...interface{}) stateFunc {
263-
l.items <- item{tError, l.start, fmt.Sprintf(format, args...)}
261+
l.items = append(l.items, item{tError, l.start, fmt.Sprintf(format, args...)})
264262
return nil
265263
}
266264

267265
// consumes and returns the next item
268266
func (l *pagelexer) nextItem() item {
269-
item := <-l.items
267+
item := l.items[0]
268+
l.items = l.items[1:]
270269
l.lastPos = item.pos
271270
return item
272271
}

0 commit comments

Comments
 (0)