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 --- commands/deploy.go | 2 +- config/guild.go | 2 +- user/level.go | 2 +- user/state.go | 15 ++++++++------- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/commands/deploy.go b/commands/deploy.go index e652d43..7b039f2 100644 --- a/commands/deploy.go +++ b/commands/deploy.go @@ -70,7 +70,7 @@ func init() { func Deploy(ctx context.Context, dg bot.Session) error { guildID := "" if common.IsDebug(ctx) { - guildID = dg.GuildAPI().State.Guilds()[0] + guildID = dg.GuildState().ListGuilds()[0] bot.Logger(ctx).Debug("using guild as debug", "guild", guildID) } _, err := interaction.OverwriteCommands(dg.SessionState().Application().ID, guildID, commands).Do(ctx) diff --git a/config/guild.go b/config/guild.go index 98ef0cb..6c46a1a 100644 --- a/config/guild.go +++ b/config/guild.go @@ -57,7 +57,7 @@ func (cfg *Guild) IsDisabled(ctx context.Context, dg bot.Session, channelID stri ok := true for channelID != "" && ok { ok = !strings.Contains(cfg.DisabledChannels, channelID) - c, err := dg.ChannelAPI().State.Channel(channelID) + c, err := dg.ChannelState().GetChannel(channelID) if err != nil { bot.Logger(ctx).Error("unable to find channel %s in state", "error", err, "channel", c) c, err = channel.Get(channelID).Do(ctx) 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