perf(template): cache template functions

This commit is contained in:
Jan De Dobbeleer 2024-08-05 13:29:15 +02:00 committed by Jan De Dobbeleer
parent 48528a3fd3
commit 9e1942035f
3 changed files with 9 additions and 4 deletions

View file

@ -1,6 +1,8 @@
package main package main
import "github.com/jandedobbeleer/oh-my-posh/src/cli" import (
"github.com/jandedobbeleer/oh-my-posh/src/cli"
)
func main() { func main() {
cli.Execute() cli.Execute()

View file

@ -10,7 +10,7 @@ import (
func BenchmarkInit(b *testing.B) { func BenchmarkInit(b *testing.B) {
cmd := cli.RootCmd cmd := cli.RootCmd
// needs to be a non-existing file as we panic otherwise // needs to be a non-existing file as we panic otherwise
cmd.SetArgs([]string{"init", "fish", "--print", "--config", "err.omp.json"}) cmd.SetArgs([]string{"init", "fish", "--print"})
out := bytes.NewBufferString("") out := bytes.NewBufferString("")
cmd.SetOut(out) cmd.SetOut(out)
@ -22,7 +22,7 @@ func BenchmarkInit(b *testing.B) {
func BenchmarkPrimary(b *testing.B) { func BenchmarkPrimary(b *testing.B) {
cmd := cli.RootCmd cmd := cli.RootCmd
// needs to be a non-existing file as we panic otherwise // needs to be a non-existing file as we panic otherwise
cmd.SetArgs([]string{"print", "primary", "--config", "err.omp.json", "--pwd", "/Users/jan/Code/oh-my-posh/src", "--shell", "fish"}) cmd.SetArgs([]string{"print", "primary", "--pwd", "/Users/jan/Code/oh-my-posh/src", "--shell", "fish"})
out := bytes.NewBufferString("") out := bytes.NewBufferString("")
cmd.SetOut(out) cmd.SetOut(out)

View file

@ -48,6 +48,8 @@ var (
} }
shell string shell string
tmplFunc = template.New("cache").Funcs(funcMap())
) )
type Text struct { type Text struct {
@ -87,7 +89,7 @@ func (t *Text) Render() (string, error) {
t.cleanTemplate() t.cleanTemplate()
tmpl, err := template.New(t.Template).Funcs(funcMap()).Parse(t.Template) tmpl, err := tmplFunc.Parse(t.Template)
if err != nil { if err != nil {
t.Env.Error(err) t.Env.Error(err)
return "", errors.New(InvalidTemplate) return "", errors.New(InvalidTemplate)
@ -106,6 +108,7 @@ func (t *Text) Render() (string, error) {
if len(msg) == 0 { if len(msg) == 0 {
return "", errors.New(IncorrectTemplate) return "", errors.New(IncorrectTemplate)
} }
return "", errors.New(msg["MSG"]) return "", errors.New(msg["MSG"])
} }