diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2015-03-18 00:36:48 +0100 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-03-18 17:05:54 +0100 |
commit | 98ee69bce207a4c2a7c2ffca2b3e7c0ccdd6e8c9 (patch) | |
tree | ebfb45ab42cad8a14dc44f8bacb92b47cfd12b94 /transform/absurlreplacer.go | |
parent | 9688ed2585fcea732095baeb4006c5c96875447c (diff) |
Write to rotating ContentReWriter in transformer chain
This commit adds the interface ContentReWriter in the tranformer chain.
This is backed by two pooled byte buffers, alternating between being the reader or the writer.
This keeps the performance characteristic of the old implementation, but in a thread safe way.
Fixes #911
Benchmark old vs new:
benchmark old ns/op new ns/op delta
BenchmarkAbsURL 17614 17384 -1.31%
BenchmarkXMLAbsURL 9431 9248 -1.94%
benchmark old allocs new allocs delta
BenchmarkAbsURL 24 28 +16.67%
BenchmarkXMLAbsURL 12 14 +16.67%
benchmark old bytes new bytes delta
BenchmarkAbsURL 3295 3424 +3.92%
BenchmarkXMLAbsURL 1954 1987 +1.69%
Diffstat (limited to 'transform/absurlreplacer.go')
-rw-r--r-- | transform/absurlreplacer.go | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/transform/absurlreplacer.go b/transform/absurlreplacer.go index 2f2a5bd53..66fdaf689 100644 --- a/transform/absurlreplacer.go +++ b/transform/absurlreplacer.go @@ -2,7 +2,7 @@ package transform import ( "bytes" - bp "github.com/spf13/hugo/bufferpool" + "io" "net/url" "strings" "unicode/utf8" @@ -33,7 +33,7 @@ type contentlexer struct { state stateFunc prefixLookup *prefixes - b *bytes.Buffer + w io.Writer } type stateFunc func(*contentlexer) stateFunc @@ -95,7 +95,7 @@ func (l *contentlexer) match(r rune) { } func (l *contentlexer) emit() { - l.b.Write(l.content[l.start:l.pos]) + l.w.Write(l.content[l.start:l.pos]) l.start = l.pos } @@ -134,7 +134,7 @@ func checkCandidate(l *contentlexer) { l.emit() } l.pos += len(m.match) - l.b.Write(m.replacement) + l.w.Write(m.replacement) l.start = l.pos return @@ -159,7 +159,6 @@ func (l *contentlexer) replace() { } l.width = width l.pos += l.width - if r == ' ' { l.prefixLookup.ms = matchStateWhitespace } else if l.prefixLookup.ms != matchStateNone { @@ -177,18 +176,16 @@ func (l *contentlexer) replace() { } } -func doReplace(content []byte, matchers []absURLMatcher) []byte { - b := bp.GetBuffer() - defer bp.PutBuffer(b) +func doReplace(rw ContentReWriter, matchers []absURLMatcher) { - lexer := &contentlexer{content: content, - b: b, + lexer := &contentlexer{ + content: rw.Content(), + w: rw, prefixLookup: &prefixes{pr: mainPrefixRunes}, matchers: matchers} lexer.replace() - return b.Bytes() } type absURLReplacer struct { @@ -229,10 +226,10 @@ func newAbsURLReplacer(baseURL string) *absURLReplacer { } -func (au *absURLReplacer) replaceInHTML(content []byte) []byte { - return doReplace(content, au.htmlMatchers) +func (au *absURLReplacer) replaceInHTML(rw ContentReWriter) { + doReplace(rw, au.htmlMatchers) } -func (au *absURLReplacer) replaceInXML(content []byte) []byte { - return doReplace(content, au.xmlMatchers) +func (au *absURLReplacer) replaceInXML(rw ContentReWriter) { + doReplace(rw, au.xmlMatchers) } |