Skip to content

Commit 12d71b1

Browse files
committed
Sort performance improvements
1 parent 5c98d7e commit 12d71b1

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

fNbt/Tags/NbtCompound.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,16 @@ public void Sort(IComparer<NbtTag> sorter, bool recursive)
203203
{
204204
var restore_sort = (NbtCompound)this.Clone();
205205
PerformAction(new DescriptionHolder("Sort {0}", this),
206-
() => { DoSort(sorter, true); },
207-
() => { DoUnsortRecursive(restore_sort); }
206+
() => DoSort(sorter, true),
207+
() => DoUnsortRecursive(restore_sort)
208208
);
209209
}
210210
else
211211
{
212212
var restore_sort = this.Tags.ToList();
213213
PerformAction(new DescriptionHolder("Sort {0}", this),
214-
() => { DoSort(sorter, false); },
215-
() => { DoUnsortRoot(restore_sort); }
214+
() => DoSort(sorter, false),
215+
() => DoUnsortRoot(restore_sort)
216216
);
217217
}
218218
}
@@ -261,7 +261,6 @@ private void DoSort(IComparer<NbtTag> sorter, bool recursive)
261261
{
262262
foreach (var tag in tags)
263263
{
264-
tag.RaiseChangedLoop();
265264
if (tag is NbtCompound sub)
266265
sub.DoSort(sorter, true);
267266
else if (tag is NbtList list)
@@ -270,6 +269,7 @@ private void DoSort(IComparer<NbtTag> sorter, bool recursive)
270269
}
271270
DoClear();
272271
DoAddRange(tags);
272+
RaiseChanged(this);
273273
}
274274

275275
private static void SortListChildren(NbtList list, IComparer<NbtTag> sorter)

fNbt/Tags/NbtContainerTag.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ public abstract class NbtContainerTag : NbtTag, IList<NbtTag>
2727
protected abstract void DoAddRange(IEnumerable<NbtTag> items);
2828
protected abstract void DoClear();
2929

30+
public IEnumerable<NbtTag> GetAllTags()
31+
{
32+
foreach (var tag in this)
33+
{
34+
yield return tag;
35+
if (tag is NbtContainerTag container)
36+
{
37+
foreach (var sub in container.GetAllTags())
38+
{
39+
yield return sub;
40+
}
41+
}
42+
}
43+
}
44+
3045
public void Add(NbtTag item)
3146
{
3247
PerformAction(new DescriptionHolder("Add {0} to {1}", item, this),

fNbt/Tags/NbtTag.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public abstract class NbtTag : ICloneable {
1515
public abstract NbtTagType TagType { get; }
1616

1717
/// <summary> Event raised when this tag or one of its children is changed. </summary>
18-
public event Action<NbtTag> Changed;
18+
public event Action<NbtTag> OnChanged;
1919

2020
/// <summary> Event raised when this tag has an undoable action performed on it. </summary>
2121
public event Action<UndoableAction> ActionPerformed;
2222

23-
private void RaiseChanged(NbtTag tag) => Changed?.Invoke(tag);
23+
protected void RaiseChanged(NbtTag tag) => OnChanged?.Invoke(tag);
2424
private void RaiseActionPerformed(UndoableAction action) => ActionPerformed?.Invoke(action);
2525

2626
/// <summary> Helper method for signaling changes to parent tags. </summary>
@@ -49,7 +49,7 @@ protected void PerformAction(DescriptionHolder description, Action action, Actio
4949
RaiseActionPerformed(undoable);
5050
}
5151

52-
internal void RaiseChangedLoop()
52+
protected void RaiseChangedLoop()
5353
{
5454
var tag = this;
5555
while (tag != null)

0 commit comments

Comments
 (0)