Skip to content

Commit

Permalink
performance fixes when importing large libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
cadon committed Sep 16, 2023
1 parent 6c6b6bc commit 3542a15
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 33 deletions.
12 changes: 2 additions & 10 deletions ARKBreedingStats/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,18 +1013,10 @@ void AddIfNotContains(List<string> list, string name)
serverList.Sort();

// owners
foreach (var owner in ownerList)
{
if (!string.IsNullOrEmpty(owner) && !tribesControl1.PlayerExists(owner))
tribesControl1.AddPlayer(owner);
}
tribesControl1.AddPlayers(ownerList);

// tribes
foreach (var tribe in tribesList)
{
if (!string.IsNullOrEmpty(tribe) && !tribesControl1.TribeExists(tribe))
tribesControl1.AddTribe(tribe);
}
tribesControl1.AddTribes(tribesList);

///// Apply autocomplete lists
// owners
Expand Down
15 changes: 7 additions & 8 deletions ARKBreedingStats/Form1.library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,6 @@ private void CalculateTopStats(List<Creature> creatures)
/// </summary>
private bool UpdateParents(IEnumerable<Creature> creatures)
{
List<Creature> placeholderAncestors = new List<Creature>();

Dictionary<Guid, Creature> creatureGuids;

bool duplicatesWereRemoved = false;
Expand Down Expand Up @@ -671,6 +669,8 @@ bool AreByteArraysEqual(byte[] firstArray, byte[] secondArray)
duplicatesWereRemoved = true;
}

var placeholderAncestors = new Dictionary<Guid, Creature>();

foreach (Creature c in creatures)
{
if (c.motherGuid == Guid.Empty && c.fatherGuid == Guid.Empty) continue;
Expand All @@ -689,7 +689,7 @@ bool AreByteArraysEqual(byte[] firstArray, byte[] secondArray)
c.Father = father;
}

_creatureCollection.creatures.AddRange(placeholderAncestors);
_creatureCollection.creatures.AddRange(placeholderAncestors.Values);

return duplicatesWereRemoved;
}
Expand All @@ -704,13 +704,12 @@ bool AreByteArraysEqual(byte[] firstArray, byte[] secondArray)
/// <param name="name">Name of the creature to create</param>
/// <param name="sex">Sex of the creature to create</param>
/// <returns></returns>
private Creature EnsurePlaceholderCreature(List<Creature> placeholders, Creature tmpl, Guid guid, string name, Sex sex)
private Creature EnsurePlaceholderCreature(Dictionary<Guid, Creature> placeholders, Creature tmpl, Guid guid, string name, Sex sex)
{
if (guid == Guid.Empty)
return null;
var existing = placeholders.FirstOrDefault(ph => ph.guid == guid);
if (existing != null)
return existing;
if (placeholders.TryGetValue(guid, out var existingCreature))
return existingCreature;

if (string.IsNullOrEmpty(name))
name = (sex == Sex.Female ? "Mother" : "Father") + " of " + tmpl.name;
Expand All @@ -722,7 +721,7 @@ private Creature EnsurePlaceholderCreature(List<Creature> placeholders, Creature
flags = CreatureFlags.Placeholder
};

placeholders.Add(creature);
placeholders.Add(creature.guid, creature);

return creature;
}
Expand Down
62 changes: 49 additions & 13 deletions ARKBreedingStats/TribesControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,20 @@ private void UpdatePlayerList()
{
listViewPlayer.Items.Clear();
Dictionary<string, Color> tribeRelColors = new Dictionary<string, Color>();

var tribeGroups = new Dictionary<string, ListViewGroup>();
var lviPlayers = new List<ListViewItem>();

foreach (Player p in players)
{
// check if group of tribe exists
ListViewGroup g = null;
foreach (ListViewGroup lvg in listViewPlayer.Groups)
{
if (lvg.Header == p.Tribe)
{
g = lvg;
break;
}
}
if (g == null)
var tribeName = p.Tribe ?? string.Empty;
if (!tribeGroups.TryGetValue(tribeName, out var g))
{
g = new ListViewGroup(p.Tribe);
listViewPlayer.Groups.Add(g);
tribeGroups[tribeName] = g;
}

if (p.Tribe != null && !tribeRelColors.ContainsKey(p.Tribe))
{
Color c = Color.White;
Expand Down Expand Up @@ -127,8 +124,11 @@ private void UpdatePlayerList()
};
if (!string.IsNullOrEmpty(p.Tribe))
lvi.SubItems[3].BackColor = tribeRelColors[p.Tribe];
listViewPlayer.Items.Add(lvi);
lviPlayers.Add(lvi);
}

listViewPlayer.Groups.AddRange(tribeGroups.Values.ToArray());
listViewPlayer.Items.AddRange(lviPlayers.ToArray());
}

/// <summary>
Expand All @@ -137,6 +137,7 @@ private void UpdatePlayerList()
private void UpdateTribeList()
{
listViewTribes.Items.Clear();
var tribeList = new List<ListViewItem>();
foreach (Tribe t in tribes)
{
ListViewItem lvi = new ListViewItem(new[] { t.TribeName, t.TribeRelation.ToString() })
Expand All @@ -145,8 +146,9 @@ private void UpdateTribeList()
Tag = t
};
lvi.SubItems[1].BackColor = RelationColor(t.TribeRelation);
listViewTribes.Items.Add(lvi);
tribeList.Add(lvi);
}
listViewTribes.Items.AddRange(tribeList.ToArray());
UpdateTribeSuggestions();
}

Expand Down Expand Up @@ -269,6 +271,23 @@ public void AddPlayer(string name = null)
textBoxPlayerName.Focus();
}

/// <summary>
/// Add players if they aren't yet in the list.
/// </summary>
/// <param name="playerNames"></param>
public void AddPlayers(List<string> playerNames)
{
if (playerNames == null) return;

var existingPlayers = players.Select(p => p.PlayerName).ToHashSet();
var newPlayers = playerNames
.Where(newPlayer => !string.IsNullOrEmpty(newPlayer) && !existingPlayers.Contains(newPlayer))
.Select(p => new Player { PlayerName = p }).ToArray();
if (!newPlayers.Any()) return;
players.AddRange(newPlayers);
UpdatePlayerList();
}

/// <summary>
/// Add tribe to tribe list.
/// </summary>
Expand All @@ -288,6 +307,23 @@ public void AddTribe(string name = null)
textBoxTribeName.Focus();
}

/// <summary>
/// Add tribes if they aren't yet in the list.
/// </summary>
/// <param name="playerNames"></param>
public void AddTribes(List<string> tribeNames)
{
if (tribeNames == null) return;

var existingTribes = tribes.Select(t => t.TribeName).ToHashSet();
var newTribes = tribeNames
.Where(newTribe => !string.IsNullOrEmpty(newTribe) && !existingTribes.Contains(newTribe))
.Select(t => new Tribe { TribeName = t }).ToArray();
if (!newTribes.Any()) return;
tribes.AddRange(newTribes);
UpdateTribeList();
}

private void DeleteSelectedPlayer()
{
if (listViewPlayer.SelectedIndices.Count > 0 && (MessageBox.Show("Delete selected Players?", "Delete?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
Expand Down
5 changes: 3 additions & 2 deletions ARKBreedingStats/library/CreatureCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ public bool MergeCreatureList(IEnumerable<Creature> creaturesToMerge, bool addPr
Species onlyThisSpeciesAdded = null;
bool onlyOneSpeciesAdded = true;

var guidDict = creatures.ToDictionary(c => c.guid);

foreach (Creature creatureNew in creaturesToMerge)
{
if (!addPreviouslyDeletedCreatures && DeletedCreatureGuids != null && DeletedCreatureGuids.Contains(creatureNew.guid)) continue;
Expand All @@ -213,8 +215,7 @@ public bool MergeCreatureList(IEnumerable<Creature> creaturesToMerge, bool addPr
onlyOneSpeciesAdded = false;
}

var creatureExisting = creatures.FirstOrDefault(c => c.guid == creatureNew.guid);
if (creatureExisting == null)
if (!guidDict.TryGetValue(creatureNew.guid, out var creatureExisting))
{
creatures.Add(creatureNew);
creaturesWereAddedOrUpdated = true;
Expand Down

0 comments on commit 3542a15

Please sign in to comment.