mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-26 10:47:04 -06:00
Each render built a new goldmark converter. For short texts, such as comments, this was most of the cost. Now `render` reuses converters from a `sync.Pool`, as the TODO in `SpecializedMarkdown` suggested. Each converter gets the `RenderInternal` of the render. The HTML output does not change. | Benchmark | Before | After | Allocs | |---|---:|---:|---| | Comment | 85.0 µs | 46.0 µs | 502 → 207 | | `BenchmarkMarkdownRender` | 100.5 µs | 62.8 µs | 536 → 241 | | README | 1.51 ms | 1.50 ms | 9679 → 9384 | No benchmark got slower (AMD EPYC 9V74, Go 1.27.4, 24 rounds). Checks: - `go test ./modules/markup/...` and golangci-lint pass. - Old and new HTML matched on 20,000 random documents. - Parallel renders pass with `-race`. Claude was used for this change. I reviewed and tested it. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: silverwind <me@silverwind.io>
82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package internal
|
|
|
|
import (
|
|
"html/template"
|
|
"io"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
|
|
"gitea.dev/modules/htmlutil"
|
|
"gitea.dev/modules/util"
|
|
|
|
"golang.org/x/net/html"
|
|
)
|
|
|
|
var reAttrClass = sync.OnceValue(func() *regexp.Regexp {
|
|
// TODO: it isn't a problem at the moment because our HTML contents are always well constructed
|
|
return regexp.MustCompile(`(<[^>]+)\s+class="([^"]+)"([^>]*>)`)
|
|
})
|
|
|
|
type RenderVarMutable[T any] struct {
|
|
Value *T
|
|
}
|
|
|
|
// RenderInternal also works without initialization
|
|
// If no initialization (no secureID), it will not protect any attributes and return the original name&value
|
|
type RenderInternal struct {
|
|
secureID string
|
|
secureIDPrefix string
|
|
}
|
|
|
|
func (r *RenderInternal) Init(output io.Writer, extraHeadHTML template.HTML) io.WriteCloser {
|
|
return r.init(util.FastCryptoRandomHex(16), output, extraHeadHTML)
|
|
}
|
|
|
|
func (r *RenderInternal) init(secID string, output io.Writer, extraHeadHTML template.HTML) io.WriteCloser {
|
|
r.secureID = secID
|
|
r.secureIDPrefix = r.secureID + ":"
|
|
return &finalProcessor{renderInternal: r, output: output, extraHeadHTML: extraHeadHTML}
|
|
}
|
|
|
|
func (r *RenderInternal) RecoverProtectedValue(v string) (string, bool) {
|
|
if !strings.HasPrefix(v, r.secureIDPrefix) {
|
|
return "", false
|
|
}
|
|
return v[len(r.secureIDPrefix):], true
|
|
}
|
|
|
|
func (r *RenderInternal) SafeAttr(name string) string {
|
|
if r.secureID == "" {
|
|
return name
|
|
}
|
|
return "data-attr-" + name
|
|
}
|
|
|
|
func (r *RenderInternal) SafeValue(val string) string {
|
|
if r.secureID == "" {
|
|
return val
|
|
}
|
|
return r.secureID + ":" + val
|
|
}
|
|
|
|
func (r *RenderInternal) NodeSafeAttr(attr, val string) html.Attribute {
|
|
return html.Attribute{Key: r.SafeAttr(attr), Val: r.SafeValue(val)}
|
|
}
|
|
|
|
func (r *RenderInternal) ProtectSafeAttrs(content template.HTML) template.HTML {
|
|
if r.secureID == "" {
|
|
return content
|
|
}
|
|
return template.HTML(reAttrClass().ReplaceAllString(string(content), `$1 data-attr-class="`+r.secureIDPrefix+`$2"$3`))
|
|
}
|
|
|
|
func (r *RenderInternal) FormatWithSafeAttrsf(w io.Writer, fmt template.HTML, a ...any) error {
|
|
htmlStr := r.ProtectSafeAttrs(htmlutil.HTMLFormat(fmt, a...))
|
|
_, err := io.WriteString(w, string(htmlStr))
|
|
return err
|
|
}
|