]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
parser/pageparser: Store shortcode names as unique.Handle[string] to save memory...
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 5 Oct 2025 15:01:26 +0000 (17:01 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 5 Oct 2025 17:06:46 +0000 (19:06 +0200)
```
                        │ stash.bench  │        perf-pagelexer.bench        │
                        │    sec/op    │    sec/op     vs base              │
ShortcodeLexer-10         68.87µ ± ∞ ¹   65.91µ ± ∞ ¹       ~ (p=0.200 n=4)
Parse-10                  7.421µ ± ∞ ¹   7.030µ ± ∞ ¹  -5.28% (p=0.029 n=4)
HasShortcode/Match-10     70.42n ± ∞ ¹   77.37n ± ∞ ¹  +9.86% (p=0.029 n=4)
HasShortcode/NoMatch-10   14.32n ± ∞ ¹   14.38n ± ∞ ¹       ~ (p=0.086 n=4)
geomean                   847.2n         847.3n        +0.01%
¹ need >= 6 samples for confidence interval at level 0.95

                        │  stash.bench  │         perf-pagelexer.bench          │
                        │     B/op      │     B/op       vs base                │
ShortcodeLexer-10         181.3Ki ± ∞ ¹   177.4Ki ± ∞ ¹  -2.19% (p=0.029 n=4)
Parse-10                  24.56Ki ± ∞ ¹   24.19Ki ± ∞ ¹  -1.53% (p=0.029 n=4)
HasShortcode/Match-10       0.000 ± ∞ ¹     0.000 ± ∞ ¹       ~ (p=1.000 n=4) ²
HasShortcode/NoMatch-10     0.000 ± ∞ ¹     0.000 ± ∞ ¹       ~ (p=1.000 n=4) ²
geomean                               ³                  -0.93%               ³
¹ need >= 6 samples for confidence interval at level 0.95
² all samples are equal
³ summaries must be >0 to compute geomean

                        │ stash.bench  │         perf-pagelexer.bench         │
                        │  allocs/op   │  allocs/op   vs base                 │
ShortcodeLexer-10         1004.0 ± ∞ ¹   921.0 ± ∞ ¹   -8.27% (p=0.029 n=4)
Parse-10                   34.00 ± ∞ ¹   14.00 ± ∞ ¹  -58.82% (p=0.029 n=4)
HasShortcode/Match-10      0.000 ± ∞ ¹   0.000 ± ∞ ¹        ~ (p=1.000 n=4) ²
HasShortcode/NoMatch-10    0.000 ± ∞ ¹   0.000 ± ∞ ¹        ~ (p=1.000 n=4) ²
```

parser/pageparser/pagelexer.go
parser/pageparser/pagelexer_shortcode.go

index a5f64b0371b0a72f206a00ccc67b24e3f78c857e..612fb75f42709da04e3ff9300ff32217eb21cd78 100644 (file)
@@ -18,6 +18,7 @@ import (
        "fmt"
        "unicode"
        "unicode/utf8"
+       "unique"
 )
 
 const eof = -1
@@ -78,7 +79,7 @@ func newPageLexer(input []byte, stateStart stateFunc, cfg Config) *pageLexer {
                lexerShortcodeState: lexerShortcodeState{
                        currLeftDelimItem:  tLeftDelimScNoMarkup,
                        currRightDelimItem: tRightDelimScNoMarkup,
-                       openShortcodes:     make(map[string]bool),
+                       openShortcodes:     make(map[unique.Handle[string]]bool),
                },
                items: make([]Item, 0, 5),
        }
index 12d07cd68a359c8874980cdb37c32ce2119e5a7c..3a656263a9b604a2a53f7a8c29441f1284803a1b 100644 (file)
 
 package pageparser
 
+import "unique"
+
 type lexerShortcodeState struct {
        currLeftDelimItem  ItemType
        currRightDelimItem ItemType
        isInline           bool
-       currShortcodeName  string          // is only set when a shortcode is in opened state
-       closingState       int             // > 0 = on its way to be closed
-       elementStepNum     int             // step number in element
-       paramElements      int             // number of elements (name + value = 2) found first
-       openShortcodes     map[string]bool // set of shortcodes in open state
+       currShortcodeName  string                         // is only set when a shortcode is in opened state
+       closingState       int                            // > 0 = on its way to be closed
+       elementStepNum     int                            // step number in element
+       paramElements      int                            // number of elements (name + value = 2) found first
+       openShortcodes     map[unique.Handle[string]]bool // set of shortcodes in open state
 }
 
 // Shortcode syntax
@@ -254,10 +256,10 @@ Loop:
                        }
                default:
                        l.backup()
-                       word := string(l.input[l.start:l.pos])
+                       word := unique.Make(string(l.input[l.start:l.pos]))
                        if l.closingState > 0 {
                                if !l.openShortcodes[word] {
-                                       return l.errorf("closing tag for shortcode '%s' does not match start tag", word)
+                                       return l.errorf("closing tag for shortcode '%s' does not match start tag", word.Value())
                                }
                                l.openShortcodes[word] = false
                                lookForEnd = true
@@ -266,7 +268,7 @@ Loop:
                        }
 
                        l.closingState = 0
-                       l.currShortcodeName = word
+                       l.currShortcodeName = word.Value()
                        l.elementStepNum++
                        if l.isInline {
                                l.emit(tScNameInline)