perf: pre-compile regex patterns in markdownToTelegramHTML
Move 10 regexp.MustCompile() calls from inside functions to package-level var declarations. Eliminates ~20 KB of heap allocation per outbound message. - markdownToTelegramHTML: 8 patterns pre-compiled - extractCodeBlocks: 1 pattern pre-compiled - extractInlineCodes: 1 pattern pre-compiled - removeBotMention @username regex stays inline (runtime-dependent) regexp.Regexp is goroutine-safe, so pre-compiled patterns are safe to share. Ref: https://github.com/chillum-codeX/picoclaw-under-the-hood
This commit is contained in:
parent
3c58f6af4c
commit
16d9338606
1 changed files with 27 additions and 14 deletions
|
|
@ -511,6 +511,22 @@ func parseChatID(chatIDStr string) (int64, error) {
|
||||||
return id, err
|
return id, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pre-compiled regex patterns for markdownToTelegramHTML.
|
||||||
|
// Compiled once at package init, reused across all calls (regexp is goroutine-safe).
|
||||||
|
// This eliminates ~20 KB of heap allocation per outbound message.
|
||||||
|
var (
|
||||||
|
reHeader = regexp.MustCompile(`^#{1,6}\s+(.+)$`)
|
||||||
|
reBlockquote = regexp.MustCompile(`^>\s*(.*)$`)
|
||||||
|
reLink = regexp.MustCompile(`\[([^\]]+)\]\(([^)]+)\)`)
|
||||||
|
reBold = regexp.MustCompile(`\*\*(.+?)\*\*`)
|
||||||
|
reBoldAlt = regexp.MustCompile(`__(.+?)__`)
|
||||||
|
reItalic = regexp.MustCompile(`_([^_]+)_`)
|
||||||
|
reStrike = regexp.MustCompile(`~~(.+?)~~`)
|
||||||
|
reListItem = regexp.MustCompile(`^[-*]\s+`)
|
||||||
|
reCodeBlock = regexp.MustCompile("```[\\w]*\\n?([\\s\\S]*?)```")
|
||||||
|
reInlineCode = regexp.MustCompile("`([^`]+)`")
|
||||||
|
)
|
||||||
|
|
||||||
func markdownToTelegramHTML(text string) string {
|
func markdownToTelegramHTML(text string) string {
|
||||||
if text == "" {
|
if text == "" {
|
||||||
return ""
|
return ""
|
||||||
|
|
@ -522,19 +538,18 @@ func markdownToTelegramHTML(text string) string {
|
||||||
inlineCodes := extractInlineCodes(text)
|
inlineCodes := extractInlineCodes(text)
|
||||||
text = inlineCodes.text
|
text = inlineCodes.text
|
||||||
|
|
||||||
text = regexp.MustCompile(`^#{1,6}\s+(.+)$`).ReplaceAllString(text, "$1")
|
text = reHeader.ReplaceAllString(text, "$1")
|
||||||
|
|
||||||
text = regexp.MustCompile(`^>\s*(.*)$`).ReplaceAllString(text, "$1")
|
text = reBlockquote.ReplaceAllString(text, "$1")
|
||||||
|
|
||||||
text = escapeHTML(text)
|
text = escapeHTML(text)
|
||||||
|
|
||||||
text = regexp.MustCompile(`\[([^\]]+)\]\(([^)]+)\)`).ReplaceAllString(text, `<a href="$2">$1</a>`)
|
text = reLink.ReplaceAllString(text, `<a href="$2">$1</a>`)
|
||||||
|
|
||||||
text = regexp.MustCompile(`\*\*(.+?)\*\*`).ReplaceAllString(text, "<b>$1</b>")
|
text = reBold.ReplaceAllString(text, "<b>$1</b>")
|
||||||
|
|
||||||
text = regexp.MustCompile(`__(.+?)__`).ReplaceAllString(text, "<b>$1</b>")
|
text = reBoldAlt.ReplaceAllString(text, "<b>$1</b>")
|
||||||
|
|
||||||
reItalic := regexp.MustCompile(`_([^_]+)_`)
|
|
||||||
text = reItalic.ReplaceAllStringFunc(text, func(s string) string {
|
text = reItalic.ReplaceAllStringFunc(text, func(s string) string {
|
||||||
match := reItalic.FindStringSubmatch(s)
|
match := reItalic.FindStringSubmatch(s)
|
||||||
if len(match) < 2 {
|
if len(match) < 2 {
|
||||||
|
|
@ -543,9 +558,9 @@ func markdownToTelegramHTML(text string) string {
|
||||||
return "<i>" + match[1] + "</i>"
|
return "<i>" + match[1] + "</i>"
|
||||||
})
|
})
|
||||||
|
|
||||||
text = regexp.MustCompile(`~~(.+?)~~`).ReplaceAllString(text, "<s>$1</s>")
|
text = reStrike.ReplaceAllString(text, "<s>$1</s>")
|
||||||
|
|
||||||
text = regexp.MustCompile(`^[-*]\s+`).ReplaceAllString(text, "• ")
|
text = reListItem.ReplaceAllString(text, "• ")
|
||||||
|
|
||||||
for i, code := range inlineCodes.codes {
|
for i, code := range inlineCodes.codes {
|
||||||
escaped := escapeHTML(code)
|
escaped := escapeHTML(code)
|
||||||
|
|
@ -570,8 +585,7 @@ type codeBlockMatch struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractCodeBlocks(text string) codeBlockMatch {
|
func extractCodeBlocks(text string) codeBlockMatch {
|
||||||
re := regexp.MustCompile("```[\\w]*\\n?([\\s\\S]*?)```")
|
matches := reCodeBlock.FindAllStringSubmatch(text, -1)
|
||||||
matches := re.FindAllStringSubmatch(text, -1)
|
|
||||||
|
|
||||||
codes := make([]string, 0, len(matches))
|
codes := make([]string, 0, len(matches))
|
||||||
for _, match := range matches {
|
for _, match := range matches {
|
||||||
|
|
@ -579,7 +593,7 @@ func extractCodeBlocks(text string) codeBlockMatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
text = re.ReplaceAllStringFunc(text, func(m string) string {
|
text = reCodeBlock.ReplaceAllStringFunc(text, func(m string) string {
|
||||||
placeholder := fmt.Sprintf("\x00CB%d\x00", i)
|
placeholder := fmt.Sprintf("\x00CB%d\x00", i)
|
||||||
i++
|
i++
|
||||||
return placeholder
|
return placeholder
|
||||||
|
|
@ -594,8 +608,7 @@ type inlineCodeMatch struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractInlineCodes(text string) inlineCodeMatch {
|
func extractInlineCodes(text string) inlineCodeMatch {
|
||||||
re := regexp.MustCompile("`([^`]+)`")
|
matches := reInlineCode.FindAllStringSubmatch(text, -1)
|
||||||
matches := re.FindAllStringSubmatch(text, -1)
|
|
||||||
|
|
||||||
codes := make([]string, 0, len(matches))
|
codes := make([]string, 0, len(matches))
|
||||||
for _, match := range matches {
|
for _, match := range matches {
|
||||||
|
|
@ -603,7 +616,7 @@ func extractInlineCodes(text string) inlineCodeMatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
text = re.ReplaceAllStringFunc(text, func(m string) string {
|
text = reInlineCode.ReplaceAllStringFunc(text, func(m string) string {
|
||||||
placeholder := fmt.Sprintf("\x00IC%d\x00", i)
|
placeholder := fmt.Sprintf("\x00IC%d\x00", i)
|
||||||
i++
|
i++
|
||||||
return placeholder
|
return placeholder
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue