Reputation: 36626
I am trying to hydrate a list of ~400 business objects and the performance becomes very slow when it needs to hydrate strings. It's taking in excess of 20secs to hydrate the 400 objects.
EDIT
We are using MySQL 5.1 and dotConnect for MySQL v5.0.12 as the data provider http://www.devart.com/dotconnect/mysql/
I did some benchmarks to narrow it down to the string types causing the problems. I started measuring the time from record 2 to n to ignore the time that might get taken up loading other assemblies.
The following code hydrates 1 object in 0ms
objUserInfo.PortalID = portalId
objUserInfo.IsSuperUser = Convert.ToBoolean(dr("IsSuperUser"))
objUserInfo.UserID = Convert.ToInt32(dr("UserID"))
This also hydrates 1 object in 0ms
objUserInfo.PortalID = portalId
objUserInfo.IsSuperUser = Convert.ToBoolean(dr("IsSuperUser"))
objUserInfo.UserID = Convert.ToInt32(dr("UserID"))
objUserInfo.Firstname = "FirstName"
However as soon as i do the convert on the datareader object to a string it takes an average of 53ms
objUserInfo.PortalID = portalId
objUserInfo.IsSuperUser = Convert.ToBoolean(dr("IsSuperUser"))
objUserInfo.UserID = Convert.ToInt32(dr("UserID"))
objUserInfo.Firstname = Convert.ToString(dr("FirstName"))
I also tried hydrating 2 strings and strangley it doesn't blow the performance out nearly as much as 1 string? The following only takes an average of 57ms to hydrate 1 object
objUserInfo.PortalID = portalId
objUserInfo.IsSuperUser = Convert.ToBoolean(dr("IsSuperUser"))
objUserInfo.UserID = Convert.ToInt32(dr("UserID"))
objUserInfo.Firstname = Convert.ToString(dr("FirstName"))
objUserInfo.LastName = Convert.ToString(dr("LastName"))
I know that a lot of people use the above syntax to hydrate business objects. Is there a more efficient/faster way to do this?
EDIT Just did another test which was to do a directcast on a string and it produces the same slow speeds :( 53ms just to do the cast.
objUserInfo.FirstName = DirectCast("alex", String)
Upvotes: 0
Views: 1416
Reputation: 4602
Give this a try:
Instead of
objUserInfo.Firstname = Convert.ToString(dr("FirstName"))
use
objUserInfo.Firstname = dr.GetString(2);
Convert.ToString(dr("FirstName"))
is doing some ugly implicit and time consuming conversions and/or (un)boxing operations.)Edit: Alex found the problem. It's not a casting problem! His comment about it:
Alright i've tracked down the problem! I'm using DotNetNuke and filling one of it's standard objects. The problem is that when i set the FirstName property it makes another call out to a profile object which is what actually takes so long! It's nothing to do with string conversion or casting.
Upvotes: 2
Reputation: 4665
What data provider do you use? We had an ugly issue with oracle one.
The most efficient would be to call dr.GetString(columnNumber) - where the column number you get by dr.GetOrdinal(columnName) (which you can cache at the beginning of your read loop).
However this should not really be the issue. Can't it be the size of columns?
Upvotes: 1
Reputation: 19004
I also tried hydrating 2 strings and strangley it doesn't blow the performance out nearly as much as 1 string? The following only takes an average of 57ms to hydrate 1 object
How are you measuring the time? Maybe there's an additional assembly loading involved and your 53 ms takes that into account.
You should start measuring the time only after the first loop (first hydration). This way any assembly loading will be done before the measurement.
Upvotes: 0