From 8a34618a70491ebe0097aafcd8cd0fc73a67d4d2 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 25 Oct 2024 23:33:32 +0300 Subject: [PATCH] hicli/html: preallocate space for html sanitization buffer --- pkg/hicli/html.go | 3 +++ pkg/hicli/sync.go | 1 + 2 files changed, 4 insertions(+) diff --git a/pkg/hicli/html.go b/pkg/hicli/html.go index 569549c..b6b2b21 100644 --- a/pkg/hicli/html.go +++ b/pkg/hicli/html.go @@ -505,9 +505,12 @@ func getCodeBlockLanguage(token html.Token) string { return "" } +const builderPreallocBuffer = 100 + func sanitizeAndLinkifyHTML(body string) (string, []id.ContentURI, error) { tz := html.NewTokenizer(strings.NewReader(body)) var built strings.Builder + built.Grow(len(body) + builderPreallocBuffer) var codeBlock *strings.Builder var codeBlockLanguage string var inlineImages []id.ContentURI diff --git a/pkg/hicli/sync.go b/pkg/hicli/sync.go index 953fd4c..467d996 100644 --- a/pkg/hicli/sync.go +++ b/pkg/hicli/sync.go @@ -384,6 +384,7 @@ func (h *HiClient) calculateLocalContent(ctx context.Context, dbEvt *database.Ev } } else { var builder strings.Builder + builder.Grow(len(content.Body) + builderPreallocBuffer) linkifyAndWriteBytes(&builder, []byte(content.Body)) sanitizedHTML = builder.String() wasPlaintext = true