Files
magpie/internal/gui/stable_install.go
T
yetone 667ac8bd6f settings: a look at one setting takes the shared settings, not a copy
settings.Load() copies the whole Settings (clone + deepCopy of every map
and slice) for each caller. 133 places read one field of it and threw
the rest away: the proxy for every outgoing request (netproxy), the
GitHub token, the China mirror switch, a model's output cap, wire or
price on a gateway request. With a real account in the tray, Load's
clone was 26.6 MB of the 318 MB the process allocated in its first
seven minutes, most of it through netproxy.For on each request.

Each of them now reads settings.Shared(), the read-only settings every
caller is given for the same settings.json. Only reads were changed:
a scalar field (string, bool, int, float) or a map field's index
read. A caller that keeps a whole map or slice, or calls a method on
the settings, still takes Load's copy.
2026-11-14 14:02:49 +08:00

65 lines
2.0 KiB
Go

package gui
import (
"context"
"log"
"sync/atomic"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/update"
)
// A fresh install of the Stable channel's version stays on Stable
// (fishmagic80 on X). usemagpie.ai offers Stable's downloads beside the
// newest's, and Settings' Update channel is every version until changed,
// so an app put in from a Stable download was offered the newest at its
// first check. Before that check, a fresh install (whatsNew.start: no
// version run before, and magpie's folder new) asks the feed for Stable's
// version; when it is this one and a newer one is out, the channel becomes
// Stable. A channel already set is kept, and so is every version when
// this one is also the newest, where the download can't be told apart.
// freshInstall: this run is magpie's first here, and the channel isn't
// settled yet. A feed that can't be read leaves it for the next check.
var freshInstall atomic.Bool
// latestStable and latestEvery are update's; tests stand in for them.
var (
latestStable = update.LatestStable
latestEvery = func(ctx context.Context) (*update.Release, error) { return update.LatestEvery(ctx, "") }
)
// takeStable runs before an update check reads the channel.
func takeStable(ctx context.Context) {
if !freshInstall.Load() {
return
}
if !update.Released(Version) || settings.Shared().UpdateChannel != "" {
freshInstall.Store(false)
return
}
st, err := latestStable(ctx)
if err != nil {
log.Println("stable on a fresh install:", err) // unknown: asked again next check
return
}
if st.Version != Version {
freshInstall.Store(false)
return
}
every, err := latestEvery(ctx)
if err != nil {
log.Println("stable on a fresh install:", err)
return
}
freshInstall.Store(false)
if !update.Newer(every.Version, Version) {
return
}
if took, err := settings.TakeStable(); err != nil {
log.Println("stable on a fresh install:", err)
} else if took {
log.Printf("a fresh install of Stable's %s: the update channel is Stable", Version)
}
}