perf(gui): the page's mime types are added when a page is served, not at launch
gui's init called mime.AddExtensionType, and the mime package reads and parses the system's whole type table (/etc/apache2/mime.types on macOS, /etc/mime.types on Linux) the first time anything asks it. So every launch, `magpie statusline` on each Claude Code message included, paid ~18,000 allocations for a table only a served page uses. Handler now adds the types once (sync.OnceFunc) before it builds the mux. TestAssetTypesHoldAgainstTheRegistry now sets each type wrong first and asks Handler to set it right. The old form passed with the call removed on both mote and the Windows box (both tables already agree), so it guarded nothing; with the call removed the new one is red on mote (.css, .js, .html, .svg, .png served as application/x-other). Measured on mote, sandbox HOME, t43 (v0.1.3517) against t44 (this): - GODEBUG=inittrace=1 sum: 4.63 MB / 46,795 allocs -> 3.94 MB / 28,742 - `magpie statusline`, 3 runs: instructions retired ~177M -> ~156M, peak footprint 16.4-17.0 MB -> 16.1-16.3 MB Run: go vet ./internal/gui (darwin, GOOS=windows) ok; go test ./internal/gui ok (130s); root go vet ./... after rebase below.
This commit is contained in:
+9
-1
@@ -997,7 +997,14 @@ var onDock func(settings.Settings)
|
||||
var assetTypes = map[string]string{".svg": "image/svg+xml", ".png": "image/png", ".css": "text/css; charset=utf-8",
|
||||
".js": "text/javascript; charset=utf-8", ".html": "text/html; charset=utf-8"}
|
||||
|
||||
func init() {
|
||||
// addAssetTypes is called by Handler, not init: the mime package reads
|
||||
// and parses the system's whole type table (/etc/apache2/mime.types,
|
||||
// ~18,000 allocations) the first time a type is added, and a launch that
|
||||
// serves no page (`magpie statusline` on every Claude Code message) never
|
||||
// needs it.
|
||||
var addAssetTypes = sync.OnceFunc(registerAssetTypes)
|
||||
|
||||
func registerAssetTypes() {
|
||||
for ext, t := range assetTypes {
|
||||
mime.AddExtensionType(ext, t)
|
||||
}
|
||||
@@ -1075,6 +1082,7 @@ func versionedPage(page []byte) []byte {
|
||||
// gw is the gateway this process serves, or nil when another magpie has it
|
||||
// (for now: see startBackend).
|
||||
func Handler(w Windows, gw *gateway.Server) http.Handler {
|
||||
addAssetTypes()
|
||||
if gw != nil {
|
||||
served.Store(gw)
|
||||
}
|
||||
|
||||
@@ -2,10 +2,20 @@ package gui
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Handler sets the page's own types even where the system's table says
|
||||
// otherwise (Windows' registry, which another program may have changed).
|
||||
func TestAssetTypesHoldAgainstTheRegistry(t *testing.T) {
|
||||
for ext := range assetTypes {
|
||||
mime.AddExtensionType(ext, "application/x-other")
|
||||
}
|
||||
was := addAssetTypes
|
||||
addAssetTypes = sync.OnceFunc(registerAssetTypes)
|
||||
t.Cleanup(func() { addAssetTypes = was; registerAssetTypes() })
|
||||
Handler(nil, nil)
|
||||
for ext, want := range assetTypes {
|
||||
if got := mime.TypeByExtension(ext); got != want {
|
||||
t.Errorf("%s served as %q, not %q", ext, got, want)
|
||||
|
||||
Reference in New Issue
Block a user