Micro-optimizations to Remove Additional Warnings

- Reduce LINQ overhead by replacing SingleOrDefault and filtering logic with manual iteration.
- Use HashSet and case-insensitive comparisons for faster lookups.

Both of these changes resolve a number of HAA0401 warnings:
Possible allocation of reference type enumerator; non-ValueType enumerator may result in a heap allocation.
This commit is contained in:
Refringe 2024-11-14 12:38:03 -05:00
parent 3faeb80e55
commit f965d476a1
Signed by untrusted user: Refringe
GPG Key ID: 7715B85B4A6306ED

View File

@ -42,7 +42,7 @@ public static class BotParser
foreach (var filePath in botFiles)
{
i++;
if (i % 100 == 0) Console.WriteLine($"Processing file {i.ToString()}");
if (i % 500 == 0) Console.WriteLine($"Processing file {i.ToString()}");
ProcessBotFileSync(baseBots, filePath, parsedBotIds, totalDupeCount);
}
@ -84,15 +84,14 @@ public static class BotParser
int dupeCount = 0;
List<Datum> bots = [];
List<Datum> bots = new List<Datum>();
try
{
// Parse the bots inside the json file
using (var reader = new StreamReader(filePath))
{
var deSerialisedObject = JsonSerializer.Deserialize<Root>(reader.ReadToEnd(), serialiserOptions);
foreach (var botData in deSerialisedObject.data)
foreach (var botData in deSerialisedObject.data.ToList())
{
// Bot fucks up something, never allow it in
if (botData._id == "6483938c53cc9087c70eae86")
@ -101,10 +100,22 @@ public static class BotParser
continue;
}
var baseBot = baseBots.SingleOrDefault(bot => bot.botType.ToString().Equals(botData.Info.Settings.Role, StringComparison.OrdinalIgnoreCase));
var role = botData.Info.Settings.Role;
var botType = Enum.Parse<BotType>(role, true);
Bot baseBot = null;
foreach (var bot in baseBots)
{
if (bot.botType == botType)
{
baseBot = bot;
break;
}
}
if (baseBot == null)
{
//Console.WriteLine($"Skipping {botData._id} due to unknown role {botData.Info.Settings.Role}");
Console.WriteLine($"Skipping {botData._id} due to unknown role {botData.Info.Settings.Role}");
continue;
}
@ -121,7 +132,7 @@ public static class BotParser
}
}
}
catch (Exception ex)
catch (Exception)
{
Console.WriteLine($"File parse fucked up: {filePath}");
throw;
@ -153,9 +164,9 @@ public static class BotParser
await Task.WhenAll(tasks.ToArray());
stopwatch.Stop();
return [.. parsedBotsDict.Values];
LoggingHelpers.LogToConsole($"Cleaned and Parsed: {parsedBotsDict.Count.ToString()} bots. {totalDupeCount.ToString()} dupes were ignored. Took {LoggingHelpers.LogTimeTaken(stopwatch.Elapsed.TotalSeconds)} seconds");
return parsedBotsDict.Values.ToList();
}
private static async Task<int> ProcessBotFile(
@ -168,17 +179,27 @@ public static class BotParser
int dupeCount = 0;
List<Datum> bots = [];
List<Datum> bots = new List<Datum>();
try
{
// Parse the bots inside the json file
using (var reader = new StreamReader(filePath))
using var reader = new StreamReader(filePath);
var deSerialisedObject = await JsonSerializer.DeserializeAsync<Root>(reader.BaseStream, serialiserOptions);
var botTypesLower = new HashSet<string>(botTypes, StringComparer.OrdinalIgnoreCase);
var filteredBots = new List<Datum>();
foreach (var botData in deSerialisedObject.data.ToList())
{
var deSerialisedObject = JsonSerializer.Deserialize<Root>(reader.ReadToEnd(), serialiserOptions);
bots.AddRange(deSerialisedObject.data.Where(botData => botTypes.Contains(botData.Info.Settings.Role.ToLower())));
var roleLower = botData.Info.Settings.Role.ToLower();
if (botTypesLower.Contains(roleLower))
{
filteredBots.Add(botData);
}
}
catch (Exception ex)
bots.AddRange(filteredBots);
}
catch (Exception)
{
Console.WriteLine($"File parse fucked up: {filePath}");
throw;
@ -235,8 +256,9 @@ public static class BotParser
if (jItemsToReplace != null && jItemsToReplace.Any())
{
foreach (var item in jItemsToReplace)
LoggingHelpers.LogToConsole($"file {fileName} has {jItemsToReplace.Count().ToString()} json issues, cleaning up.", ConsoleColor.Yellow);
var jItemsToReplaceList = jItemsToReplace.ToList();
foreach (var item in jItemsToReplaceList)
{
var obj = new { x = 1, y = 0, r = 0 };
item.Replace(JToken.FromObject(obj));