The optimization is required for scenarios where the Session.PreFetch method is executed to prefetch collection properties participating in associations and objects loaded by collections support deferred deletion.
This requirement was found when researching the scenario described by our customer in this ticket: How to optimize XPO performance. The provided example illustrates the problem:
C#internal class A : XPObject
{
private B _b;
public A(Session session) : base(session)
{
}
[Association("a-b"), NoForeignKey]
public B B
{
get { return _b; }
set { SetPropertyValue("B", ref _b, value); }
}
}
internal class B : XPObject
{
public B(Session session) : base(session)
{
}
[Association("a-b"), Delayed, NoForeignKey]
public XPCollection<A> As { get { return GetCollection<A>("As"); } }
}
static void Main(string[] args)
{
XpoDefault.DataLayer =
XpoDefault.GetDataLayer(
@"Data Source=.;Initial Catalog=perf;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False",
AutoCreateOption.DatabaseAndSchema);
using (var uow = new UnitOfWork())
{
for (int i = 0; i < 100000; i++)
{
var b = new B(uow);
b.Oid = i;
b.Save();
if (i%10000 == 0)
{
Console.WriteLine(i);
}
}
uow.CommitChanges();
}
using (var uow = new UnitOfWork())
{
var coll = new XPQuery<B>(uow).ToList();
uow.PreFetch(coll, "As");
//start profiling
foreach (var item in coll)
{
//FilterList takes 92% of time
foreach (var a in item.As)
Console.WriteLine(a.Oid);
}
//end profiling
}
}