From df64ec5d0f636a9ade25c3a5046433d23d0d47c8 Mon Sep 17 00:00:00 2001 From: Russ Ross Date: Tue, 31 May 2011 11:11:04 -0600 Subject: [PATCH] allocate new buffers on stack; mild speed improvement --- block.go | 66 +++++++++++++++++++++++++------------------------- inline.go | 32 ++++++++++++------------ markdown.go | 12 ++++----- smartypants.go | 4 +-- 4 files changed, 57 insertions(+), 57 deletions(-) diff --git a/block.go b/block.go index f1eef15..f95675e 100644 --- a/block.go +++ b/block.go @@ -116,8 +116,8 @@ func blockPrefixHeader(out *bytes.Buffer, rndr *render, data []byte) int { end-- } if end > i { - work := bytes.NewBuffer(nil) - parseInline(work, rndr, data[i:end]) + var work bytes.Buffer + parseInline(&work, rndr, data[i:end]) if rndr.mk.Header != nil { rndr.mk.Header(out, work.Bytes(), level, rndr.mk.Opaque) } @@ -450,7 +450,7 @@ func blockFencedCode(out *bytes.Buffer, rndr *render, data []byte) int { return 0 } - work := bytes.NewBuffer(nil) + var work bytes.Buffer for beg < len(data) { fence_end := isFencedCode(data[beg:], nil) @@ -491,10 +491,10 @@ func blockFencedCode(out *bytes.Buffer, rndr *render, data []byte) int { } func blockTable(out *bytes.Buffer, rndr *render, data []byte) int { - header_work := bytes.NewBuffer(nil) - i, columns, col_data := blockTableHeader(header_work, rndr, data) + var header_work bytes.Buffer + i, columns, col_data := blockTableHeader(&header_work, rndr, data) if i > 0 { - body_work := bytes.NewBuffer(nil) + var body_work bytes.Buffer for i < len(data) { pipes, row_start := 0, i @@ -509,7 +509,7 @@ func blockTable(out *bytes.Buffer, rndr *render, data []byte) int { break } - blockTableRow(body_work, rndr, data[row_start:i], columns, col_data) + blockTableRow(&body_work, rndr, data[row_start:i], columns, col_data) i++ } @@ -609,7 +609,7 @@ func blockTableHeader(out *bytes.Buffer, rndr *render, data []byte) (size int, c func blockTableRow(out *bytes.Buffer, rndr *render, data []byte, columns int, col_data []int) { i, col := 0, 0 - row_work := bytes.NewBuffer(nil) + var row_work bytes.Buffer if i < len(data) && data[i] == '|' { i++ @@ -632,15 +632,15 @@ func blockTableRow(out *bytes.Buffer, rndr *render, data []byte, columns int, co cell_end-- } - cell_work := bytes.NewBuffer(nil) - parseInline(cell_work, rndr, data[cell_start:cell_end+1]) + var cell_work bytes.Buffer + parseInline(&cell_work, rndr, data[cell_start:cell_end+1]) if rndr.mk.TableCell != nil { cdata := 0 if col < len(col_data) { cdata = col_data[col] } - rndr.mk.TableCell(row_work, cell_work.Bytes(), cdata, rndr.mk.Opaque) + rndr.mk.TableCell(&row_work, cell_work.Bytes(), cdata, rndr.mk.Opaque) } i++ @@ -653,7 +653,7 @@ func blockTableRow(out *bytes.Buffer, rndr *render, data []byte, columns int, co if col < len(col_data) { cdata = col_data[col] } - rndr.mk.TableCell(row_work, empty_cell, cdata, rndr.mk.Opaque) + rndr.mk.TableCell(&row_work, empty_cell, cdata, rndr.mk.Opaque) } } @@ -679,8 +679,8 @@ func blockQuotePrefix(data []byte) int { // parse a blockquote fragment func blockQuote(out *bytes.Buffer, rndr *render, data []byte) int { - block := bytes.NewBuffer(nil) - work := bytes.NewBuffer(nil) + var block bytes.Buffer + var work bytes.Buffer beg, end := 0, 0 for beg < len(data) { for end = beg + 1; end < len(data) && data[end-1] != '\n'; end++ { @@ -701,7 +701,7 @@ func blockQuote(out *bytes.Buffer, rndr *render, data []byte) int { beg = end } - parseBlock(block, rndr, work.Bytes()) + parseBlock(&block, rndr, work.Bytes()) if rndr.mk.BlockQuote != nil { rndr.mk.BlockQuote(out, block.Bytes(), rndr.mk.Opaque) } @@ -720,7 +720,7 @@ func blockCodePrefix(data []byte) int { } func blockCode(out *bytes.Buffer, rndr *render, data []byte) int { - work := bytes.NewBuffer(nil) + var work bytes.Buffer beg, end := 0, 0 for beg < len(data) { @@ -754,7 +754,7 @@ func blockCode(out *bytes.Buffer, rndr *render, data []byte) int { n++ } if n > 0 { - work = bytes.NewBuffer(workbytes[:len(workbytes)-n]) + work.Truncate(len(workbytes)-n) } work.WriteByte('\n') @@ -798,11 +798,11 @@ func blockOliPrefix(data []byte) int { // parse ordered or unordered list block func blockList(out *bytes.Buffer, rndr *render, data []byte, flags int) int { - work := bytes.NewBuffer(nil) + var work bytes.Buffer i, j := 0, 0 for i < len(data) { - j = blockListItem(work, rndr, data[i:], &flags) + j = blockListItem(&work, rndr, data[i:], &flags) i += j if j == 0 || flags&LIST_ITEM_END_OF_LIST != 0 { @@ -846,8 +846,8 @@ func blockListItem(out *bytes.Buffer, rndr *render, data []byte, flags *int) int } // get working buffers - work := bytes.NewBuffer(nil) - inter := bytes.NewBuffer(nil) + var work bytes.Buffer + var inter bytes.Buffer // put the first line into the working buffer work.Write(data[beg:end]) @@ -924,18 +924,18 @@ func blockListItem(out *bytes.Buffer, rndr *render, data []byte, flags *int) int if *flags&LIST_ITEM_CONTAINS_BLOCK != 0 { // intermediate render of block li if sublist > 0 && sublist < len(workbytes) { - parseBlock(inter, rndr, workbytes[:sublist]) - parseBlock(inter, rndr, workbytes[sublist:]) + parseBlock(&inter, rndr, workbytes[:sublist]) + parseBlock(&inter, rndr, workbytes[sublist:]) } else { - parseBlock(inter, rndr, workbytes) + parseBlock(&inter, rndr, workbytes) } } else { // intermediate render of inline li if sublist > 0 && sublist < len(workbytes) { - parseInline(inter, rndr, workbytes[:sublist]) - parseBlock(inter, rndr, workbytes[sublist:]) + parseInline(&inter, rndr, workbytes[:sublist]) + parseBlock(&inter, rndr, workbytes[sublist:]) } else { - parseInline(inter, rndr, workbytes) + parseInline(&inter, rndr, workbytes) } } @@ -983,8 +983,8 @@ func blockParagraph(out *bytes.Buffer, rndr *render, data []byte) int { } if level == 0 { - tmp := bytes.NewBuffer(nil) - parseInline(tmp, rndr, work[:size]) + var tmp bytes.Buffer + parseInline(&tmp, rndr, work[:size]) if rndr.mk.Paragraph != nil { rndr.mk.Paragraph(out, tmp.Bytes(), rndr.mk.Opaque) } @@ -1004,8 +1004,8 @@ func blockParagraph(out *bytes.Buffer, rndr *render, data []byte) int { } if size > 0 { - tmp := bytes.NewBuffer(nil) - parseInline(tmp, rndr, work[:size]) + var tmp bytes.Buffer + parseInline(&tmp, rndr, work[:size]) if rndr.mk.Paragraph != nil { rndr.mk.Paragraph(out, tmp.Bytes(), rndr.mk.Opaque) } @@ -1017,8 +1017,8 @@ func blockParagraph(out *bytes.Buffer, rndr *render, data []byte) int { } } - header_work := bytes.NewBuffer(nil) - parseInline(header_work, rndr, work[:size]) + var header_work bytes.Buffer + parseInline(&header_work, rndr, work[:size]) if rndr.mk.Header != nil { rndr.mk.Header(out, header_work.Bytes(), level, rndr.mk.Opaque) diff --git a/inline.go b/inline.go index 9e7852b..0f3c068 100644 --- a/inline.go +++ b/inline.go @@ -334,7 +334,7 @@ func inlineLink(out *bytes.Buffer, rndr *render, data []byte, offset int) int { // find the reference if link_b == link_e { if text_has_nl { - b := bytes.NewBuffer(nil) + var b bytes.Buffer for j := 1; j < txt_e; j++ { switch { @@ -371,7 +371,7 @@ func inlineLink(out *bytes.Buffer, rndr *render, data []byte, offset int) int { // craft the id if text_has_nl { - b := bytes.NewBuffer(nil) + var b bytes.Buffer for j := 1; j < txt_e; j++ { switch { @@ -403,19 +403,19 @@ func inlineLink(out *bytes.Buffer, rndr *render, data []byte, offset int) int { } // build content: img alt is escaped, link content is parsed - content := bytes.NewBuffer(nil) + var content bytes.Buffer if txt_e > 1 { if isImg { content.Write(data[1:txt_e]) } else { - parseInline(content, rndr, data[1:txt_e]) + parseInline(&content, rndr, data[1:txt_e]) } } var u_link []byte if len(link) > 0 { - u_link_buf := bytes.NewBuffer(nil) - unescapeText(u_link_buf, link) + var u_link_buf bytes.Buffer + unescapeText(&u_link_buf, link) u_link = u_link_buf.Bytes() } @@ -449,8 +449,8 @@ func inlineLAngle(out *bytes.Buffer, rndr *render, data []byte, offset int) int if end > 2 { switch { case rndr.mk.AutoLink != nil && altype != LINK_TYPE_NOT_AUTOLINK: - u_link := bytes.NewBuffer(nil) - unescapeText(u_link, data[1:end+1-2]) + var u_link bytes.Buffer + unescapeText(&u_link, data[1:end+1-2]) ret = rndr.mk.AutoLink(out, u_link.Bytes(), altype, rndr.mk.Opaque) case rndr.mk.RawHtmlTag != nil: ret = rndr.mk.RawHtmlTag(out, data[:end], rndr.mk.Opaque) @@ -632,8 +632,8 @@ func inlineAutoLink(out *bytes.Buffer, rndr *render, data []byte, offset int) in } if rndr.mk.AutoLink != nil { - u_link := bytes.NewBuffer(nil) - unescapeText(u_link, data[:link_end]) + var u_link bytes.Buffer + unescapeText(&u_link, data[:link_end]) rndr.mk.AutoLink(out, u_link.Bytes(), LINK_TYPE_NORMAL, rndr.mk.Opaque) } @@ -884,8 +884,8 @@ func inlineHelperEmph1(out *bytes.Buffer, rndr *render, data []byte, c byte) int } } - work := bytes.NewBuffer(nil) - parseInline(work, rndr, data[:i]) + var work bytes.Buffer + parseInline(&work, rndr, data[:i]) r := rndr.mk.Emphasis(out, work.Bytes(), rndr.mk.Opaque) if r > 0 { return i + 1 @@ -918,8 +918,8 @@ func inlineHelperEmph2(out *bytes.Buffer, rndr *render, data []byte, c byte) int i += length if i+1 < len(data) && data[i] == c && data[i+1] == c && i > 0 && !isspace(data[i-1]) { - work := bytes.NewBuffer(nil) - parseInline(work, rndr, data[:i]) + var work bytes.Buffer + parseInline(&work, rndr, data[:i]) r := render_method(out, work.Bytes(), rndr.mk.Opaque) if r > 0 { return i + 2 @@ -952,9 +952,9 @@ func inlineHelperEmph3(out *bytes.Buffer, rndr *render, data []byte, offset int, switch { case (i+2 < len(data) && data[i+1] == c && data[i+2] == c && rndr.mk.TripleEmphasis != nil): // triple symbol found - work := bytes.NewBuffer(nil) + var work bytes.Buffer - parseInline(work, rndr, data[:i]) + parseInline(&work, rndr, data[:i]) r := rndr.mk.TripleEmphasis(out, work.Bytes(), rndr.mk.Opaque) if r > 0 { return i + 3 diff --git a/markdown.go b/markdown.go index 5513a69..2f96d67 100644 --- a/markdown.go +++ b/markdown.go @@ -191,7 +191,7 @@ func Markdown(input []byte, renderer *Renderer, extensions uint32) []byte { } // first pass: look for references, copy everything else - text := bytes.NewBuffer(nil) + var text bytes.Buffer beg, end := 0, 0 for beg < len(input) { // iterate over lines if end = isReference(rndr, input[beg:]); end > 0 { @@ -204,7 +204,7 @@ func Markdown(input []byte, renderer *Renderer, extensions uint32) []byte { // add the line body if present if end > beg { - expandTabs(text, input[beg:end]) + expandTabs(&text, input[beg:end]) } for end < len(input) && (input[end] == '\n' || input[end] == '\r') { @@ -220,9 +220,9 @@ func Markdown(input []byte, renderer *Renderer, extensions uint32) []byte { } // second pass: actual rendering - output := bytes.NewBuffer(nil) + var output bytes.Buffer if rndr.mk.DocumentHeader != nil { - rndr.mk.DocumentHeader(output, rndr.mk.Opaque) + rndr.mk.DocumentHeader(&output, rndr.mk.Opaque) } if text.Len() > 0 { @@ -231,11 +231,11 @@ func Markdown(input []byte, renderer *Renderer, extensions uint32) []byte { if finalchar != '\n' && finalchar != '\r' { text.WriteByte('\n') } - parseBlock(output, rndr, text.Bytes()) + parseBlock(&output, rndr, text.Bytes()) } if rndr.mk.DocumentFooter != nil { - rndr.mk.DocumentFooter(output, rndr.mk.Opaque) + rndr.mk.DocumentFooter(&output, rndr.mk.Opaque) } if rndr.nesting != 0 { diff --git a/smartypants.go b/smartypants.go index f47ec2a..44996c4 100644 --- a/smartypants.go +++ b/smartypants.go @@ -332,8 +332,8 @@ func htmlSmartypants(ob *bytes.Buffer, text []byte, opaque interface{}) { smrt := smartypantsData{false, false} // first do normal entity escaping - escaped := bytes.NewBuffer(nil) - attrEscape(escaped, text) + var escaped bytes.Buffer + attrEscape(&escaped, text) text = escaped.Bytes() mark := 0