Accelerate the convo list.

Shorten some things.
This commit is contained in:
Azareal 2019-09-01 08:07:34 +10:00
parent c309faf79f
commit 523837910c
8 changed files with 144 additions and 149 deletions

View File

@ -38,12 +38,7 @@ func (s *DefaultLikeStore) BulkExists(ids []int, sentBy int, targetType string)
}
eids = append(eids,id)
}
err = rows.Err()
if err != nil {
return nil, err
}
return eids, nil
return eids, rows.Err()
}
// TODO: Write a test for this

View File

@ -39,10 +39,10 @@ func NewMemoryPollCache(capacity int) *MemoryPollCache {
}
// Get fetches a poll by ID. Returns ErrNoRows if not present.
func (mus *MemoryPollCache) Get(id int) (*Poll, error) {
mus.RLock()
item, ok := mus.items[id]
mus.RUnlock()
func (s *MemoryPollCache) Get(id int) (*Poll, error) {
s.RLock()
item, ok := s.items[id]
s.RUnlock()
if ok {
return item, nil
}
@ -50,19 +50,19 @@ func (mus *MemoryPollCache) Get(id int) (*Poll, error) {
}
// BulkGet fetches multiple polls by their IDs. Indices without polls will be set to nil, so make sure you check for those, we might want to change this behaviour to make it less confusing.
func (mus *MemoryPollCache) BulkGet(ids []int) (list []*Poll) {
func (s *MemoryPollCache) BulkGet(ids []int) (list []*Poll) {
list = make([]*Poll, len(ids))
mus.RLock()
s.RLock()
for i, id := range ids {
list[i] = mus.items[id]
list[i] = s.items[id]
}
mus.RUnlock()
s.RUnlock()
return list
}
// GetUnsafe fetches a poll by ID. Returns ErrNoRows if not present. THIS METHOD IS NOT THREAD-SAFE.
func (mus *MemoryPollCache) GetUnsafe(id int) (*Poll, error) {
item, ok := mus.items[id]
func (s *MemoryPollCache) GetUnsafe(id int) (*Poll, error) {
item, ok := s.items[id]
if ok {
return item, nil
}
@ -70,95 +70,95 @@ func (mus *MemoryPollCache) GetUnsafe(id int) (*Poll, error) {
}
// Set overwrites the value of a poll in the cache, whether it's present or not. May return a capacity overflow error.
func (mus *MemoryPollCache) Set(item *Poll) error {
mus.Lock()
user, ok := mus.items[item.ID]
func (s *MemoryPollCache) Set(item *Poll) error {
s.Lock()
user, ok := s.items[item.ID]
if ok {
mus.Unlock()
s.Unlock()
*user = *item
} else if int(mus.length) >= mus.capacity {
mus.Unlock()
} else if int(s.length) >= s.capacity {
s.Unlock()
return ErrStoreCapacityOverflow
} else {
mus.items[item.ID] = item
mus.Unlock()
atomic.AddInt64(&mus.length, 1)
s.items[item.ID] = item
s.Unlock()
atomic.AddInt64(&s.length, 1)
}
return nil
}
// Add adds a poll to the cache, similar to Set, but it's only intended for new items. This method might be deprecated in the near future, use Set. May return a capacity overflow error.
// ? Is this redundant if we have Set? Are the efficiency wins worth this? Is this even used?
func (mus *MemoryPollCache) Add(item *Poll) error {
mus.Lock()
if int(mus.length) >= mus.capacity {
mus.Unlock()
func (s *MemoryPollCache) Add(item *Poll) error {
s.Lock()
if int(s.length) >= s.capacity {
s.Unlock()
return ErrStoreCapacityOverflow
}
mus.items[item.ID] = item
mus.length = int64(len(mus.items))
mus.Unlock()
s.items[item.ID] = item
s.length = int64(len(s.items))
s.Unlock()
return nil
}
// AddUnsafe is the unsafe version of Add. May return a capacity overflow error. THIS METHOD IS NOT THREAD-SAFE.
func (mus *MemoryPollCache) AddUnsafe(item *Poll) error {
if int(mus.length) >= mus.capacity {
func (s *MemoryPollCache) AddUnsafe(item *Poll) error {
if int(s.length) >= s.capacity {
return ErrStoreCapacityOverflow
}
mus.items[item.ID] = item
mus.length = int64(len(mus.items))
s.items[item.ID] = item
s.length = int64(len(s.items))
return nil
}
// Remove removes a poll from the cache by ID, if they exist. Returns ErrNoRows if no items exist.
func (mus *MemoryPollCache) Remove(id int) error {
mus.Lock()
_, ok := mus.items[id]
func (s *MemoryPollCache) Remove(id int) error {
s.Lock()
_, ok := s.items[id]
if !ok {
mus.Unlock()
s.Unlock()
return ErrNoRows
}
delete(mus.items, id)
mus.Unlock()
atomic.AddInt64(&mus.length, -1)
delete(s.items, id)
s.Unlock()
atomic.AddInt64(&s.length, -1)
return nil
}
// RemoveUnsafe is the unsafe version of Remove. THIS METHOD IS NOT THREAD-SAFE.
func (mus *MemoryPollCache) RemoveUnsafe(id int) error {
_, ok := mus.items[id]
func (s *MemoryPollCache) RemoveUnsafe(id int) error {
_, ok := s.items[id]
if !ok {
return ErrNoRows
}
delete(mus.items, id)
atomic.AddInt64(&mus.length, -1)
delete(s.items, id)
atomic.AddInt64(&s.length, -1)
return nil
}
// Flush removes all the polls from the cache, useful for tests.
func (mus *MemoryPollCache) Flush() {
mus.Lock()
mus.items = make(map[int]*Poll)
mus.length = 0
mus.Unlock()
func (s *MemoryPollCache) Flush() {
s.Lock()
s.items = make(map[int]*Poll)
s.length = 0
s.Unlock()
}
// ! Is this concurrent?
// Length returns the number of polls in the memory cache
func (mus *MemoryPollCache) Length() int {
return int(mus.length)
func (s *MemoryPollCache) Length() int {
return int(s.length)
}
// SetCapacity sets the maximum number of polls which this cache can hold
func (mus *MemoryPollCache) SetCapacity(capacity int) {
func (s *MemoryPollCache) SetCapacity(capacity int) {
// Ints are moved in a single instruction, so this should be thread-safe
mus.capacity = capacity
s.capacity = capacity
}
// GetCapacity returns the maximum number of polls this cache can hold
func (mus *MemoryPollCache) GetCapacity() int {
return mus.capacity
func (s *MemoryPollCache) GetCapacity() int {
return s.capacity
}
// NullPollCache is a poll cache to be used when you don't want a cache and just want queries to passthrough to the database
@ -171,37 +171,37 @@ func NewNullPollCache() *NullPollCache {
}
// nolint
func (mus *NullPollCache) Get(id int) (*Poll, error) {
func (s *NullPollCache) Get(id int) (*Poll, error) {
return nil, ErrNoRows
}
func (mus *NullPollCache) BulkGet(ids []int) (list []*Poll) {
func (s *NullPollCache) BulkGet(ids []int) (list []*Poll) {
return make([]*Poll, len(ids))
}
func (mus *NullPollCache) GetUnsafe(id int) (*Poll, error) {
func (s *NullPollCache) GetUnsafe(id int) (*Poll, error) {
return nil, ErrNoRows
}
func (mus *NullPollCache) Set(_ *Poll) error {
func (s *NullPollCache) Set(_ *Poll) error {
return nil
}
func (mus *NullPollCache) Add(_ *Poll) error {
func (s *NullPollCache) Add(_ *Poll) error {
return nil
}
func (mus *NullPollCache) AddUnsafe(_ *Poll) error {
func (s *NullPollCache) AddUnsafe(_ *Poll) error {
return nil
}
func (mus *NullPollCache) Remove(id int) error {
func (s *NullPollCache) Remove(id int) error {
return nil
}
func (mus *NullPollCache) RemoveUnsafe(id int) error {
func (s *NullPollCache) RemoveUnsafe(id int) error {
return nil
}
func (mus *NullPollCache) Flush() {
func (s *NullPollCache) Flush() {
}
func (mus *NullPollCache) Length() int {
func (s *NullPollCache) Length() int {
return 0
}
func (mus *NullPollCache) SetCapacity(_ int) {
func (s *NullPollCache) SetCapacity(_ int) {
}
func (mus *NullPollCache) GetCapacity() int {
func (s *NullPollCache) GetCapacity() int {
return 0
}

View File

@ -40,10 +40,10 @@ func NewMemoryReplyCache(capacity int) *MemoryReplyCache {
}
// Get fetches a reply by ID. Returns ErrNoRows if not present.
func (mts *MemoryReplyCache) Get(id int) (*Reply, error) {
mts.RLock()
item, ok := mts.items[id]
mts.RUnlock()
func (s *MemoryReplyCache) Get(id int) (*Reply, error) {
s.RLock()
item, ok := s.items[id]
s.RUnlock()
if ok {
return item, nil
}
@ -51,8 +51,8 @@ func (mts *MemoryReplyCache) Get(id int) (*Reply, error) {
}
// GetUnsafe fetches a reply by ID. Returns ErrNoRows if not present. THIS METHOD IS NOT THREAD-SAFE.
func (mts *MemoryReplyCache) GetUnsafe(id int) (*Reply, error) {
item, ok := mts.items[id]
func (s *MemoryReplyCache) GetUnsafe(id int) (*Reply, error) {
item, ok := s.items[id]
if ok {
return item, nil
}
@ -60,69 +60,69 @@ func (mts *MemoryReplyCache) GetUnsafe(id int) (*Reply, error) {
}
// BulkGet fetches multiple replies by their IDs. Indices without replies will be set to nil, so make sure you check for those, we might want to change this behaviour to make it less confusing.
func (c *MemoryReplyCache) BulkGet(ids []int) (list []*Reply) {
func (s *MemoryReplyCache) BulkGet(ids []int) (list []*Reply) {
list = make([]*Reply, len(ids))
c.RLock()
s.RLock()
for i, id := range ids {
list[i] = c.items[id]
list[i] = s.items[id]
}
c.RUnlock()
s.RUnlock()
return list
}
// Set overwrites the value of a reply in the cache, whether it's present or not. May return a capacity overflow error.
func (mts *MemoryReplyCache) Set(item *Reply) error {
mts.Lock()
_, ok := mts.items[item.ID]
func (s *MemoryReplyCache) Set(item *Reply) error {
s.Lock()
_, ok := s.items[item.ID]
if ok {
mts.items[item.ID] = item
} else if int(mts.length) >= mts.capacity {
mts.Unlock()
s.items[item.ID] = item
} else if int(s.length) >= s.capacity {
s.Unlock()
return ErrStoreCapacityOverflow
} else {
mts.items[item.ID] = item
atomic.AddInt64(&mts.length, 1)
s.items[item.ID] = item
atomic.AddInt64(&s.length, 1)
}
mts.Unlock()
s.Unlock()
return nil
}
// Add adds a reply to the cache, similar to Set, but it's only intended for new items. This method might be deprecated in the near future, use Set. May return a capacity overflow error.
// ? Is this redundant if we have Set? Are the efficiency wins worth this? Is this even used?
func (mts *MemoryReplyCache) Add(item *Reply) error {
func (s *MemoryReplyCache) Add(item *Reply) error {
log.Print("MemoryReplyCache.Add")
mts.Lock()
if int(mts.length) >= mts.capacity {
mts.Unlock()
s.Lock()
if int(s.length) >= s.capacity {
s.Unlock()
return ErrStoreCapacityOverflow
}
mts.items[item.ID] = item
mts.Unlock()
atomic.AddInt64(&mts.length, 1)
s.items[item.ID] = item
s.Unlock()
atomic.AddInt64(&s.length, 1)
return nil
}
// AddUnsafe is the unsafe version of Add. May return a capacity overflow error. THIS METHOD IS NOT THREAD-SAFE.
func (mts *MemoryReplyCache) AddUnsafe(item *Reply) error {
if int(mts.length) >= mts.capacity {
func (s *MemoryReplyCache) AddUnsafe(item *Reply) error {
if int(s.length) >= s.capacity {
return ErrStoreCapacityOverflow
}
mts.items[item.ID] = item
mts.length = int64(len(mts.items))
s.items[item.ID] = item
s.length = int64(len(s.items))
return nil
}
// Remove removes a reply from the cache by ID, if they exist. Returns ErrNoRows if no items exist.
func (mts *MemoryReplyCache) Remove(id int) error {
mts.Lock()
_, ok := mts.items[id]
func (s *MemoryReplyCache) Remove(id int) error {
s.Lock()
_, ok := s.items[id]
if !ok {
mts.Unlock()
s.Unlock()
return ErrNoRows
}
delete(mts.items, id)
mts.Unlock()
atomic.AddInt64(&mts.length, -1)
delete(s.items, id)
s.Unlock()
atomic.AddInt64(&s.length, -1)
return nil
}

View File

@ -39,10 +39,10 @@ func NewMemoryTopicCache(capacity int) *MemoryTopicCache {
}
// Get fetches a topic by ID. Returns ErrNoRows if not present.
func (mts *MemoryTopicCache) Get(id int) (*Topic, error) {
mts.RLock()
item, ok := mts.items[id]
mts.RUnlock()
func (s *MemoryTopicCache) Get(id int) (*Topic, error) {
s.RLock()
item, ok := s.items[id]
s.RUnlock()
if ok {
return item, nil
}
@ -50,8 +50,8 @@ func (mts *MemoryTopicCache) Get(id int) (*Topic, error) {
}
// GetUnsafe fetches a topic by ID. Returns ErrNoRows if not present. THIS METHOD IS NOT THREAD-SAFE.
func (mts *MemoryTopicCache) GetUnsafe(id int) (*Topic, error) {
item, ok := mts.items[id]
func (s *MemoryTopicCache) GetUnsafe(id int) (*Topic, error) {
item, ok := s.items[id]
if ok {
return item, nil
}
@ -59,68 +59,68 @@ func (mts *MemoryTopicCache) GetUnsafe(id int) (*Topic, error) {
}
// BulkGet fetches multiple topics by their IDs. Indices without topics will be set to nil, so make sure you check for those, we might want to change this behaviour to make it less confusing.
func (c *MemoryTopicCache) BulkGet(ids []int) (list []*Topic) {
func (s *MemoryTopicCache) BulkGet(ids []int) (list []*Topic) {
list = make([]*Topic, len(ids))
c.RLock()
s.RLock()
for i, id := range ids {
list[i] = c.items[id]
list[i] = s.items[id]
}
c.RUnlock()
s.RUnlock()
return list
}
// Set overwrites the value of a topic in the cache, whether it's present or not. May return a capacity overflow error.
func (mts *MemoryTopicCache) Set(item *Topic) error {
mts.Lock()
_, ok := mts.items[item.ID]
func (s *MemoryTopicCache) Set(item *Topic) error {
s.Lock()
_, ok := s.items[item.ID]
if ok {
mts.items[item.ID] = item
} else if int(mts.length) >= mts.capacity {
mts.Unlock()
s.items[item.ID] = item
} else if int(s.length) >= s.capacity {
s.Unlock()
return ErrStoreCapacityOverflow
} else {
mts.items[item.ID] = item
atomic.AddInt64(&mts.length, 1)
s.items[item.ID] = item
atomic.AddInt64(&s.length, 1)
}
mts.Unlock()
s.Unlock()
return nil
}
// Add adds a topic to the cache, similar to Set, but it's only intended for new items. This method might be deprecated in the near future, use Set. May return a capacity overflow error.
// ? Is this redundant if we have Set? Are the efficiency wins worth this? Is this even used?
func (mts *MemoryTopicCache) Add(item *Topic) error {
mts.Lock()
if int(mts.length) >= mts.capacity {
mts.Unlock()
func (s *MemoryTopicCache) Add(item *Topic) error {
s.Lock()
if int(s.length) >= s.capacity {
s.Unlock()
return ErrStoreCapacityOverflow
}
mts.items[item.ID] = item
mts.Unlock()
atomic.AddInt64(&mts.length, 1)
s.items[item.ID] = item
s.Unlock()
atomic.AddInt64(&s.length, 1)
return nil
}
// AddUnsafe is the unsafe version of Add. May return a capacity overflow error. THIS METHOD IS NOT THREAD-SAFE.
func (mts *MemoryTopicCache) AddUnsafe(item *Topic) error {
if int(mts.length) >= mts.capacity {
func (s *MemoryTopicCache) AddUnsafe(item *Topic) error {
if int(s.length) >= s.capacity {
return ErrStoreCapacityOverflow
}
mts.items[item.ID] = item
mts.length = int64(len(mts.items))
s.items[item.ID] = item
s.length = int64(len(s.items))
return nil
}
// Remove removes a topic from the cache by ID, if they exist. Returns ErrNoRows if no items exist.
func (mts *MemoryTopicCache) Remove(id int) error {
mts.Lock()
_, ok := mts.items[id]
func (s *MemoryTopicCache) Remove(id int) error {
s.Lock()
_, ok := s.items[id]
if !ok {
mts.Unlock()
s.Unlock()
return ErrNoRows
}
delete(mts.items, id)
mts.Unlock()
atomic.AddInt64(&mts.length, -1)
delete(s.items, id)
s.Unlock()
atomic.AddInt64(&s.length, -1)
return nil
}

View File

@ -4,7 +4,7 @@
</div>
</div>
<div class="colstack_item the_form">
<form action="/user/convos/create/submit/?session={{.CurrentUser.Session}}" method="post">
<form action="/user/convos/create/submit/?s={{.CurrentUser.Session}}" method="post">
<div class="formrow real_first_child">
<div class="formitem formlabel"><a>Recipient/s</a></div>
<div class="formitem"><input name="recp" type="text"{{if .RecpName}} value="{{.RecpName}}"{{end}} /></div>

View File

@ -4,7 +4,7 @@
<div class="rowitem"><h1>{{lang "create_topic_head"}}</h1></div>
</div>
<div class="rowblock the_form">
<form id="quick_post_form" enctype="multipart/form-data" action="/topic/create/submit/?session={{.CurrentUser.Session}}" method="post"></form>
<form id="quick_post_form" enctype="multipart/form-data" action="/topic/create/submit/?s={{.CurrentUser.Session}}" method="post"></form>
<div class="formrow real_first_child">
<div class="formitem formlabel"><a>{{lang "create_topic_board"}}</a></div>
<div class="formitem"><select form="quick_post_form" id="topic_board_input" name="topic-board">

View File

@ -29,7 +29,7 @@
{{if .CurrentUser.Perms.CreateTopic}}
<div id="forum_topic_create_form" class="rowblock topic_create_form quick_create_form" style="display:none;" aria-label="{{lang "quick_topic.aria"}}">
<form id="quick_post_form" enctype="multipart/form-data" action="/topic/create/submit/?session={{.CurrentUser.Session}}" method="post"></form>
<form id="quick_post_form" enctype="multipart/form-data" action="/topic/create/submit/?s={{.CurrentUser.Session}}" method="post"></form>
<img class="little_row_avatar" src="{{.CurrentUser.MicroAvatar}}" height=64 alt="{{lang "quick_topic.avatar_alt"}}" title="{{lang "quick_topic.avatar_tooltip"}}" />
<input form="quick_post_form" id="topic_board_input" name="topic-board" value="{{.Forum.ID}}" type="hidden">
<div class="main_form">

View File

@ -26,8 +26,8 @@
{{template "topics_mod_floater.html"}}
{{if .CurrentUser.Perms.CreateTopic}}
<div id="forum_topic_create_form" class="rowblock topic_create_form quick_create_form" style="display: none;" aria-label="{{lang "quick_topic.aria"}}">
<form id="quick_post_form" enctype="multipart/form-data" action="/topic/create/submit/?session={{.CurrentUser.Session}}" method="post"></form>
<div id="forum_topic_create_form" class="rowblock topic_create_form quick_create_form" style="display:none;" aria-label="{{lang "quick_topic.aria"}}">
<form id="quick_post_form" enctype="multipart/form-data" action="/topic/create/submit/?s={{.CurrentUser.Session}}" method="post"></form>
<img class="little_row_avatar" src="{{.CurrentUser.MicroAvatar}}" height=64 alt="{{lang "quick_topic.avatar_alt"}}" title="{{lang "quick_topic.avatar_tooltip"}}" />
<input form="quick_post_form" id="topic_board_input" name="topic-board" value="{{.Forum.ID}}" type="hidden">
<div class="main_form">