Skip to content

Commit

Permalink
Merge pull request #12146 from MrJul/fixes/composition-resources-inva…
Browse files Browse the repository at this point in the history
…lidation

Fix composition render resources invalidation
  • Loading branch information
kekekeks authored Jul 13, 2023
2 parents 868cb1e + e0f9052 commit 124f124
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,31 @@ protected override void DeserializeChangesCore(BatchStreamReader reader, TimeSpa
_items.Add(reader.ReadObject<IRenderDataItem>());

var collector = s_resourceHashSetPool.Get();
foreach(var item in _items)
if (item is IRenderDataItemWithServerResources resourceItem)
resourceItem.Collect(collector);

CollectResources(_items, collector);

foreach (var r in collector.Resources)
{
_referencedResources.Add(r);
r.AddObserver(this);
}

collector.Resources.Clear();
s_resourceHashSetPool.ReturnAndSetNull(ref collector);

base.DeserializeChangesCore(reader, committedAt);
}

private static void CollectResources(PooledInlineList<IRenderDataItem> items, IRenderDataServerResourcesCollector collector)
{
foreach (var item in items)
{
if (item is IRenderDataItemWithServerResources resourceItem)
resourceItem.Collect(collector);
else if (item is RenderDataPushNode pushNode)
CollectResources(pushNode.Children, collector);
}
}

public Rect? Bounds
{
get
Expand Down Expand Up @@ -133,4 +142,4 @@ public override void Dispose()
Reset();
base.Dispose();
}
}
}
16 changes: 10 additions & 6 deletions src/Avalonia.Base/Utilities/SmallDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void SetCore(TKey key, TValue value, bool overwrite)
throw new ArgumentException("Key already exists in dictionary");
}

if (arr[c].Key == null)
if (arr[c].Key == null && free == -1)
free = c;
}

Expand Down Expand Up @@ -337,11 +337,15 @@ public bool MoveNext()
}
else if (_type == Type.Array)
{
var next = _index + 1;
if (_arr!.Length - 1 < next || _arr[next].Key == null)
return false;
_index = next;
return true;
for (var next = _index + 1; next < _arr!.Length; ++next)
{
if (_arr[next].Key != null)
{
_index = next;
return true;
}
}
return false;
}
else if (_type == Type.Dictionary)
return _inner.MoveNext();
Expand Down
53 changes: 53 additions & 0 deletions tests/Avalonia.Base.UnitTests/Utilities/InlineDictionaryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#nullable enable

using System.Collections.Generic;
using Avalonia.Utilities;
using Xunit;

namespace Avalonia.Base.UnitTests.Utilities;

public class InlineDictionaryTests
{
[Fact]
public void Enumeration_After_Add_With_Internal_Array_Works()
{
var dic = new InlineDictionary<string, int>();
dic.Add("foo", 1);
dic.Add("bar", 2);
dic.Add("baz", 3);

Assert.Equal(
new[] {
new KeyValuePair<string, int>("foo", 1),
new KeyValuePair<string, int>("bar", 2),
new KeyValuePair<string, int>("baz", 3)
},
dic);
}

[Fact]
public void Enumeration_After_Remove_With_Internal_Array_Works()
{
var dic = new InlineDictionary<string, int>();
dic.Add("foo", 1);
dic.Add("bar", 2);
dic.Add("baz", 3);

Assert.Equal(
new[] {
new KeyValuePair<string, int>("foo", 1),
new KeyValuePair<string, int>("bar", 2),
new KeyValuePair<string, int>("baz", 3)
},
dic);

dic.Remove("bar");

Assert.Equal(
new[] {
new KeyValuePair<string, int>("foo", 1),
new KeyValuePair<string, int>("baz", 3)
},
dic);
}
}

0 comments on commit 124f124

Please sign in to comment.