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.
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// magpie model tier: a model's own speed (kdajenks on X).
|
|
|
|
const tierUsage = `usage:
|
|
magpie model tier <provider/model> the speed it is sent at
|
|
magpie model tier <provider/model> fast send every request for it in its vendor's fast mode
|
|
magpie model tier <provider/model> ultrafast send it in the faster Ultrafast mode where an account offers it
|
|
(priority where not)
|
|
magpie model tier <provider/model> usual the usual speed again (also --reset)
|
|
|
|
An agent's own fast pick (the bolt in its model list) goes first, and a routing group
|
|
sends its members as it says.`
|
|
|
|
func modelTierCmd(args []string) error {
|
|
if len(args) == 0 {
|
|
return fmt.Errorf("%s", tierUsage)
|
|
}
|
|
if slices.ContainsFunc(args, isHelp) {
|
|
fmt.Println(tierUsage)
|
|
return nil
|
|
}
|
|
p, model, err := modelRef(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(args) == 1 {
|
|
fmt.Printf("%s/%s: %s\n", p.ID, model, tierWord(provider.ModelTier(p.ID, model)))
|
|
return nil
|
|
}
|
|
val := strings.TrimSpace(args[1])
|
|
if isReset([]string{val}) {
|
|
val = ""
|
|
}
|
|
if err := provider.SetModelTier(p.ID+"/"+model, val); err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("%s/%s: %s\n", p.ID, model, tierWord(settings.Shared().ModelTiers[p.ID+"/"+model]))
|
|
return nil
|
|
}
|
|
|
|
func tierWord(t string) string {
|
|
switch t {
|
|
case "priority":
|
|
return "fast"
|
|
case "ultrafast":
|
|
return "ultrafast"
|
|
}
|
|
return "usual"
|
|
}
|