From 7ea45a16b4cf0b86487c6a1f29e0c897040efc02 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Fri, 30 Dec 2016 05:46:13 +0100 Subject: [PATCH] Huge startup time increase on large bots --- .../Games/Commands/CleverBotCommands.cs | 19 +++++++++---------- .../Permissions/Commands/CmdCdsCommands.cs | 7 ++----- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs b/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs index f0b99b3f..3fd20cf1 100644 --- a/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs +++ b/src/NadekoBot/Modules/Games/Commands/CleverBotCommands.cs @@ -24,9 +24,8 @@ namespace NadekoBot.Modules.Games public string Status { get; set; } public string Response { get; set; } } - //user#discrim is the key - public static ConcurrentHashSet ChannelsInConversation { get; } = new ConcurrentHashSet(); - public static ConcurrentDictionary CleverbotGuilds { get; } = new ConcurrentDictionary(); + + public static ConcurrentDictionary> CleverbotGuilds { get; } = new ConcurrentDictionary>(); static CleverBotCommands() { @@ -36,10 +35,10 @@ namespace NadekoBot.Modules.Games using (var uow = DbHandler.UnitOfWork()) { var bot = ChatterBotFactory.Create(ChatterBotType.CLEVERBOT); - CleverbotGuilds = new ConcurrentDictionary( + CleverbotGuilds = new ConcurrentDictionary>( NadekoBot.AllGuildConfigs .Where(gc => gc.CleverbotEnabled) - .ToDictionary(gc => gc.GuildId, gc => bot.CreateSession())); + .ToDictionary(gc => gc.GuildId, gc => new Lazy(() => bot.CreateSession(), true))); } sw.Stop(); @@ -52,7 +51,7 @@ namespace NadekoBot.Modules.Games if (channel == null) return false; - ChatterBotSession cleverbot; + Lazy cleverbot; if (!CleverbotGuilds.TryGetValue(channel.Guild.Id, out cleverbot)) return false; @@ -75,7 +74,7 @@ namespace NadekoBot.Modules.Games await msg.Channel.TriggerTypingAsync().ConfigureAwait(false); - var response = await cleverbot.Think(message).ConfigureAwait(false); + var response = await cleverbot.Value.Think(message).ConfigureAwait(false); try { await msg.Channel.SendConfirmAsync(response.SanitizeMentions()).ConfigureAwait(false); @@ -94,7 +93,7 @@ namespace NadekoBot.Modules.Games { var channel = (ITextChannel)imsg.Channel; - ChatterBotSession throwaway; + Lazy throwaway; if (CleverbotGuilds.TryRemove(channel.Guild.Id, out throwaway)) { using (var uow = DbHandler.UnitOfWork()) @@ -109,7 +108,7 @@ namespace NadekoBot.Modules.Games var cleverbot = ChatterBotFactory.Create(ChatterBotType.CLEVERBOT); var session = cleverbot.CreateSession(); - CleverbotGuilds.TryAdd(channel.Guild.Id, session); + CleverbotGuilds.TryAdd(channel.Guild.Id, new Lazy(() => session, true)); using (var uow = DbHandler.UnitOfWork()) { @@ -121,4 +120,4 @@ namespace NadekoBot.Modules.Games } } } -} +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Permissions/Commands/CmdCdsCommands.cs b/src/NadekoBot/Modules/Permissions/Commands/CmdCdsCommands.cs index c18bf8ed..e8d5adb6 100644 --- a/src/NadekoBot/Modules/Permissions/Commands/CmdCdsCommands.cs +++ b/src/NadekoBot/Modules/Permissions/Commands/CmdCdsCommands.cs @@ -28,11 +28,8 @@ namespace NadekoBot.Modules.Permissions static CmdCdsCommands() { - using (var uow = DbHandler.UnitOfWork()) - { - var configs = NadekoBot.AllGuildConfigs; - commandCooldowns = new ConcurrentDictionary>(configs.ToDictionary(k => k.GuildId, v => new ConcurrentHashSet(v.CommandCooldowns))); - } + var configs = NadekoBot.AllGuildConfigs; + commandCooldowns = new ConcurrentDictionary>(configs.ToDictionary(k => k.GuildId, v => new ConcurrentHashSet(v.CommandCooldowns))); } [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)]