hicli/html: preallocate space for html sanitization buffer

This commit is contained in:
Tulir Asokan 2024-10-25 23:33:32 +03:00
parent 227ba474ef
commit 8a34618a70
2 changed files with 4 additions and 0 deletions

View file

@ -505,9 +505,12 @@ func getCodeBlockLanguage(token html.Token) string {
return "" return ""
} }
const builderPreallocBuffer = 100
func sanitizeAndLinkifyHTML(body string) (string, []id.ContentURI, error) { func sanitizeAndLinkifyHTML(body string) (string, []id.ContentURI, error) {
tz := html.NewTokenizer(strings.NewReader(body)) tz := html.NewTokenizer(strings.NewReader(body))
var built strings.Builder var built strings.Builder
built.Grow(len(body) + builderPreallocBuffer)
var codeBlock *strings.Builder var codeBlock *strings.Builder
var codeBlockLanguage string var codeBlockLanguage string
var inlineImages []id.ContentURI var inlineImages []id.ContentURI

View file

@ -384,6 +384,7 @@ func (h *HiClient) calculateLocalContent(ctx context.Context, dbEvt *database.Ev
} }
} else { } else {
var builder strings.Builder var builder strings.Builder
builder.Grow(len(content.Body) + builderPreallocBuffer)
linkifyAndWriteBytes(&builder, []byte(content.Body)) linkifyAndWriteBytes(&builder, []byte(content.Body))
sanitizedHTML = builder.String() sanitizedHTML = builder.String()
wasPlaintext = true wasPlaintext = true