mirror of
https://github.com/gohugoio/hugo.git
synced 2024-12-26 11:51:44 +00:00
f1fec88c30
This commit replaces the multuple `bytes.Containts` and `bytes.Replace` with a custom replacer that does one pass through the document and exploits the fact that there are two common prefixes we search for, `src=` and `href=`. This is both faster and consumes less memory. There may be even better algos to use here, but we must leave some room for improvements for future versions. This should also make it possible to solve #816. ``` benchmark old ns/op new ns/op delta BenchmarkAbsUrl 25795 22597 -12.40% BenchmarkXmlAbsUrl 17187 11166 -35.03% benchmark old allocs new allocs delta BenchmarkAbsUrl 60 33 -45.00% BenchmarkXmlAbsUrl 30 16 -46.67% benchmark old bytes new bytes delta BenchmarkAbsUrl 5844 4167 -28.70% BenchmarkXmlAbsUrl 3754 2069 -44.89% ``` Fixes #894
33 lines
621 B
Go
33 lines
621 B
Go
package transform
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
var absUrlInit sync.Once
|
|
var ar *absurlReplacer
|
|
|
|
// for performance reasons, we reuse the first baseUrl given
|
|
func initAbsurlReplacer(baseURL string) {
|
|
absUrlInit.Do(func() {
|
|
ar = newAbsurlReplacer(baseURL)
|
|
})
|
|
}
|
|
|
|
func AbsURL(absURL string) (trs []link, err error) {
|
|
initAbsurlReplacer(absURL)
|
|
|
|
trs = append(trs, func(content []byte) []byte {
|
|
return ar.replaceInHtml(content)
|
|
})
|
|
return
|
|
}
|
|
|
|
func AbsURLInXML(absURL string) (trs []link, err error) {
|
|
initAbsurlReplacer(absURL)
|
|
|
|
trs = append(trs, func(content []byte) []byte {
|
|
return ar.replaceInXml(content)
|
|
})
|
|
return
|
|
}
|