perf: reorder struct fields

this apparently saves in memory usage. not sure
how this is going to impact normal usage, but...
TorchedSammy 2022-11-27 21:30:54 -04:00
parent bd4e0df7b3
commit 63359cdb1e
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
6 changed files with 68 additions and 57 deletions

View File

@ -2,13 +2,13 @@ package main
import ( import (
"fmt" "fmt"
"path/filepath"
"go/ast" "go/ast"
"go/doc" "go/doc"
"go/parser" "go/parser"
"go/token" "go/token"
"strings"
"os" "os"
"path/filepath"
"strings"
) )
type EmmyPiece struct { type EmmyPiece struct {
@ -17,9 +17,9 @@ type EmmyPiece struct {
Params []string // we only need to know param name to put in function Params []string // we only need to know param name to put in function
} }
type DocPiece struct { type DocPiece struct {
Doc []string FuncSig string
FuncSig string
FuncName string FuncName string
Doc []string
} }
// feel free to clean this up // feel free to clean this up
@ -29,9 +29,8 @@ func main() {
os.Mkdir("docs", 0777) os.Mkdir("docs", 0777)
os.Mkdir("emmyLuaDocs", 0777) os.Mkdir("emmyLuaDocs", 0777)
dirs := []string{"./"} 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() { if !info.IsDir() {
return nil return nil
} }
@ -65,8 +64,12 @@ func main() {
p := doc.New(f, "./", doc.AllDecls) p := doc.New(f, "./", doc.AllDecls)
for _, t := range p.Funcs { for _, t := range p.Funcs {
mod := l mod := l
if strings.HasPrefix(t.Name, "hl") { mod = "hilbish" } if strings.HasPrefix(t.Name, "hl") {
if !strings.HasPrefix(t.Name, prefix[mod]) || t.Name == "Loader" { continue } mod = "hilbish"
}
if !strings.HasPrefix(t.Name, prefix[mod]) || t.Name == "Loader" {
continue
}
parts := strings.Split(strings.TrimSpace(t.Doc), "\n") parts := strings.Split(strings.TrimSpace(t.Doc), "\n")
funcsig := parts[0] funcsig := parts[0]
doc := parts[1:] doc := parts[1:]
@ -90,8 +93,8 @@ func main() {
} }
dps := DocPiece{ dps := DocPiece{
Doc: funcdoc, Doc: funcdoc,
FuncSig: funcsig, FuncSig: funcsig,
FuncName: strings.TrimPrefix(t.Name, prefix[mod]), FuncName: strings.TrimPrefix(t.Name, prefix[mod]),
} }
@ -100,7 +103,9 @@ func main() {
} }
for _, t := range p.Types { for _, t := range p.Types {
for _, m := range t.Methods { 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") parts := strings.Split(strings.TrimSpace(m.Doc), "\n")
funcsig := parts[0] funcsig := parts[0]
doc := parts[1:] doc := parts[1:]
@ -135,7 +140,9 @@ func main() {
} }
for mod, v := range docs { for mod, v := range docs {
if mod == "main" { continue } if mod == "main" {
continue
}
f, _ := os.Create("docs/" + mod + ".txt") f, _ := os.Create("docs/" + mod + ".txt")
for _, dps := range v { for _, dps := range v {
f.WriteString(dps.FuncSig + " > ") f.WriteString(dps.FuncSig + " > ")
@ -149,7 +156,9 @@ func main() {
} }
for mod, v := range emmyDocs { for mod, v := range emmyDocs {
if mod == "main" { continue } if mod == "main" {
continue
}
f, _ := os.Create("emmyLuaDocs/" + mod + ".lua") f, _ := os.Create("emmyLuaDocs/" + mod + ".lua")
f.WriteString("--- @meta\n\nlocal " + mod + " = {}\n\n") f.WriteString("--- @meta\n\nlocal " + mod + " = {}\n\n")
for _, em := range v { for _, em := range v {

View File

@ -4,10 +4,10 @@ import (
"bytes" "bytes"
"context" "context"
"errors" "errors"
"os/exec"
"fmt" "fmt"
"io" "io"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -19,21 +19,21 @@ import (
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
"mvdan.cc/sh/v3/shell" "mvdan.cc/sh/v3/shell"
//"github.com/yuin/gopher-lua/parse" //"github.com/yuin/gopher-lua/parse"
"mvdan.cc/sh/v3/expand"
"mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax" "mvdan.cc/sh/v3/syntax"
"mvdan.cc/sh/v3/expand"
) )
var errNotExec = errors.New("not executable") var errNotExec = errors.New("not executable")
var errNotFound = errors.New("not found") var errNotFound = errors.New("not found")
var runnerMode rt.Value = rt.StringValue("hybrid") var runnerMode rt.Value = rt.StringValue("hybrid")
type execError struct{ type execError struct {
err error
typ string typ string
cmd string cmd string
code int code int
colon bool colon bool
err error
} }
func (e execError) Error() string { func (e execError) Error() string {

View File

@ -5,11 +5,12 @@ import (
"hilbish/util" "hilbish/util"
rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib" "github.com/arnodel/golua/lib/packagelib"
rt "github.com/arnodel/golua/runtime"
) )
type listenerType int type listenerType int
const ( const (
goListener listenerType = iota goListener listenerType = iota
luaListener luaListener
@ -19,18 +20,18 @@ const (
type Recoverer func(event string, handler *Listener, err interface{}) type Recoverer func(event string, handler *Listener, err interface{})
// Listener is a struct that holds the handler for an event. // Listener is a struct that holds the handler for an event.
type Listener struct{ type Listener struct {
typ listenerType
once bool
caller func(...interface{}) caller func(...interface{})
luaCaller *rt.Closure luaCaller *rt.Closure
typ listenerType
once bool
} }
type Bait struct{ type Bait struct {
Loader packagelib.Loader
recoverer Recoverer recoverer Recoverer
handlers map[string][]*Listener handlers map[string][]*Listener
rtm *rt.Runtime rtm *rt.Runtime
Loader packagelib.Loader
} }
// New creates a new Bait instance. // 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() luaHandlers := rt.NewTable()
for _, handler := range handlers { for _, handler := range handlers {
if handler.typ != luaListener { continue } if handler.typ != luaListener {
luaHandlers.Set(rt.IntValue(luaHandlers.Len() + 1), rt.FunctionValue(handler.luaCaller)) continue
}
luaHandlers.Set(rt.IntValue(luaHandlers.Len()+1), rt.FunctionValue(handler.luaCaller))
} }
if luaHandlers.Len() == 0 { if luaHandlers.Len() == 0 {

View File

@ -10,7 +10,7 @@ import (
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
) )
type luaHistory struct {} type luaHistory struct{}
func (h *luaHistory) Write(line string) (int, error) { func (h *luaHistory) Write(line string) (int, error) {
histWrite := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("add")) histWrite := hshMod.Get(rt.StringValue("history")).AsTable().Get(rt.StringValue("add"))
@ -54,8 +54,8 @@ func (h *luaHistory) Dump() interface{} {
} }
type fileHistory struct { type fileHistory struct {
f *os.File
items []string items []string
f *os.File
} }
func newFileHistory(path string) *fileHistory { func newFileHistory(path string) *fileHistory {

30
job.go
View File

@ -19,22 +19,20 @@ var jobs *jobHandler
var jobMetaKey = rt.StringValue("hshjob") var jobMetaKey = rt.StringValue("hshjob")
type job struct { 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 cmderr io.Writer
stdout *bytes.Buffer cmdout io.Writer
stderr *bytes.Buffer
ud *rt.UserData 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 { func (j *job) start() error {
@ -204,9 +202,9 @@ func luaBackgroundJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
type jobHandler struct { type jobHandler struct {
jobs map[int]*job jobs map[int]*job
latestID int
foreground bool // if job currently in the foreground
mu *sync.RWMutex mu *sync.RWMutex
latestID int
foreground bool
} }
func newJobHandler() *jobHandler { func newJobHandler() *jobHandler {

View File

@ -10,21 +10,22 @@ import (
) )
type timerType int64 type timerType int64
const ( const (
timerInterval timerType = iota timerInterval timerType = iota
timerTimeout 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 id int
typ timerType typ timerType
running bool running bool
dur time.Duration
fun *rt.Closure
th *timerHandler
ticker *time.Ticker
ud *rt.UserData
channel chan struct{}
} }
func (t *timer) start() error { func (t *timer) start() error {