parser/pageparser: Don't store the byte slices

On its own this change doesn't do any magic, but this is part of a bigger picture about making Hugo leaner in the
memory usage department.
This commit is contained in:
Bjørn Erik Pedersen
2022-07-07 16:11:47 +02:00
parent 72b0ccdb01
commit 223bf28004
13 changed files with 385 additions and 198 deletions

View File

@@ -22,21 +22,59 @@ import (
"github.com/yuin/goldmark/util"
)
type lowHigh struct {
Low int
High int
}
type Item struct {
Type ItemType
Pos int
Val []byte
Type ItemType
Err error
// The common case is a single segment.
low int
high int
// This is the uncommon case.
segments []lowHigh
// Used for validation.
firstByte byte
isString bool
}
type Items []Item
func (i Item) ValStr() string {
return string(i.Val)
func (i Item) Pos() int {
if len(i.segments) > 0 {
return i.segments[0].Low
}
return i.low
}
func (i Item) ValTyped() any {
str := i.ValStr()
func (i Item) Val(source []byte) []byte {
if len(i.segments) == 0 {
return source[i.low:i.high]
}
if len(i.segments) == 1 {
return source[i.segments[0].Low:i.segments[0].High]
}
var b bytes.Buffer
for _, s := range i.segments {
b.Write(source[s.Low:s.High])
}
return b.Bytes()
}
func (i Item) ValStr(source []byte) string {
return string(i.Val(source))
}
func (i Item) ValTyped(source []byte) any {
str := i.ValStr(source)
if i.isString {
// A quoted value that is a string even if it looks like a number etc.
return str
@@ -73,8 +111,8 @@ func (i Item) IsIndentation() bool {
return i.Type == tIndentation
}
func (i Item) IsNonWhitespace() bool {
return len(bytes.TrimSpace(i.Val)) > 0
func (i Item) IsNonWhitespace(source []byte) bool {
return len(bytes.TrimSpace(i.Val(source))) > 0
}
func (i Item) IsShortcodeName() bool {
@@ -125,20 +163,21 @@ func (i Item) IsError() bool {
return i.Type == tError
}
func (i Item) String() string {
func (i Item) ToString(source []byte) string {
val := i.Val(source)
switch {
case i.Type == tEOF:
return "EOF"
case i.Type == tError:
return string(i.Val)
return string(val)
case i.Type == tIndentation:
return fmt.Sprintf("%s:[%s]", i.Type, util.VisualizeSpaces(i.Val))
return fmt.Sprintf("%s:[%s]", i.Type, util.VisualizeSpaces(val))
case i.Type > tKeywordMarker:
return fmt.Sprintf("<%s>", i.Val)
case len(i.Val) > 50:
return fmt.Sprintf("%v:%.20q...", i.Type, i.Val)
return fmt.Sprintf("<%s>", val)
case len(val) > 50:
return fmt.Sprintf("%v:%.20q...", i.Type, val)
}
return fmt.Sprintf("%v:[%s]", i.Type, i.Val)
return fmt.Sprintf("%v:[%s]", i.Type, val)
}
type ItemType int