Skip to content

Commit

Permalink
Remove the goroutines from the shortcode lexer
Browse files Browse the repository at this point in the history
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%
```
  • Loading branch information
bep committed Dec 7, 2017
1 parent 3153526 commit 2436941
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions hugolib/shortcodeparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ type pagelexer struct {
openShortcodes map[string]bool // set of shortcodes in open state

// items delivered to client
items chan item
items []item
}

// note: the input position here is normally 0 (start), but
Expand All @@ -172,9 +172,9 @@ func newShortcodeLexer(name, input string, inputPosition pos) *pagelexer {
currRightDelimItem: tRightDelimScNoMarkup,
pos: inputPosition,
openShortcodes: make(map[string]bool),
items: make(chan item),
items: make([]item, 0, 5),
}
go lexer.runShortcodeLexer()
lexer.runShortcodeLexer()
return lexer
}

Expand All @@ -184,8 +184,6 @@ func (l *pagelexer) runShortcodeLexer() {
for l.state = lexTextOutsideShortcodes; l.state != nil; {
l.state = l.state(l)
}

close(l.items)
}

// state functions
Expand Down Expand Up @@ -227,7 +225,7 @@ func (l *pagelexer) backup() {

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

Expand All @@ -239,7 +237,7 @@ func (l *pagelexer) ignoreEscapesAndEmit(t itemType) {
}
return r
}, l.input[l.start:l.pos])
l.items <- item{t, l.start, val}
l.items = append(l.items, item{t, l.start, val})
l.start = l.pos
}

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

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

// consumes and returns the next item
func (l *pagelexer) nextItem() item {
item := <-l.items
item := l.items[0]
l.items = l.items[1:]
l.lastPos = item.pos
return item
}
Expand Down

0 comments on commit 2436941

Please sign in to comment.