From 06f0779525da27fb1a0cec466d563ad676133ffc Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 12 Nov 2019 16:56:51 +1000 Subject: [PATCH] Shorten some names and eliminate a few allocs. --- common/menu_item_store.go | 4 ++-- common/profile_reply_store.go | 7 ++++--- common/reply.go | 2 +- common/reply_store.go | 9 +++++---- common/topic_list.go | 31 +++++++++++++++---------------- common/topic_store.go | 6 +++--- common/word_filters.go | 2 +- 7 files changed, 31 insertions(+), 30 deletions(-) diff --git a/common/menu_item_store.go b/common/menu_item_store.go index 8302e45d..1f51f26b 100644 --- a/common/menu_item_store.go +++ b/common/menu_item_store.go @@ -13,10 +13,10 @@ func NewDefaultMenuItemStore() *DefaultMenuItemStore { } } -func (s *DefaultMenuItemStore) Add(item MenuItem) { +func (s *DefaultMenuItemStore) Add(i MenuItem) { s.lock.Lock() defer s.lock.Unlock() - s.items[item.ID] = item + s.items[i.ID] = i } func (s *DefaultMenuItemStore) Get(id int) (MenuItem, error) { diff --git a/common/profile_reply_store.go b/common/profile_reply_store.go index 7edd1b41..908ad755 100644 --- a/common/profile_reply_store.go +++ b/common/profile_reply_store.go @@ -23,10 +23,11 @@ type SQLProfileReplyStore struct { } func NewSQLProfileReplyStore(acc *qgen.Accumulator) (*SQLProfileReplyStore, error) { + ur := "users_replies" return &SQLProfileReplyStore{ - get: acc.Select("users_replies").Columns("uid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress").Where("rid = ?").Prepare(), - create: acc.Insert("users_replies").Columns("uid, content, parsed_content, createdAt, createdBy, ipaddress").Fields("?,?,?,UTC_TIMESTAMP(),?,?").Prepare(), - count: acc.Count("users_replies").Prepare(), + get: acc.Select(ur).Columns("uid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress").Where("rid = ?").Prepare(), + create: acc.Insert(ur).Columns("uid, content, parsed_content, createdAt, createdBy, ipaddress").Fields("?,?,?,UTC_TIMESTAMP(),?,?").Prepare(), + count: acc.Count(ur).Prepare(), }, acc.FirstError() } diff --git a/common/reply.go b/common/reply.go index aa3c5e2f..03c4c31a 100644 --- a/common/reply.go +++ b/common/reply.go @@ -81,7 +81,7 @@ type ReplyStmts struct { func init() { DbInits.Add(func(acc *qgen.Accumulator) error { replyStmts = ReplyStmts{ - isLiked: acc.Select("likes").Columns("targetItem").Where("sentBy = ? and targetItem = ? and targetType = 'replies'").Prepare(), + isLiked: acc.Select("likes").Columns("targetItem").Where("sentBy = ? and targetItem = ? and targetType='replies'").Prepare(), createLike: acc.Insert("likes").Columns("weight, targetItem, targetType, sentBy").Fields("?,?,?,?").Prepare(), edit: acc.Update("replies").Set("content = ?, parsed_content = ?").Where("rid = ? AND poll = 0").Prepare(), setPoll: acc.Update("replies").Set("poll = ?").Where("rid = ? AND poll = 0").Prepare(), diff --git a/common/reply_store.go b/common/reply_store.go index 23f62211..bbaa3348 100644 --- a/common/reply_store.go +++ b/common/reply_store.go @@ -8,7 +8,7 @@ var Rstore ReplyStore type ReplyStore interface { Get(id int) (*Reply, error) - Create(topic *Topic, content string, ipaddress string, uid int) (id int, err error) + Create(topic *Topic, content string, ip string, uid int) (id int, err error) Count() (count int) SetCache(cache ReplyCache) @@ -27,11 +27,12 @@ func NewSQLReplyStore(acc *qgen.Accumulator, cache ReplyCache) (*SQLReplyStore, if cache == nil { cache = NewNullReplyCache() } + re := "replies" return &SQLReplyStore{ cache: cache, - get: acc.Select("replies").Columns("tid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount, attachCount, actionType").Where("rid = ?").Prepare(), - create: acc.Insert("replies").Columns("tid, content, parsed_content, createdAt, lastUpdated, ipaddress, words, createdBy").Fields("?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?").Prepare(), - count: acc.Count("replies").Prepare(), + get: acc.Select(re).Columns("tid, content, createdBy, createdAt, lastEdit, lastEditBy, ipaddress, likeCount, attachCount, actionType").Where("rid = ?").Prepare(), + create: acc.Insert(re).Columns("tid, content, parsed_content, createdAt, lastUpdated, ipaddress, words, createdBy").Fields("?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),?,?,?").Prepare(), + count: acc.Count(re).Prepare(), }, acc.FirstError() } diff --git a/common/topic_list.go b/common/topic_list.go index bf9d6a8d..3c65ee8b 100644 --- a/common/topic_list.go +++ b/common/topic_list.go @@ -62,11 +62,11 @@ func (tList *DefaultTopicList) Tick() error { oddLists := make(map[int]*TopicListHolder) evenLists := make(map[int]*TopicListHolder) - addList := func(gid int, holder *TopicListHolder) { + addList := func(gid int, hold *TopicListHolder) { if gid%2 == 0 { - evenLists[gid] = holder + evenLists[gid] = hold } else { - oddLists[gid] = holder + oddLists[gid] = hold } } @@ -127,19 +127,19 @@ func (tList *DefaultTopicList) GetListByGroup(group *Group, page int, orderby st } // TODO: Cache the first three pages not just the first along with all the topics on this beaten track if page == 1 && orderby == "" && len(filterIDs) == 0 { - var holder *TopicListHolder + var hold *TopicListHolder var ok bool if group.ID%2 == 0 { tList.evenLock.RLock() - holder, ok = tList.evenGroups[group.ID] + hold, ok = tList.evenGroups[group.ID] tList.evenLock.RUnlock() } else { tList.oddLock.RLock() - holder, ok = tList.oddGroups[group.ID] + hold, ok = tList.oddGroups[group.ID] tList.oddLock.RUnlock() } if ok { - return holder.List, holder.ForumList, holder.Paginator, nil + return hold.List, hold.ForumList, hold.Paginator, nil } } @@ -153,9 +153,9 @@ func (tList *DefaultTopicList) GetListByCanSee(canSee []int, page int, orderby s // We need a list of the visible forums for Quick Topic // ? - Would it be useful, if we could post in social groups from /topics/? for _, fid := range canSee { - forum := Forums.DirtyGet(fid) - if forum.Name != "" && forum.Active && (forum.ParentType == "" || forum.ParentType == "forum") { - fcopy := forum.Copy() + f := Forums.DirtyGet(fid) + if f.Name != "" && f.Active && (f.ParentType == "" || f.ParentType == "forum") { + fcopy := f.Copy() // TODO: Add a hook here for plugin_guilds forumList = append(forumList, fcopy) } @@ -223,9 +223,9 @@ func (tList *DefaultTopicList) GetList(page int, orderby string, filterIDs []int // We need a list of the visible forums for Quick Topic // ? - Would it be useful, if we could post in social groups from /topics/? for _, fid := range canSee { - forum := Forums.DirtyGet(fid) - if forum.Name != "" && forum.Active && (forum.ParentType == "" || forum.ParentType == "forum") { - fcopy := forum.Copy() + f := Forums.DirtyGet(fid) + if f.Name != "" && f.Active && (f.ParentType == "" || f.ParentType == "forum") { + fcopy := f.Copy() // TODO: Add a hook here for plugin_guilds forumList = append(forumList, fcopy) } @@ -260,7 +260,7 @@ func (tList *DefaultTopicList) getList(page int, orderby string, argList []inter } // TODO: Prepare common qlist lengths to speed this up in common cases, prepared statements are prepared lazily anyway, so it probably doesn't matter if we do ten or so - stmt, err := qgen.Builder.SimpleSelect("topics", "tid, title, content, createdBy, is_closed, sticky, createdAt, lastReplyAt, lastReplyBy, lastReplyID, parentID, views, postCount, likeCount, attachCount, poll, data", "parentID IN("+qlist+")", orderq, "?,?") + stmt, err := qgen.Builder.SimpleSelect("topics", "tid,title,content,createdBy,is_closed,sticky,createdAt,lastReplyAt,lastReplyBy,lastReplyID,parentID,views,postCount,likeCount,attachCount,poll,data", "parentID IN("+qlist+")", orderq, "?,?") if err != nil { return nil, Paginator{nil, 1, 1}, err } @@ -338,8 +338,7 @@ func (tList *DefaultTopicList) getList(page int, orderby string, argList []inter } } } - err = rows.Err() - if err != nil { + if err = rows.Err(); err != nil { return nil, Paginator{nil, 1, 1}, err } diff --git a/common/topic_store.go b/common/topic_store.go index a9ed4c9c..158e52e3 100644 --- a/common/topic_store.go +++ b/common/topic_store.go @@ -197,7 +197,7 @@ func (s *DefaultTopicStore) Exists(id int) bool { return s.exists.QueryRow(id).Scan(&id) == nil } -func (s *DefaultTopicStore) Create(fid int, topicName string, content string, uid int, ipaddress string) (tid int, err error) { +func (s *DefaultTopicStore) Create(fid int, topicName string, content string, uid int, ip string) (tid int, err error) { if topicName == "" { return 0, ErrNoTitle } @@ -210,9 +210,9 @@ func (s *DefaultTopicStore) Create(fid int, topicName string, content string, ui if parsedContent == "" { return 0, ErrNoBody } - + // TODO: Move this statement into the topic store - res, err := s.create.Exec(fid, topicName, content, parsedContent, uid, ipaddress, WordCount(content), uid) + res, err := s.create.Exec(fid, topicName, content, parsedContent, uid, ip, WordCount(content), uid) if err != nil { return 0, err } diff --git a/common/word_filters.go b/common/word_filters.go index b415786a..1d27cee7 100644 --- a/common/word_filters.go +++ b/common/word_filters.go @@ -26,7 +26,7 @@ type WordFilterStore interface { ReloadAll() error GetAll() (filters map[int]*WordFilter, err error) Get(id int) (*WordFilter, error) - Create(find string, replacement string) (int, error) + Create(find string, replace string) (int, error) Delete(id int) error Update(id int, find string, replacement string) error Length() int