summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-02-23 15:01:19 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-02-23 16:29:21 +0100
commitbf14d0cb26df901cccea593dfefaabfdc42d01af (patch)
treea01c0662219e04fd2abb6c01bc68f2b6b349f01e /common
parentf521336c8147d00e9caa0b4ba4ab64bc43c69101 (diff)
Speed up GetPage
``` name old time/op new time/op delta GetPage-10 413ns ± 0% 287ns ± 1% -30.47% (p=0.029 n=4+4) GetPageRegular/From_root-10 757ns ± 1% 706ns ± 1% -6.75% (p=0.029 n=4+4) GetPageRegular/Page_relative-10 838ns ± 1% 786ns ± 1% -6.16% (p=0.029 n=4+4) name old alloc/op new alloc/op delta GetPage-10 312B ± 0% 24B ± 0% -92.31% (p=0.029 n=4+4) GetPageRegular/From_root-10 328B ± 0% 200B ± 0% -39.02% (p=0.029 n=4+4) GetPageRegular/Page_relative-10 360B ± 0% 232B ± 0% -35.56% (p=0.029 n=4+4) name old allocs/op new allocs/op delta GetPage-10 8.00 ± 0% 2.00 ± 0% -75.00% (p=0.029 n=4+4) GetPageRegular/From_root-10 7.00 ± 0% 5.00 ± 0% -28.57% (p=0.029 n=4+4) GetPageRegular/Page_relative-10 9.00 ± 0% 7.00 ± 0% -22.22% (p=0.029 n=4+4) ```
Diffstat (limited to 'common')
-rw-r--r--common/paths/pathparser.go26
1 files changed, 22 insertions, 4 deletions
diff --git a/common/paths/pathparser.go b/common/paths/pathparser.go
index 898eee341..20f5ee30d 100644
--- a/common/paths/pathparser.go
+++ b/common/paths/pathparser.go
@@ -54,15 +54,27 @@ func NormalizePathStringBasic(s string) string {
// ParseIdentity parses component c with path s into a StringIdentity.
func (pp *PathParser) ParseIdentity(c, s string) identity.StringIdentity {
+ p := pp.parsePooled(c, s)
+ defer putPath(p)
+ return identity.StringIdentity(p.IdentifierBase())
+}
+
+// ParseBaseAndBaseNameNoIdentifier parses component c with path s into a base and a base name without any identifier.
+func (pp *PathParser) ParseBaseAndBaseNameNoIdentifier(c, s string) (string, string) {
+ p := pp.parsePooled(c, s)
+ defer putPath(p)
+ return p.Base(), p.BaseNameNoIdentifier()
+}
+
+func (pp *PathParser) parsePooled(c, s string) *Path {
s = NormalizePathStringBasic(s)
p := getPath()
p.component = c
- defer putPath(p)
p, err := pp.doParse(c, s, p)
if err != nil {
panic(err)
}
- return identity.StringIdentity(p.IdentifierBase())
+ return p
}
// Parse parses component c with path s into Path using Hugo's content path rules.
@@ -259,7 +271,9 @@ type Path struct {
var pathPool = &sync.Pool{
New: func() any {
- return &Path{}
+ p := &Path{}
+ p.reset()
+ return p
},
}
@@ -268,6 +282,11 @@ func getPath() *Path {
}
func putPath(p *Path) {
+ p.reset()
+ pathPool.Put(p)
+}
+
+func (p *Path) reset() {
p.s = ""
p.posContainerLow = -1
p.posContainerHigh = -1
@@ -279,7 +298,6 @@ func putPath(p *Path) {
p.disabled = false
p.trimLeadingSlash = false
p.unnormalized = nil
- pathPool.Put(p)
}
// TrimLeadingSlash returns a copy of the Path with the leading slash removed.