diff options
author | Noah Campbell <noahcampbell@gmail.com> | 2013-10-31 22:14:11 -0700 |
---|---|---|
committer | Noah Campbell <noahcampbell@gmail.com> | 2013-11-01 09:59:57 -0700 |
commit | 9af47f07d3f9700e233a98d7e84d85c3a30f9cd5 (patch) | |
tree | 13df4879bf1bf5f895d74029086dfce5a6b60c04 /transform/absurl.go | |
parent | f4cb8e1688b1459472413f3c85cb3b6297397020 (diff) |
Improve rendering time
50% speedup. Fix #91
to run the benchmark:
go test -test.run=NONE -bench=".*" -test.benchmem=true ./transform/ > new.txt
to compare the results:
/usr/local/go/misc/benchcmp baseline.txt new.txt
Speedup and memory improvements
benchmark old ns/op new ns/op delta
BenchmarkChain 101219 50453 -50.15%
BenchmarkTransform 51625 45531 -11.80%
benchmark old allocs new allocs delta
BenchmarkChain 222 103 -53.60%
BenchmarkTransform 135 106 -21.48%
benchmark old bytes new bytes delta
BenchmarkChain 23919 10998 -54.02%
BenchmarkTransform 11858 10665 -10.06%
Diffstat (limited to 'transform/absurl.go')
-rw-r--r-- | transform/absurl.go | 29 |
1 files changed, 9 insertions, 20 deletions
diff --git a/transform/absurl.go b/transform/absurl.go index 087aba5b1..f66edab9d 100644 --- a/transform/absurl.go +++ b/transform/absurl.go @@ -2,38 +2,28 @@ package transform import ( htmltran "code.google.com/p/go-html-transform/html/transform" - "io" "net/url" ) -type AbsURL struct { - BaseURL string -} - -func (t *AbsURL) Apply(w io.Writer, r io.Reader) (err error) { - var tr *htmltran.Transformer +func AbsURL(absURL string) (trs []*htmltran.Transform, err error) { + var baseURL *url.URL - if tr, err = htmltran.NewFromReader(r); err != nil { + if baseURL, err = url.Parse(absURL); err != nil { return } - if err = t.absUrlify(tr, elattr{"a", "href"}, elattr{"script", "src"}); err != nil { + if trs, err = absUrlify(baseURL, elattr{"a", "href"}, elattr{"script", "src"}); err != nil { return } - - return tr.Render(w) + return } type elattr struct { tag, attr string } -func (t *AbsURL) absUrlify(tr *htmltran.Transformer, selectors ...elattr) (err error) { - var baseURL, inURL *url.URL - - if baseURL, err = url.Parse(t.BaseURL); err != nil { - return - } +func absUrlify(baseURL *url.URL, selectors ...elattr) (trs []*htmltran.Transform, err error) { + var inURL *url.URL replace := func(in string) string { if inURL, err = url.Parse(in); err != nil { @@ -46,9 +36,8 @@ func (t *AbsURL) absUrlify(tr *htmltran.Transformer, selectors ...elattr) (err e } for _, el := range selectors { - if err = tr.Apply(htmltran.TransformAttrib(el.attr, replace), el.tag); err != nil { - return - } + mt := htmltran.MustTrans(htmltran.TransformAttrib(el.attr, replace), el.tag) + trs = append(trs, mt) } return |