Skip to content

Commit

Permalink
perf(dynamicicon): Preallocate image buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jan 30, 2025
1 parent e03748c commit 79c861c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.3

require (
fyne.io/systray v1.11.0
gabe565.com/utils v0.0.0-20241114234101-e128cd3269b5
gabe565.com/utils v0.0.0-20250130072635-ed882da6fcdd
github.com/Masterminds/sprig/v3 v3.3.0
github.com/dmarkham/enumer v1.5.10
github.com/emersion/go-autostart v0.0.0-20210130080809-00ed301c8e9a
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
fyne.io/systray v1.11.0 h1:D9HISlxSkx+jHSniMBR6fCFOUjk1x/OOOJLa9lJYAKg=
fyne.io/systray v1.11.0/go.mod h1:RVwqP9nYMo7h5zViCBHri2FgjXF7H2cub7MAq4NSoLs=
gabe565.com/utils v0.0.0-20241114234101-e128cd3269b5 h1:uqtftvk1FMAsFPAT9ICLLntGsZHmrghgIGBClVCsVHk=
gabe565.com/utils v0.0.0-20241114234101-e128cd3269b5/go.mod h1:1WioSVukwGZYG4Q0LJBnRhgYyVljmW2Izl+RW36ALUc=
gabe565.com/utils v0.0.0-20250130072635-ed882da6fcdd h1:ATna6PCWp9+8x3hBC/miPdvu4uVDRj4zw2QYNW6OaV0=
gabe565.com/utils v0.0.0-20250130072635-ed882da6fcdd/go.mod h1:6FZP8OeF0k3hTsPClyDGEILcBg3hvGcSp4u4pmjMusY=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
Expand Down
6 changes: 5 additions & 1 deletion internal/dynamicicon/dynamicicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"log/slog"
"os"
"path/filepath"
"slices"
"sync"
"time"

"gabe565.com/nightscout-menu-bar/internal/config"
"gabe565.com/nightscout-menu-bar/internal/nightscout"
"gabe565.com/nightscout-menu-bar/internal/util"
"gabe565.com/utils/bytefmt"
"github.com/flopp/go-findfont"
"github.com/goki/freetype/truetype"
"golang.org/x/image/font"
Expand Down Expand Up @@ -133,15 +135,17 @@ func (d *DynamicIcon) Generate(p *nightscout.Properties) ([]byte, error) {
drawer.DrawString(bgnow)

var buf bytes.Buffer
buf.Grow(2 * bytefmt.KiB)
if err := encode(&buf, d.img); err != nil {
return nil, err
}

slog.Debug("Generated dynamic icon",
"took", time.Since(start),
"size", bytefmt.Encode(int64(buf.Len())),
"font_size", fontSize,
"value", bgnow,
"size", buf.Len(),
)
return buf.Bytes(), nil
return slices.Clip(buf.Bytes()), nil
}

0 comments on commit 79c861c

Please sign in to comment.