How can I improve performance when mapping really big data structure to other classes for serialization / web api?
I have this class
interface INode
{
string Id { get; }
INode Parent { get; }
MyCollection<INode> Children { get; }
}
class MyCollection<T> : IEnumerable<T> where T : INode
{
private IDictionary<string, T> nodes;
public MyCollection(IEnumerable<T> nodeCollection)
{
nodes = nodeCollection.ToDictionary(t => t.Id);
}
public IEnumerator<T> GetEnumerator()
=> nodes.Values.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
}
So when I initialize my tree I recursively create collections under collections, and there's pretty big performance loss, but I guess there's nothing you can do, because ToDictionary() method iterates through the collection and takes its time.
But when I pass this class to web api, I need to map it first to some other class because otherwise serializer tells me that there's cyclical references (Nodes[0] -> Parent -> Nodes[0] -> Parent -> Nodes[0]). So I map my structure recursively to this class:
class DomainNode {
public string Id { get; set; }
public string ParentId { get; set; }
public IEnumerable<DomainNode> Children { get; set; }
}
by setting all the properties manually (without e.g. Automapper for now).
For big datasets, theres very big performance impact. When I load some mediocre collection, it takes full 7 seconds to get my response. It there any faster way? Maybe with hacks?
If there's special instructions how to do it with Json.Net / Web.Api (Owin) it would be great.