From 61c3c7300dc4bf992d1d41fcc6f31168643a497a Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Thu, 23 Jul 2026 21:47:45 -0700 Subject: Avoid some allocations for the index pages --- roseh.moe.go | 104 ++++++++++++----------------------------------------------- 1 file changed, 21 insertions(+), 83 deletions(-) diff --git a/roseh.moe.go b/roseh.moe.go index 84f3abf..9daecb5 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -88,19 +88,21 @@ func loadSecrets() error { } type packageConfig struct { - url string - description string + Name string `xml:",attr"` + URL string `xml:",attr"` + Description string `xml:",attr"` } type commandConfig struct { - url string - description string - multi bool + Name string `xml:",attr"` + URL string `xml:",attr"` + Description string `xml:",attr"` + Multi bool `xml:",attr"` } type serviceConfiguration struct { - packageRedirects map[string]packageConfig - commandRedirects map[string]commandConfig + PackageRedirects []packageConfig `xml:"Redirects>Package"` + CommandRedirects []commandConfig `xml:"Redirects>Command"` } var ( @@ -127,39 +129,12 @@ func loadConfig() (*serviceConfiguration, error) { if err != nil { return nil, err } - var fileConfig struct { - Package []struct { - Name string `xml:",attr"` - URL string `xml:",attr"` - Description string `xml:",attr"` - } `xml:"Redirects>Package"` - Command []struct { - Name string `xml:",attr"` - URL string `xml:",attr"` - Description string `xml:",attr"` - Multi bool `xml:",attr"` - } `xml:"Redirects>Command"` - } - if err := xml.Unmarshal(fileBytes, &fileConfig); err != nil { + config := new(serviceConfiguration) + if err := xml.Unmarshal(fileBytes, config); err != nil { return nil, err } - config := &serviceConfiguration{ - packageRedirects: make(map[string]packageConfig), - commandRedirects: make(map[string]commandConfig), - } - for _, pkg := range fileConfig.Package { - config.packageRedirects[pkg.Name] = packageConfig{ - url: pkg.URL, - description: pkg.Description, - } - } - for _, cmd := range fileConfig.Command { - config.commandRedirects[cmd.Name] = commandConfig{ - url: cmd.URL, - description: cmd.Description, - multi: cmd.Multi, - } - } + slices.SortFunc(config.PackageRedirects, func(x, y packageConfig) int { return strings.Compare(x.Name, y.Name) }) + slices.SortFunc(config.CommandRedirects, func(x, y commandConfig) int { return strings.Compare(x.Name, y.Name) }) serviceConfigMu.Lock() serviceConfig = config serviceConfigLastModified = newModTime @@ -818,13 +793,13 @@ func cmd(w http.ResponseWriter, r *http.Request) { http.Error(w, fmt.Sprintf("load config: %s", err), http.StatusInternalServerError) return } - source, ok := serviceConfig.commandRedirects[r.PathValue("cmd")] + sourceIdx, ok := slices.BinarySearchFunc(serviceConfig.CommandRedirects, r.PathValue("cmd"), func(cmd commandConfig, name string) int { return strings.Compare(cmd.Name, name) }) if !ok { notFound(w, r) return } if r.FormValue("go-get") == "1" { - fmt.Fprintf(w, ``, r.PathValue("cmd"), source.url) + fmt.Fprintf(w, ``, r.PathValue("cmd"), serviceConfig.CommandRedirects[sourceIdx].URL) } else { http.Redirect(w, r, "https://pkg.go.dev/roseh.moe/cmd/"+r.PathValue("cmd"), http.StatusFound) } @@ -836,13 +811,8 @@ var ( pkgIndexTemplate = template.Must(outlinedTemplate(pkgIndexString)) ) -type pkgIndexTemplatePackage struct { - Name string - Description string -} - type pkgIndexTemplateArgs struct { - Packages []pkgIndexTemplatePackage + Packages []packageConfig } func pkgIndex(w http.ResponseWriter, r *http.Request) { @@ -852,19 +822,7 @@ func pkgIndex(w http.ResponseWriter, r *http.Request) { http.Error(w, "Failed to load config", http.StatusInternalServerError) return } - packages := make([]string, 0, len(serviceConfig.packageRedirects)) - for pkg := range serviceConfig.packageRedirects { - packages = append(packages, pkg) - } - slices.Sort(packages) - templatePackages := make([]pkgIndexTemplatePackage, len(packages)) - for i, pkg := range packages { - templatePackages[i] = pkgIndexTemplatePackage{ - Name: pkg, - Description: serviceConfig.packageRedirects[pkg].description, - } - } - if err := pkgIndexTemplate.Execute(w, pkgIndexTemplateArgs{Packages: templatePackages}); err != nil { + if err := pkgIndexTemplate.Execute(w, pkgIndexTemplateArgs{Packages: serviceConfig.PackageRedirects}); err != nil { log.Printf("Warning: pkgIndex: %s", err) } } @@ -875,14 +833,8 @@ var ( cmdIndexTemplate = template.Must(outlinedTemplate(cmdIndexString)) ) -type cmdIndexTemplateCommand struct { - Name string - Description string - Multi bool -} - type cmdIndexTemplateArgs struct { - Commands []cmdIndexTemplateCommand + Commands []commandConfig } func cmdIndex(w http.ResponseWriter, r *http.Request) { @@ -892,21 +844,7 @@ func cmdIndex(w http.ResponseWriter, r *http.Request) { http.Error(w, "Failed to load config", http.StatusInternalServerError) return } - commands := make([]string, 0, len(serviceConfig.commandRedirects)) - for cmd := range serviceConfig.commandRedirects { - commands = append(commands, cmd) - } - slices.Sort(commands) - templateCommands := make([]cmdIndexTemplateCommand, len(commands)) - for i, name := range commands { - cmd := serviceConfig.commandRedirects[name] - templateCommands[i] = cmdIndexTemplateCommand{ - Name: name, - Description: cmd.description, - Multi: cmd.multi, - } - } - if err := cmdIndexTemplate.Execute(w, cmdIndexTemplateArgs{Commands: templateCommands}); err != nil { + if err := cmdIndexTemplate.Execute(w, cmdIndexTemplateArgs{Commands: serviceConfig.CommandRedirects}); err != nil { log.Printf("Warning: pkgIndex: %s", err) } } @@ -918,13 +856,13 @@ func pkg(w http.ResponseWriter, r *http.Request) { http.Error(w, "Failed to load config", http.StatusInternalServerError) return } - source, ok := serviceConfig.packageRedirects[r.PathValue("pkg")] + sourceIdx, ok := slices.BinarySearchFunc(serviceConfig.PackageRedirects, r.PathValue("pkg"), func(pkg packageConfig, name string) int { return strings.Compare(pkg.Name, name) }) if !ok { notFound(w, r) return } if r.FormValue("go-get") == "1" { - fmt.Fprintf(w, ``, r.PathValue("pkg"), source.url) + fmt.Fprintf(w, ``, r.PathValue("pkg"), serviceConfig.PackageRedirects[sourceIdx].URL) } else { http.Redirect(w, r, "https://pkg.go.dev/roseh.moe/pkg/"+r.PathValue("pkg"), http.StatusFound) } -- cgit v1.3.1