Skip to content

Commit 4414ef7

Browse files
committed
parser/pageparser: Store shortcode names as unique.Handle[string] to save memory allocations
``` │ 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) ² ```
1 parent a133393 commit 4414ef7

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

‎parser/pageparser/pagelexer.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"unicode"
2020
"unicode/utf8"
21+
"unique"
2122
)
2223

2324
const eof = -1
@@ -78,7 +79,7 @@ func newPageLexer(input []byte, stateStart stateFunc, cfg Config) *pageLexer {
7879
lexerShortcodeState: lexerShortcodeState{
7980
currLeftDelimItem: tLeftDelimScNoMarkup,
8081
currRightDelimItem: tRightDelimScNoMarkup,
81-
openShortcodes: make(map[string]bool),
82+
openShortcodes: make(map[unique.Handle[string]]bool),
8283
},
8384
items: make([]Item, 0, 5),
8485
}

‎parser/pageparser/pagelexer_shortcode.go‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313

1414
package pageparser
1515

16+
import "unique"
17+
1618
type lexerShortcodeState struct {
1719
currLeftDelimItem ItemType
1820
currRightDelimItem ItemType
1921
isInline bool
20-
currShortcodeName string // is only set when a shortcode is in opened state
21-
closingState int // > 0 = on its way to be closed
22-
elementStepNum int // step number in element
23-
paramElements int // number of elements (name + value = 2) found first
24-
openShortcodes map[string]bool // set of shortcodes in open state
22+
currShortcodeName string // is only set when a shortcode is in opened state
23+
closingState int // > 0 = on its way to be closed
24+
elementStepNum int // step number in element
25+
paramElements int // number of elements (name + value = 2) found first
26+
openShortcodes map[unique.Handle[string]]bool // set of shortcodes in open state
2527
}
2628

2729
// Shortcode syntax
@@ -254,10 +256,10 @@ Loop:
254256
}
255257
default:
256258
l.backup()
257-
word := string(l.input[l.start:l.pos])
259+
word := unique.Make(string(l.input[l.start:l.pos]))
258260
if l.closingState > 0 {
259261
if !l.openShortcodes[word] {
260-
return l.errorf("closing tag for shortcode '%s' does not match start tag", word)
262+
return l.errorf("closing tag for shortcode '%s' does not match start tag", word.Value())
261263
}
262264
l.openShortcodes[word] = false
263265
lookForEnd = true
@@ -266,7 +268,7 @@ Loop:
266268
}
267269

268270
l.closingState = 0
269-
l.currShortcodeName = word
271+
l.currShortcodeName = word.Value()
270272
l.elementStepNum++
271273
if l.isInline {
272274
l.emit(tScNameInline)

0 commit comments

Comments
 (0)