Avoid some conditionals in inline parser

Rearrange inline parser a little bit to check less conditionals for
every byte.

* Add early check for len(data) == 0
* Move 'for i < len(data)' check inside the (rarer) positive clause of
  trigger result handling
* A check for newline turned out to be redundant
* Look up p.inlineCallback only once

All that does not gain much performance in itself, but doesn't hurt and
makes the code structure simpler, which will hopefully allow further
streamlining.
This commit is contained in:
Vytautas Saltenis 2016-10-29 10:10:22 +03:00
parent c60ee1aab0
commit 6438ce6de8

View File

@ -34,35 +34,30 @@ var (
func (p *parser) inline(currBlock *Node, data []byte) {
// this is called recursively: enforce a maximum depth
if p.nesting >= p.maxNesting {
if p.nesting >= p.maxNesting || len(data) == 0 {
return
}
p.nesting++
i, end := 0, 0
for i < len(data) {
// Stop at EOL
if data[i] == '\n' && i+1 == len(data) {
break
}
var handler inlineParser
for {
for ; end < len(data); end++ {
if p.inlineCallback[data[end]] != nil {
handler = p.inlineCallback[data[end]]
if handler != nil {
break
}
}
if end >= len(data) {
if data[end-1] == '\n' {
currBlock.AppendChild(text(data[i : end-1]))
} else {
currBlock.AppendChild(text(data[i:end]))
end--
}
currBlock.AppendChild(text(data[i:end]))
break
}
// call the trigger
handler := p.inlineCallback[data[end]]
if consumed, node := handler(p, data, end); consumed == 0 {
// No action from the callback.
end++
@ -74,6 +69,9 @@ func (p *parser) inline(currBlock *Node, data []byte) {
}
// Skip past whatever the callback used.
i = end + consumed
if i >= len(data) {
break
}
end = i
}
}