threads: debounce client-side thread building

Debounce client-side thread building in the message store. Debouncing is
useful when multiple messages are loaded, i.e. when scrolling with
PgUp/PgDown.

Without the debouncing, all client-side threads will be built everytime
the message store is updated which creates a noticable lag in the
message list ui when client-side threading is activated.

The default debouncing delay can be changed by changing
'client-threads-delay' in the UI config section.

Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
Koni Marti 2022-07-26 15:41:13 +02:00 committed by Robin Jarry
parent a1a549cb1e
commit 54a0a377e0
6 changed files with 36 additions and 8 deletions

View file

@ -179,6 +179,11 @@ completion-popovers=true
# Default: false # Default: false
#force-client-threads=false #force-client-threads=false
# Debounce client-side thread building
#
# Default: 50ms
#client-threads-delay=50ms
[statusline] [statusline]
# Describes the format string for the statusline. # Describes the format string for the statusline.
# #

View file

@ -48,6 +48,7 @@ type UIConfig struct {
MouseEnabled bool `ini:"mouse-enabled"` MouseEnabled bool `ini:"mouse-enabled"`
ThreadingEnabled bool `ini:"threading-enabled"` ThreadingEnabled bool `ini:"threading-enabled"`
ForceClientThreads bool `ini:"force-client-threads"` ForceClientThreads bool `ini:"force-client-threads"`
ClientThreadsDelay time.Duration `ini:"client-threads-delay"`
FuzzyComplete bool `ini:"fuzzy-complete"` FuzzyComplete bool `ini:"fuzzy-complete"`
NewMessageBell bool `ini:"new-message-bell"` NewMessageBell bool `ini:"new-message-bell"`
Spinner string `ini:"spinner"` Spinner string `ini:"spinner"`
@ -712,6 +713,7 @@ func LoadConfigFromFile(root *string) (*AercConfig, error) {
EmptyMessage: "(no messages)", EmptyMessage: "(no messages)",
EmptyDirlist: "(no folders)", EmptyDirlist: "(no folders)",
MouseEnabled: false, MouseEnabled: false,
ClientThreadsDelay: 50 * time.Millisecond,
NewMessageBell: true, NewMessageBell: true,
FuzzyComplete: false, FuzzyComplete: false,
Spinner: "[..] , [..] , [..] , [..] , [..], [..] , [..] , [..] ", Spinner: "[..] , [..] , [..] , [..] , [..], [..] , [..] , [..] ",

View file

@ -5,6 +5,7 @@ import (
"time" "time"
"git.sr.ht/~rjarry/aerc/lib/sort" "git.sr.ht/~rjarry/aerc/lib/sort"
"git.sr.ht/~rjarry/aerc/logging"
"git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/types" "git.sr.ht/~rjarry/aerc/worker/types"
) )
@ -53,8 +54,8 @@ type MessageStore struct {
triggerNewEmail func(*models.MessageInfo) triggerNewEmail func(*models.MessageInfo)
triggerDirectoryChange func() triggerDirectoryChange func()
dirInfoUpdateDebounce *time.Timer threadBuilderDebounce *time.Timer
dirInfoUpdateDelay time.Duration threadBuilderDelay time.Duration
} }
const MagicUid = 0xFFFFFFFF const MagicUid = 0xFFFFFFFF
@ -62,12 +63,10 @@ const MagicUid = 0xFFFFFFFF
func NewMessageStore(worker *types.Worker, func NewMessageStore(worker *types.Worker,
dirInfo *models.DirectoryInfo, dirInfo *models.DirectoryInfo,
defaultSortCriteria []*types.SortCriterion, defaultSortCriteria []*types.SortCriterion,
thread bool, clientThreads bool, thread bool, clientThreads bool, clientThreadsDelay time.Duration,
triggerNewEmail func(*models.MessageInfo), triggerNewEmail func(*models.MessageInfo),
triggerDirectoryChange func()) *MessageStore { triggerDirectoryChange func()) *MessageStore {
dirInfoUpdateDelay := 5 * time.Second
if !dirInfo.Caps.Thread { if !dirInfo.Caps.Thread {
clientThreads = true clientThreads = true
} }
@ -95,8 +94,7 @@ func NewMessageStore(worker *types.Worker,
triggerNewEmail: triggerNewEmail, triggerNewEmail: triggerNewEmail,
triggerDirectoryChange: triggerDirectoryChange, triggerDirectoryChange: triggerDirectoryChange,
dirInfoUpdateDelay: dirInfoUpdateDelay, threadBuilderDelay: clientThreadsDelay,
dirInfoUpdateDebounce: time.NewTimer(dirInfoUpdateDelay),
} }
} }
@ -386,7 +384,17 @@ func (store *MessageStore) runThreadBuilder() {
store.builder.Update(msg) store.builder.Update(msg)
} }
} }
if store.threadBuilderDebounce != nil {
if store.threadBuilderDebounce.Stop() {
logging.Infof("thread builder debounced")
}
}
store.threadBuilderDebounce = time.AfterFunc(store.threadBuilderDelay, func() {
store.Threads = store.builder.Threads(store.uids) store.Threads = store.builder.Threads(store.uids)
if store.onUpdate != nil {
store.onUpdate(store)
}
})
} }
func (store *MessageStore) Delete(uids []uint32, func (store *MessageStore) Delete(uids []uint32,

View file

@ -1,6 +1,7 @@
package lib package lib
import ( import (
"sync"
"time" "time"
"git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/logging"
@ -10,6 +11,7 @@ import (
) )
type ThreadBuilder struct { type ThreadBuilder struct {
sync.Mutex
threadBlocks map[uint32]jwz.Threadable threadBlocks map[uint32]jwz.Threadable
messageidToUid map[string]uint32 messageidToUid map[string]uint32
seen map[uint32]bool seen map[uint32]bool
@ -27,6 +29,9 @@ func NewThreadBuilder() *ThreadBuilder {
// Uids returns the uids in threading order // Uids returns the uids in threading order
func (builder *ThreadBuilder) Uids() []uint32 { func (builder *ThreadBuilder) Uids() []uint32 {
builder.Lock()
defer builder.Unlock()
if builder.threadedUids == nil { if builder.threadedUids == nil {
return []uint32{} return []uint32{}
} }
@ -35,6 +40,9 @@ func (builder *ThreadBuilder) Uids() []uint32 {
// Update updates the thread builder with a new message header // Update updates the thread builder with a new message header
func (builder *ThreadBuilder) Update(msg *models.MessageInfo) { func (builder *ThreadBuilder) Update(msg *models.MessageInfo) {
builder.Lock()
defer builder.Unlock()
if msg != nil { if msg != nil {
if threadable := newThreadable(msg); threadable != nil { if threadable := newThreadable(msg); threadable != nil {
builder.messageidToUid[threadable.MessageThreadID()] = msg.Uid builder.messageidToUid[threadable.MessageThreadID()] = msg.Uid
@ -45,6 +53,9 @@ func (builder *ThreadBuilder) Update(msg *models.MessageInfo) {
// Threads returns a slice of threads for the given list of uids // Threads returns a slice of threads for the given list of uids
func (builder *ThreadBuilder) Threads(uids []uint32) []*types.Thread { func (builder *ThreadBuilder) Threads(uids []uint32) []*types.Thread {
builder.Lock()
defer builder.Unlock()
start := time.Now() start := time.Now()
threads := builder.buildAercThreads(builder.generateStructure(uids), uids) threads := builder.buildAercThreads(builder.generateStructure(uids), uids)

View file

@ -288,6 +288,7 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) {
acct.GetSortCriteria(), acct.GetSortCriteria(),
acct.UiConfig().ThreadingEnabled, acct.UiConfig().ThreadingEnabled,
acct.UiConfig().ForceClientThreads, acct.UiConfig().ForceClientThreads,
acct.UiConfig().ClientThreadsDelay,
func(msg *models.MessageInfo) { func(msg *models.MessageInfo) {
acct.conf.Triggers.ExecNewEmail(acct.acct, acct.conf.Triggers.ExecNewEmail(acct.acct,
acct.conf, msg) acct.conf, msg)

View file

@ -344,6 +344,7 @@ func (ml *MessageList) storeUpdate(store *lib.MessageStore) {
if ml.Store() != store { if ml.Store() != store {
return return
} }
ml.Invalidate()
} }
func (ml *MessageList) SetStore(store *lib.MessageStore) { func (ml *MessageList) SetStore(store *lib.MessageStore) {