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.
Vytautas Saltenis vytas@uber.com
Sat, 29 Oct 2016 10:10:22 +0300
1 files changed,
10 insertions(+),
12 deletions(-)
jump to
M
inline.go
→
inline.go
@@ -34,35 +34,30 @@ // offset is the number of valid chars before the current cursor
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 @@ currBlock.AppendChild(node)
} // Skip past whatever the callback used. i = end + consumed + if i >= len(data) { + break + } end = i } }