a few micro opts for slices
Ted Unangst tedu@tedunangst.com
Tue, 05 Mar 2024 15:23:38 -0500
3 files changed,
25 insertions(+),
18 deletions(-)
M
database.go
→
database.go
@@ -386,7 +386,7 @@ elog.Printf("error querying honks: %s", err)
return nil } defer rows.Close() - var honks []*Honk + honks := make([]*Honk, 0, 64) for rows.Next() { h := scanhonk(rows) if h != nil {@@ -421,8 +421,8 @@ }
func donksforhonks(honks []*Honk) { db := opendatabase() - var ids []string - hmap := make(map[int64]*Honk) + ids := make([]string, 0, len(honks)) + hmap := make(map[int64]*Honk, len(honks)) for _, h := range honks { ids = append(ids, fmt.Sprintf("%d", h.ID)) hmap[h.ID] = h@@ -535,8 +535,8 @@ }
func donksforchonks(chonks []*Chonk) { db := opendatabase() - var ids []string - chmap := make(map[int64]*Chonk) + ids := make([]string, 0, len(chonks)) + chmap := make(map[int64]*Chonk, len(chonks)) for _, ch := range chonks { ids = append(ids, fmt.Sprintf("%d", ch.ID)) chmap[ch.ID] = ch
M
web.go
→
web.go
@@ -691,7 +691,7 @@ elog.Printf("query err: %s", err)
return } defer rows.Close() - var honkers []Honker + honkers := make([]Honker, 0, 256) for rows.Next() { var xid string rows.Scan(&xid)@@ -720,7 +720,7 @@ if len(honks) > 20 {
honks = honks[0:20] } - var jonks []junk.Junk + jonks := make([]junk.Junk, 0, 256) for _, h := range honks { j, _ := jonkjonk(user, h) jonks = append(jonks, j)@@ -920,7 +920,7 @@ if len(honks) > 40 {
honks = honks[0:40] } - var xids []string + xids := make([]string, 0, len(honks)) for _, h := range honks { xids = append(xids, h.XID) }@@ -963,7 +963,8 @@ elog.Printf("selection error: %s", err)
return } defer rows.Close() - var onts, pops []Ont + onts := make([]Ont, 0, 1024) + pops := make([]Ont, 0, 128) for rows.Next() { var o Ont err := rows.Scan(&o.Name, &o.Count)@@ -989,7 +990,7 @@ })
if len(pops) > 40 { pops = pops[:40] } - var letters []string + letters := make([]string, 0, 64) var lastrune rune = -1 for _, o := range onts { if r := firstRune(o.Name); r != lastrune {@@ -1170,15 +1171,15 @@ func threadsort(honks []*Honk) []*Honk {
sort.Slice(honks, func(i, j int) bool { return honks[i].Date.Before(honks[j].Date) }) - honkx := make(map[string]*Honk) - kids := make(map[string][]*Honk) + honkx := make(map[string]*Honk, len(honks)) + kids := make(map[string][]*Honk, len(honks)) for _, h := range honks { honkx[h.XID] = h rid := h.RID kids[rid] = append(kids[rid], h) } - done := make(map[*Honk]bool) - var thread []*Honk + done := make(map[*Honk]bool, len(honks)) + thread := make([]*Honk, 0, len(honks)) var nextlevel func(p *Honk) level := 0 nextlevel = func(p *Honk) {@@ -1335,7 +1336,7 @@ templinfo := getInfo(r)
rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy, 0) //reversehonks(rawhonks) rawhonks = threadsort(rawhonks) - var honks []*Honk + honks := make([]*Honk, 0, len(rawhonks)) for i, h := range rawhonks { if h.XID == xid { templinfo["Honkology"] = honkology(h)@@ -1668,10 +1669,10 @@ http.Error(w, "no editing that please", http.StatusInternalServerError)
return } noise := honk.Noise - var savedfiles []string honks := []*Honk{honk} donksforhonks(honks) + var savedfiles []string for _, d := range honk.Donks { savedfiles = append(savedfiles, fmt.Sprintf("%s:%d", d.XID, d.FileID)) }@@ -2098,7 +2099,7 @@
func showhonkers(w http.ResponseWriter, r *http.Request) { userid := UserID(login.GetUserInfo(r).UserID) honkers := gethonkers(userid) - var letters []string + letters := make([]string, 0, 64) var lastrune rune = -1 for _, h := range honkers { if r := firstRune(h.Name); r != lastrune {@@ -2181,7 +2182,7 @@ }
var combocache = gencache.New(gencache.Options[UserID, []string]{Fill: func(userid UserID) ([]string, bool) { honkers := gethonkers(userid) - var combos []string + combos := make([]string, 0, len(honkers)) for _, h := range honkers { combos = append(combos, h.Combos...) }@@ -2821,6 +2822,7 @@ if err == nil {
emunames, _ = dir.Readdirnames(0) dir.Close() } + allemus = make([]Emu, 0, len(emunames)) for _, e := range emunames { if len(e) <= 4 { continue