cache default noavatar strings to avoid allocs

This commit is contained in:
Azareal 2020-02-07 22:27:33 +10:00
parent 812471d15f
commit e37c98eaa1
2 changed files with 25 additions and 6 deletions

View File

@ -95,11 +95,13 @@ type config struct {
PostIPCutoff int PostIPCutoff int
PollIPCutoff int PollIPCutoff int
LogPruneCutoff int LogPruneCutoff int
//SelfDeleteTruncCutoff int // Personal data is stripped from the mod action rows only leaving the TID and the action for later investigation.
DisableLastIP bool DisableLastIP bool
DisablePostIP bool DisablePostIP bool
DisablePollIP bool DisablePollIP bool
DisableRegLog bool DisableRegLog bool
//DisableSelfDeleteLog bool
DisableLiveTopicList bool DisableLiveTopicList bool
DisableJSAntispam bool DisableJSAntispam bool
@ -168,7 +170,18 @@ func LoadConfig() error {
return nil return nil
} }
var noavatarCache200 []string
var noavatarCache48 []string
func ProcessConfig() (err error) { func ProcessConfig() (err error) {
if !Config.DisableDefaultNoavatar {
noavatarCache200 = make([]string, 5)
noavatarCache48 = make([]string, 5)
for i := 0; i < 5; i++ {
noavatarCache200[i] = "/s/n" + strconv.Itoa(i) + "-" + strconv.Itoa(200) + ".png?i=0"
noavatarCache48[i] = "/s/n" + strconv.Itoa(i) + "-" + strconv.Itoa(48) + ".png?i=0"
}
}
Config.Noavatar = strings.Replace(Config.Noavatar, "{site_url}", Site.URL, -1) Config.Noavatar = strings.Replace(Config.Noavatar, "{site_url}", Site.URL, -1)
guestAvatar = GuestAvatar{buildNoavatar(0, 200), buildNoavatar(0, 48)} guestAvatar = GuestAvatar{buildNoavatar(0, 200), buildNoavatar(0, 48)}

View File

@ -12,6 +12,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
//"log" //"log"
qgen "github.com/Azareal/Gosora/query_gen" qgen "github.com/Azareal/Gosora/query_gen"
@ -720,6 +721,11 @@ func buildNoavatar(uid, width int) string {
} }
} }
if !Config.DisableDefaultNoavatar && uid < 5 { if !Config.DisableDefaultNoavatar && uid < 5 {
if width == 200 {
return noavatarCache200[uid]
} else if width == 48 {
return noavatarCache48[uid]
}
return "/s/n" + strconv.Itoa(uid) + "-" + strconv.Itoa(width) + ".png?i=0" return "/s/n" + strconv.Itoa(uid) + "-" + strconv.Itoa(width) + ".png?i=0"
} }
return strings.Replace(strings.Replace(Config.Noavatar, "{id}", strconv.Itoa(uid), 1), "{width}", strconv.Itoa(width), 1) return strings.Replace(strings.Replace(Config.Noavatar, "{id}", strconv.Itoa(uid), 1), "{width}", strconv.Itoa(width), 1)
@ -727,7 +733,7 @@ func buildNoavatar(uid, width int) string {
// ? Make this part of *User? // ? Make this part of *User?
// TODO: Write tests for this // TODO: Write tests for this
func BuildAvatar(uid int, avatar string) (normalAvatar string, microAvatar string) { func BuildAvatar(uid int, avatar string) (normalAvatar, microAvatar string) {
if avatar == "" { if avatar == "" {
if uid == 0 { if uid == 0 {
return guestAvatar.Normal, guestAvatar.Micro return guestAvatar.Normal, guestAvatar.Micro