pkg/pools: Use pool for strings.Builder everywhere

This commit is contained in:
Tom Wiesing 2023-01-24 10:52:15 +01:00
parent 8af2213d5a
commit a63bb2f669
No known key found for this signature in database
7 changed files with 44 additions and 25 deletions

View file

@ -7,8 +7,9 @@ import (
"math/rand" "math/rand"
"net/url" "net/url"
"reflect" "reflect"
"strings"
"time" "time"
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
) )
// Config represents the configuration of a WissKI Distillery. // Config represents the configuration of a WissKI Distillery.
@ -111,7 +112,8 @@ func (config *Config) CSRFSecret() []byte {
// String serializes this configuration into a string // String serializes this configuration into a string
func (config Config) String() string { func (config Config) String() string {
values := &strings.Builder{} builder := pools.GetBuilder()
defer pools.ReleaseBuilder(builder)
vConfig := reflect.ValueOf(config) vConfig := reflect.ValueOf(config)
tConfig := vConfig.Type() tConfig := vConfig.Type()
@ -127,8 +129,8 @@ func (config Config) String() string {
continue continue
} }
fmt.Fprintf(values, "%s=%v\n", env, vField.Interface()) fmt.Fprintf(builder, "%s=%v\n", env, vField.Interface())
} }
return values.String() return builder.String()
} }

View file

@ -4,14 +4,16 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"strings"
"github.com/FAU-CDI/wisski-distillery/pkg/countwriter" "github.com/FAU-CDI/wisski-distillery/pkg/countwriter"
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
) )
func (snapshot Snapshot) String() string { func (snapshot Snapshot) String() string {
var builder strings.Builder builder := pools.GetBuilder()
snapshot.Report(&builder) defer pools.ReleaseBuilder(builder)
snapshot.Report(builder)
return builder.String() return builder.String()
} }
@ -63,8 +65,10 @@ func (snapshot Snapshot) Report(w io.Writer) (int, error) {
// Strings turns this backup into a string for the BackupReport. // Strings turns this backup into a string for the BackupReport.
func (backup Backup) String() string { func (backup Backup) String() string {
var builder strings.Builder builder := pools.GetBuilder()
backup.Report(&builder) defer pools.ReleaseBuilder(builder)
backup.Report(builder)
return builder.String() return builder.String()
} }

View file

@ -12,6 +12,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/internal/status" "github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx" "github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
) )
//go:embed "public.html" //go:embed "public.html"
@ -58,13 +59,17 @@ func (home *Home) publicHandler(ctx context.Context) http.Handler {
return pc, httpx.ErrNotFound return pc, httpx.ErrNotFound
} }
// get a builder
builder := pools.GetBuilder()
defer pools.ReleaseBuilder(builder)
// prepare about // prepare about
pc.aboutContext.Instances = home.homeInstances.Get(nil) pc.aboutContext.Instances = home.homeInstances.Get(nil)
pc.aboutContext.SelfRedirect = home.Config.SelfRedirect.String() pc.aboutContext.SelfRedirect = home.Config.SelfRedirect.String()
// render the about template // render the about template
var builder strings.Builder
if err := about.Execute(&builder, pc.aboutContext); err != nil { if err := about.Execute(builder, pc.aboutContext); err != nil {
return pc, nil return pc, nil
} }

View file

@ -12,6 +12,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component" "github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
gmmeta "github.com/yuin/goldmark-meta" gmmeta "github.com/yuin/goldmark-meta"
@ -89,7 +90,8 @@ var newsFS embed.FS
// Items returns a list of all news items // Items returns a list of all news items
func Items() ([]Item, error) { func Items() ([]Item, error) {
var builder strings.Builder builder := pools.GetBuilder()
defer pools.ReleaseBuilder(builder)
files, err := fs.Glob(newsFS, "NEWS/*.md") files, err := fs.Glob(newsFS, "NEWS/*.md")
if err != nil { if err != nil {
@ -99,7 +101,7 @@ func Items() ([]Item, error) {
items := make([]Item, len(files)) items := make([]Item, len(files))
for i, file := range files { for i, file := range files {
items[i].ID = file[len("NEWS/") : len(file)-len(".md")] items[i].ID = file[len("NEWS/") : len(file)-len(".md")]
if err := items[i].parse(file, &builder); err != nil { if err := items[i].parse(file, builder); err != nil {
return nil, err return nil, err
} }
} }

View file

@ -7,10 +7,10 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"reflect" "reflect"
"strings"
"time" "time"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx" "github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
"github.com/gorilla/csrf" "github.com/gorilla/csrf"
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
@ -159,7 +159,9 @@ func (ctx *tContext[C]) renderSafe(name string, t *template.Template, c any) (te
} }
value, err, panicked := func() (value template.HTML, err error, panicked bool) { value, err, panicked := func() (value template.HTML, err error, panicked bool) {
var builder strings.Builder // get a builder
builder := pools.GetBuilder()
defer pools.ReleaseBuilder(builder)
defer func() { defer func() {
if panicked { if panicked {
@ -173,7 +175,7 @@ func (ctx *tContext[C]) renderSafe(name string, t *template.Template, c any) (te
}() }()
panicked = true panicked = true
err = t.Execute(&builder, c) err = t.Execute(builder, c)
panicked = false panicked = false
if err != nil { if err != nil {

View file

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/FAU-CDI/wisski-distillery/internal/phpx" "github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
@ -24,19 +25,20 @@ type DrupalUser struct {
} }
func (du DrupalUser) String() string { func (du DrupalUser) String() string {
var builder strings.Builder builder := pools.GetBuilder()
defer pools.ReleaseBuilder(builder)
builder.WriteString("DrupalUser{") builder.WriteString("DrupalUser{")
defer builder.WriteString("}") defer builder.WriteString("}")
fmt.Fprintf(&builder, "UID: %d, ", du.UID) fmt.Fprintf(builder, "UID: %d, ", du.UID)
fmt.Fprintf(&builder, "Name: %q, ", du.Name) fmt.Fprintf(builder, "Name: %q, ", du.Name)
if du.Mail != "" { if du.Mail != "" {
fmt.Fprintf(&builder, "Mail: %q, ", du.Mail) fmt.Fprintf(builder, "Mail: %q, ", du.Mail)
} }
fmt.Fprintf(&builder, "Status: %t, ", du.Status) fmt.Fprintf(builder, "Status: %t, ", du.Status)
for _, tn := range []struct { for _, tn := range []struct {
Name string Name string
@ -50,10 +52,10 @@ func (du DrupalUser) String() string {
if tn.Time.IsZero() { if tn.Time.IsZero() {
continue continue
} }
fmt.Fprintf(&builder, "%s: %q, ", tn.Name, tn.Time.Format(time.Stamp)) fmt.Fprintf(builder, "%s: %q, ", tn.Name, tn.Time.Format(time.Stamp))
} }
fmt.Fprintf(&builder, "Roles: %s", du.Roles) fmt.Fprintf(builder, "Roles: %s", du.Roles)
return builder.String() return builder.String()
} }

View file

@ -4,7 +4,8 @@ package password
import ( import (
"crypto/rand" "crypto/rand"
"math/big" "math/big"
"strings"
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
) )
// NOTE(twiesing): A bunch of scripts cannot properly handle the extra characters in the password. // NOTE(twiesing): A bunch of scripts cannot properly handle the extra characters in the password.
@ -23,7 +24,8 @@ func Password(length int) (string, error) {
} }
// create a buffer to write the string to! // create a buffer to write the string to!
var password strings.Builder password := pools.GetBuilder()
defer pools.ReleaseBuilder(password)
password.Grow(length) password.Grow(length)
for i := 0; i < length; i++ { for i := 0; i < length; i++ {