From 9da4d0379b10da8b33563dcd280aa2a9586aa3fb Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 5 Mar 2026 19:03:28 +0100 Subject: perf(state): use avl for copaings --- user/level.go | 2 +- user/state.go | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) (limited to 'user') diff --git a/user/level.go b/user/level.go index 609130f..98b9329 100644 --- a/user/level.go +++ b/user/level.go @@ -59,7 +59,7 @@ func PeriodicReducer(ctx context.Context, dg bot.Session) { n := 0 var wg sync.WaitGroup - for _, g := range dg.GuildAPI().State.Guilds() { + for _, g := range dg.GuildState().ListGuilds() { cfg := config.GetGuildConfig(ctx, g) res := common.GetDB(ctx). Model(&CopaingXP{}). diff --git a/user/state.go b/user/state.go index a9a97f2..d1f84b5 100644 --- a/user/state.go +++ b/user/state.go @@ -8,6 +8,7 @@ import ( "time" "git.anhgelus.world/anhgelus/les-copaings-bot/common" + "github.com/nyttikord/avl" "github.com/nyttikord/gokord/state" "gorm.io/gorm" ) @@ -101,7 +102,7 @@ func saveStateInDB(ctx context.Context) error { state.saveInDB.Lock() defer state.saveInDB.Unlock() - for _, v := range state.storage { + for _, v := range state.storage.All() { if v.mustSave() { err := v.SaveInDB(ctx) if err != nil { @@ -125,23 +126,23 @@ func FromCopaing(c *Copaing) *CopaingCached { const KeyCopaingCachedPrefix = "cc:" -func KeyCopaingCached(c *Copaing) state.Key { +func KeyCopaingCached(c *Copaing) string { return KeyCopaingCachedRaw(c.GuildID, c.DiscordID) } -func KeyCopaingCachedRaw(guildID, copaingID string) state.Key { - return KeyCopaingCachedPrefix + state.Key(guildID+":"+copaingID) +func KeyCopaingCachedRaw(guildID, copaingID string) string { + return guildID + ":" + copaingID } type State struct { mu sync.RWMutex saveInDB sync.Mutex - storage state.MapStorage[CopaingCached] + storage *state.AVLStorage[string, CopaingCached] } func NewState(db *gorm.DB) *State { state := &State{ - storage: state.MapStorage[CopaingCached]{}, + storage: state.WrapAVLAsStorage(avl.NewKeyString[CopaingCached]()), } var cs []*Copaing err := db.Find(&cs).Error @@ -191,7 +192,7 @@ func (s *State) Copaings(guild string) []CopaingCached { defer s.mu.RUnlock() var ccs []CopaingCached - for _, cc := range s.storage { + for _, cc := range s.storage.All() { if cc.GuildID == guild { ccs = append(ccs, deepCopy(cc)) } -- cgit v1.2.3