> 1. Profile AttributeMappingSource and find out why it takes so long to
> create.

I found that  AttributedMetaModel.cs : FindTables takes  905 ms by using 
code in end of this message.

Longest statement is only AddTableType call. Only this statemets takse more 
than 10 ms in only 4 times from hundreds of calls:

239 ms, 11 ms, 29 ms, 11 ms

There is always one AddTableType call which takes huge time (239 ms).

This is occurs at 21 ... 23 th member, seems not related to concrete member 
type.
Without those delays FindTable takes 610 ms, no single statement will take 
more than 10 seconds. 610 ms is very long time for this.

Maybe this is O(n) isse:

FindTables scans hundreds of members but processes tables only:

            for (int i = 0; i < memberInfos.Length; ++i)
            {
                var info = memberInfos[i];
                Type memberType = info.GetMemberType();
                if (memberType == null)
                    continue;

                if (memberType.IsGenericType)
                {
                    if (memberType.GetGenericTypeDefinition() != 
typeof(Table<>))
                        continue;
                    Type argumentType = memberType.GetGenericArguments()[0];
                    if (argumentType.IsGenericParameter)
                        continue;
//        ........ real processing occurs only in addtabletype:
                    AddTableType(argumentType);

AddTableType calls SetupAssociations() which scans also all members of 
argumentType:

   foreach (var memberInfo in type.GetMembers())

Entity classes are partial and inherit are from EntityBase class. So every 
entity may contain hundreds of member.

For 300 tables having average 50 members there are 15000 iterations whose 
where inner loop contains number reflection calls.
No idea how to optimize or profile it further. I noticed that .NET classes 
contain members whese name contains cache. It seems that
they use caching to speed up reflection. So maybe some cache can help here.

> 2. In your code, use the DataContext(IDbConnection, MappingSource) or
> DataContext(string, MappingSource) constructors.  This allows you to
> provide your own (already constructed) MappingSource value, thus
> preventing DataContext from creating a new AttributeMappingSource
> instance.

In my case this solution moves the problem from DbLinq to web service code:

DataContext classes are instantiated from web service calls. Single web 
service call may instantiate same DataContext class 15 times.
There may be multple concurrent web services running (net runs them in 
multiple threads).

Creating and using single DataContext in every web service causes still 900 
ms delay. Also in some cases web service must instantiate same datacontext 
multiple times.

Very quick look into AttributeMappingSource() did'nt show thread unsafety. 
Is it reasonable to assume undocumented feature that 
AttributeMappingSource() instance member calls ares thread-safe ?

Or is it OK to apply patch which uses

lock(this) {  .... }

locks to make AttributeMappingSource()  thread safe ?
Or should there be ThreadSafeAttributeMappingSource()  class like 
ThreadSafeDictionay is created from Dictionary class in dblinq ?

Andrus.



Code used for profiling:

        private void FindTables()
        {

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            MemberInfo[] memberInfos = 
_ContextType.GetMembers(BindingFlags.GetField
                    | BindingFlags.GetProperty | BindingFlags.Static | 
BindingFlags.Instance
                    | BindingFlags.NonPublic | BindingFlags.Public);
            Debug.WriteLine(memberInfos.Length.ToString() + " members " + 
stopWatch.ElapsedMilliseconds.ToString());

            //foreach (var info in memberInfos)
            for (int i = 0; i < memberInfos.Length; ++i)
            {
                var sw2 = new Stopwatch();
                sw2.Start();

                var info = memberInfos[i];

                Type memberType = info.GetMemberType();
                if (sw2.ElapsedMilliseconds > 10)
                    Debug.Write(" getmembertype " + 
sw2.ElapsedMilliseconds.ToString());

                if (memberType == null)
                {
                    continue;
                }

                //Ok first possible problem here - there seems to be the 
.net ITable/Table and the local one
                //Same goes for the attribute types
                //Any reason for that?
                //looking for a table generic
                if (memberType.IsGenericType)
                {
                    if (memberType.GetGenericTypeDefinition() != 
typeof(Table<>))
                    {
                        continue;
                    }

                    Type argumentType = memberType.GetGenericArguments()[0];

                    //If the argument type is a generic parameter we are not 
interested
                    //Most likly it is the GetTable function
                    if (argumentType.IsGenericParameter)
                    {
                        continue;
                    }
                    if (sw2.ElapsedMilliseconds > 10)
                        Debug.Write(memberType.ToString() + " 
isgenericparameter " + sw2.Elapsed.ToString());

                    AddTableType(argumentType);
                    if (sw2.ElapsedMilliseconds > 10)
                        Debug.WriteLine(i.ToString() + " " + 
memberType.ToString() + " addtabletype " + 
sw2.ElapsedMilliseconds.ToString());
                }
                if (sw2.ElapsedMilliseconds > 10)
                    Debug.WriteLine(i.ToString() + " " + 
memberType.ToString() + " for end " + sw2.ElapsedMilliseconds.ToString());
            }
            Debug.WriteLine(" exit findtables " + 
stopWatch.ElapsedMilliseconds.ToString());
        }


output if datacontext is created in for loop:

458 members 0
23 DbLinq.Data.Linq.Table`1[Eeva.Business.Sugulus] addtabletype 245
23 DbLinq.Data.Linq.Table`1[Eeva.Business.Sugulus] for end 245
97 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] addtabletype 11
97 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] for end 11
168 DbLinq.Data.Linq.Table`1[Eeva.Business.Kaardika] addtabletype 26
168 DbLinq.Data.Linq.Table`1[Eeva.Business.Kaardika] for end 26
184 DbLinq.Data.Linq.Table`1[Eeva.Business.Kontekst] addtabletype 37
184 DbLinq.Data.Linq.Table`1[Eeva.Business.Kontekst] for end 38
370 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] addtabletype 11
370 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] for end 11
389 DbLinq.Data.Linq.Table`1[Eeva.Business.Report] addtabletype 13
389 DbLinq.Data.Linq.Table`1[Eeva.Business.Report] for end 13
 exit findtables 937
458 members 0
23 DbLinq.Data.Linq.Table`1[Eeva.Business.Sugulus] addtabletype 262
23 DbLinq.Data.Linq.Table`1[Eeva.Business.Sugulus] for end 262
97 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] addtabletype 11
97 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] for end 12
355 DbLinq.Data.Linq.Table`1[Eeva.Business.Naidud] addtabletype 27
355 DbLinq.Data.Linq.Table`1[Eeva.Business.Naidud] for end 28
370 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] addtabletype 11
370 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] for end 11
 exit findtables 920
458 members 0
23 DbLinq.Data.Linq.Table`1[Eeva.Business.Sugulus] addtabletype 239
23 DbLinq.Data.Linq.Table`1[Eeva.Business.Sugulus] for end 240
97 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] addtabletype 11
97 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] for end 12
307 DbLinq.Data.Linq.Table`1[Eeva.Business.Hinnamtr] addtabletype 29
307 DbLinq.Data.Linq.Table`1[Eeva.Business.Hinnamtr] for end 29
370 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] addtabletype 11
370 DbLinq.Data.Linq.Table`1[Eeva.Business.Parameet] for end 11
 exit findtables 905


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DbLinq" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/dblinq?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to