views:

555

answers:

3

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)
A: 

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.

Igor Brejc
Yeah i only measure from 2 to n. The first one takes over 150ms. I'll edit the question to include that.
Alex
+1  A: 

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?

Rashack
we're using dotConnect for MySQL v5.0.12 http://www.devart.com/dotconnect/mysql/
Alex
+2  A: 

Give this a try:

Instead of

objUserInfo.Firstname = Convert.ToString(dr("FirstName"))

use

objUserInfo.Firstname = dr.GetString(2);
  1. Use the GetXXX methods and rely on Microsoft to have optimized the conversion. (Edit: I am suspecting that Convert.ToString(dr("FirstName")) is doing some ugly implicit and time consuming conversions and/or (un)boxing operations.)
  2. Instead of accessing the column with a string indexer ("FirstName") use a number indexer (2). That's faster, but less readable.

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.

Theo Lenndorff
I tried dr.GetString(2). Over 3 iterations of the same 400 objects it gave me a performance improvement of 1ms :( so it averages 52ms now :)
Alex
Is there still some Convert.ToString(dr(...)) around? If yes, replace or delete those lines.BTW: What database do you use and what libraries do you use to connect?
Theo Lenndorff
Nope this is the line objUserInfo.FirstName = dr.GetString(3). I have edited the question to reflect the db and provider. It's MySQL 5.1 and dotConnect
Alex
Ok, then you have to rely on MySQL to have optimized the conversions. ;-)I hope you don't think I am nitpicking, but did you really eliminated all(!) Convert operations and exclusively use the GetXXX methods for all(!) fields?
Theo Lenndorff
I meant rely on DevArt ... not MySql ...
Theo Lenndorff
Yep 100% sure :). Just did another test and added result to question
Alex
Now that's more of a guess, since I don't know much about the MySQL libraries: Try GetValue(2).ToString().
Theo Lenndorff
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.
Alex