Skip to content

Fix composition render resources invalidation #12146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}