From 63359cdb1e875d82392e3a52a9b7e6db71ed0683 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Sun, 27 Nov 2022 21:30:54 -0400 Subject: [PATCH] perf: reorder struct fields this apparently saves in memory usage. not sure how this is going to impact normal usage, but... --- cmd/docgen/docgen.go | 41 +++++++++++++++++++++++++---------------- exec.go | 8 ++++---- golibs/bait/bait.go | 21 ++++++++++++--------- history.go | 4 ++-- job.go | 30 ++++++++++++++---------------- timer.go | 21 +++++++++++---------- 6 files changed, 68 insertions(+), 57 deletions(-) diff --git a/cmd/docgen/docgen.go b/cmd/docgen/docgen.go index 39a2a76..b1dcf14 100644 --- a/cmd/docgen/docgen.go +++ b/cmd/docgen/docgen.go @@ -2,13 +2,13 @@ package main import ( "fmt" - "path/filepath" "go/ast" "go/doc" "go/parser" "go/token" - "strings" "os" + "path/filepath" + "strings" ) type EmmyPiece struct { @@ -17,9 +17,9 @@ type EmmyPiece struct { Params []string // we only need to know param name to put in function } type DocPiece struct { - Doc []string - FuncSig string + FuncSig string FuncName string + Doc []string } // feel free to clean this up @@ -28,10 +28,9 @@ func main() { fset := token.NewFileSet() os.Mkdir("docs", 0777) os.Mkdir("emmyLuaDocs", 0777) - dirs := []string{"./"} - filepath.Walk("golibs/", func (path string, info os.FileInfo, err error) error { + filepath.Walk("golibs/", func(path string, info os.FileInfo, err error) error { if !info.IsDir() { return nil } @@ -65,8 +64,12 @@ func main() { p := doc.New(f, "./", doc.AllDecls) for _, t := range p.Funcs { mod := l - if strings.HasPrefix(t.Name, "hl") { mod = "hilbish" } - if !strings.HasPrefix(t.Name, prefix[mod]) || t.Name == "Loader" { continue } + if strings.HasPrefix(t.Name, "hl") { + mod = "hilbish" + } + if !strings.HasPrefix(t.Name, prefix[mod]) || t.Name == "Loader" { + continue + } parts := strings.Split(strings.TrimSpace(t.Doc), "\n") funcsig := parts[0] doc := parts[1:] @@ -88,19 +91,21 @@ func main() { funcdoc = append(funcdoc, d) } } - + dps := DocPiece{ - Doc: funcdoc, - FuncSig: funcsig, + Doc: funcdoc, + FuncSig: funcsig, FuncName: strings.TrimPrefix(t.Name, prefix[mod]), } - + docs[mod] = append(docs[mod], dps) emmyDocs[mod] = append(emmyDocs[mod], em) } for _, t := range p.Types { for _, m := range t.Methods { - if !strings.HasPrefix(m.Name, prefix[l]) || m.Name == "Loader" { continue } + if !strings.HasPrefix(m.Name, prefix[l]) || m.Name == "Loader" { + continue + } parts := strings.Split(strings.TrimSpace(m.Doc), "\n") funcsig := parts[0] doc := parts[1:] @@ -135,7 +140,9 @@ func main() { } for mod, v := range docs { - if mod == "main" { continue } + if mod == "main" { + continue + } f, _ := os.Create("docs/" + mod + ".txt") for _, dps := range v { f.WriteString(dps.FuncSig + " > ") @@ -147,9 +154,11 @@ func main() { f.WriteString("\n") } } - + for mod, v := range emmyDocs { - if mod == "main" { continue } + if mod == "main" { + continue + } f, _ := os.Create("emmyLuaDocs/" + mod + ".lua") f.WriteString("--- @meta\n\nlocal " + mod + " = {}\n\n") for _, em := range v { diff --git a/exec.go b/exec.go index caf7d1b..597cd26 100644 --- a/exec.go +++ b/exec.go @@ -4,10 +4,10 @@ import ( "bytes" "context" "errors" - "os/exec" "fmt" "io" "os" + "os/exec" "path/filepath" "runtime" "strings" @@ -19,21 +19,21 @@ import ( rt "github.com/arnodel/golua/runtime" "mvdan.cc/sh/v3/shell" //"github.com/yuin/gopher-lua/parse" + "mvdan.cc/sh/v3/expand" "mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/syntax" - "mvdan.cc/sh/v3/expand" ) var errNotExec = errors.New("not executable") var errNotFound = errors.New("not found") var runnerMode rt.Value = rt.StringValue("hybrid") -type execError struct{ +type execError struct { + err error typ string cmd string code int colon bool - err error } func (e execError) Error() string { diff --git a/golibs/bait/bait.go b/golibs/bait/bait.go index f071f92..e553d62 100644 --- a/golibs/bait/bait.go +++ b/golibs/bait/bait.go @@ -5,11 +5,12 @@ import ( "hilbish/util" - rt "github.com/arnodel/golua/runtime" "github.com/arnodel/golua/lib/packagelib" + rt "github.com/arnodel/golua/runtime" ) type listenerType int + const ( goListener listenerType = iota luaListener @@ -19,18 +20,18 @@ const ( type Recoverer func(event string, handler *Listener, err interface{}) // Listener is a struct that holds the handler for an event. -type Listener struct{ - typ listenerType - once bool +type Listener struct { caller func(...interface{}) luaCaller *rt.Closure + typ listenerType + once bool } -type Bait struct{ - Loader packagelib.Loader +type Bait struct { recoverer Recoverer - handlers map[string][]*Listener + handlers map[string][]*Listener rtm *rt.Runtime + Loader packagelib.Loader } // New creates a new Bait instance. @@ -316,8 +317,10 @@ func (b *Bait) bhooks(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { luaHandlers := rt.NewTable() for _, handler := range handlers { - if handler.typ != luaListener { continue } - luaHandlers.Set(rt.IntValue(luaHandlers.Len() + 1), rt.FunctionValue(handler.luaCaller)) + if handler.typ != luaListener { + continue + } + luaHandlers.Set(rt.IntValue(luaHandlers.Len()+1), rt.FunctionValue(handler.luaCaller)) } if luaHandlers.Len() == 0 { diff --git a/history.go b/history.go index a8eb089..63b7fc7 100644 --- a/history.go +++ b/history.go @@ -10,7 +10,7 @@ import ( rt "github.com/arnodel/golua/runtime" ) -type luaHistory struct {} +type luaHistory struct{} func (h *luaHistory) Write(line string) (int, error) { histWrite := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("add")) @@ -54,8 +54,8 @@ func (h *luaHistory) Dump() interface{} { } type fileHistory struct { + f *os.File items []string - f *os.File } func newFileHistory(path string) *fileHistory { diff --git a/job.go b/job.go index 709cc1f..cebc813 100644 --- a/job.go +++ b/job.go @@ -19,22 +19,20 @@ var jobs *jobHandler var jobMetaKey = rt.StringValue("hshjob") type job struct { - cmd string - running bool - id int - pid int - exitCode int - once bool - args []string - // save path for a few reasons, one being security (lmao) while the other - // would just be so itll be the same binary command always (path changes) - path string - handle *exec.Cmd - cmdout io.Writer cmderr io.Writer - stdout *bytes.Buffer - stderr *bytes.Buffer + cmdout io.Writer ud *rt.UserData + stderr *bytes.Buffer + stdout *bytes.Buffer + handle *exec.Cmd + path string + cmd string + args []string + exitCode int + pid int + id int + once bool + running bool } func (j *job) start() error { @@ -204,9 +202,9 @@ func luaBackgroundJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { type jobHandler struct { jobs map[int]*job - latestID int - foreground bool // if job currently in the foreground mu *sync.RWMutex + latestID int + foreground bool } func newJobHandler() *jobHandler { diff --git a/timer.go b/timer.go index 74d13c4..149d835 100644 --- a/timer.go +++ b/timer.go @@ -10,21 +10,22 @@ import ( ) type timerType int64 + const ( timerInterval timerType = iota timerTimeout ) -type timer struct{ +type timer struct { + ticker *time.Ticker + ud *rt.UserData + channel chan struct{} + fun *rt.Closure + th *timerHandler + dur time.Duration id int typ timerType running bool - dur time.Duration - fun *rt.Closure - th *timerHandler - ticker *time.Ticker - ud *rt.UserData - channel chan struct{} } func (t *timer) start() error { @@ -56,7 +57,7 @@ func (t *timer) start() error { } } }() - + return nil } @@ -69,7 +70,7 @@ func (t *timer) stop() error { t.running = false t.th.running-- t.th.wg.Done() - + return nil } @@ -87,7 +88,7 @@ func timerStart(thr *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err != nil { return nil, err } - + return c.Next(), nil }