From e49e093ad80178feaf947739ce0d4d411a023544 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Sun, 4 Dec 2022 19:42:16 -0500 Subject: [PATCH] css: optimize printing quoted unescaped strings --- internal/css_printer/css_printer.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/css_printer/css_printer.go b/internal/css_printer/css_printer.go index ed0d3630369..eb8ce4c02ef 100644 --- a/internal/css_printer/css_printer.go +++ b/internal/css_printer/css_printer.go @@ -543,12 +543,18 @@ func (p *printer) printWithEscape(c rune, escape escapeKind, remainingText strin } } +// Note: This function is hot in profiles func (p *printer) printQuotedWithQuote(text string, quote byte) { if quote != quoteForURL { p.css = append(p.css, quote) } - for i, c := range text { + n := len(text) + i := 0 + runStart := 0 + + for i < n { + c, width := utf8.DecodeRuneInString(text[i:]) escape := escapeNone switch c { @@ -577,7 +583,18 @@ func (p *printer) printQuotedWithQuote(text string, quote byte) { } } - p.printWithEscape(c, escape, text[i:], false) + if escape != escapeNone { + if runStart < i { + p.css = append(p.css, text[runStart:i]...) + } + p.printWithEscape(c, escape, text[i:], false) + runStart = i + width + } + i += width + } + + if runStart < n { + p.css = append(p.css, text[runStart:]...) } if quote != quoteForURL {