vijay
vijay

Reputation: 13

c# Performance Issue while creating Multiple objects

In our code, we are Mapping the domain entity into an API DTO object. In the process of converting entity is returning a huge amount of records. when we try to map those records into DTO its taking huge amount of time. Here is the example

  Class test
{
    public virtual int ID { get; protected internal set; }
    public virtual Employee Employee { get; protected internal set; }
    public virtual BusinessDate Start { get; protected internal set; }
    protected internal virtual IList<ABC> _abc { get; set; }
    public virtual IEnumerable<ABC>  ABCRange{
  get
  {
    return _abc;
  }
}

 public IEnumerable<test> GetByEmployees(IEnumerable<int> employees)
    {
      return ChunkIDsQueryForSQLServer2100Limit(employees, empArray =>
              (from ar in GetAllAsQueryable()
              where empArray.Contains(ar.Employee.ID)
              select ar).ToList());
    }
class ABC
{
    public virtual int ID { get; protected internal set; }
    public virtual int DayOfWeek { get; protected internal set; }
    public virtual bool StartTimeOffset { get; protected internal set; }
    public virtual bool EndTimeOffset { get; protected internal set; }
    public virtual AvailabilityType Type { get; protected internal set; }

}

DTO Mapper:

    return new AvailabilityRequestCollectionResource
      {
        AvailabilityRequests = availabilityRequest.Select(AvailabilityRequestMap).ToList()
      };

     private static TEST2 AvailabilityRequestMap(TEST test)
    {
       var availabilityRequestResource = new TEST2
      {
        ID = availabilityRequest.ID,
        EmployeeID = availabilityRequest.Employee.ID,
        GeneralAvailability = AvailabilityTypeMap(availabilityRequest.ABCRange.Where(f => f.Type == AvailabilityType.General)),
        PreferredAvailability = AvailabilityTypeMap(availabilityRequest.ABCRange.Where(f => f.Type == AvailabilityType.Preferred))
      };
    }

    private static List<XYZ> AvailabilityTypeMap(IEnumerable<ABC> abc)
    {
      var availList = new List<XYZ>();

      availList.AddRange(abc.Select(x =>
        new XYZ
        {
          ID = x.ID,
          DayOfWeek = (Day)x.DayOfWeek,
          StartTimeOffset = x.StartTimeOffset,
          EndTimeOffset = x.EndTimeOffset,
          WeekNumber = 1
        }
      ));
      return availList;
    }

now the issue is the above method is taking almost 15sec to create say 10k objects of XYZ. Our performance bottle neck is make it as 2sec and every time the no. of objects may vary. How to make it? We tried parallelism and tasking, those are not helping as lazy loading from domain is in place which we cannot modify.

Note : ABC and XYZ are not having same set of attributes

Thanks

Upvotes: 0

Views: 299

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062630

lazy loading from domain is in place

That is almost certainly 100% of your bottleneck.

which we cannot modify

Then you can't fix it.

You need to challenge the lazy loading, or do something to pre-emptively load that data efficiently (i.e. not per-item - n+1 is brutal).

Creating 10k objects and populating them from values that exist should be pretty-much instantaneous (as long as the implementation doesn't do anything silly). That isn't the issue, and staring at the code that does that: won't help.

Upvotes: 1

Related Questions