I have an application which communicates with a database. Each table has a unique id.
When I pull in a row from the database, I populate a class with all of the fields (this is all generated code).
I originally just used Int32 as the type of the ID, but kept finding that inappropriate types were being passed to methods etc.
My solution to this was to create a specific type which I call idUser (where User is the actual table name). The reason for this is that if I try to pass an idSalesMan into a method which is expecting an idUser, it generates a compile-time error, which is really useful and stops me from making school-boy errors.
The code behind the idUser is like this..
[Serializable]
public struct idUser : IComparable
{
public idUser(int InitVal) { this.m_Main=InitVal; }
private int m_Main;
public int Main { get { return m_Main; } set { m_Main = value; }}
public static implicit operator int(idUser Main) { return Main.m_Main; }
public static implicit operator idUser(int Main) { return new idUser(Main); }
public static bool operator ==(idUser one, idUser two) { return one.m_Main == two.m_Main; }
public static bool operator !=(idUser one, idUser two) { return one.m_Main != two.m_Main; }
public static bool operator <(idUser one, idUser two) { return one.m_Main < two.m_Main; }
public static bool operator >(idUser one, idUser two) { return one.m_Main > two.m_Main; }
public override string ToString() {{ return this.m_Main.ToString(CultureInfo.InvariantCulture); }}
public override bool Equals(object obj) { return this.m_Main == ((idUser)obj).m_Main; }
public override int GetHashCode() { return this.m_Main.GetHashCode(); }
public int CompareTo(object obj) { return this.m_Main.CompareTo(((idUser)obj).m_Main); }
}
This works fine, is easy (as it’s generated by a tool I’ve written), and prevents the issues I present above.
However, as my database has about 100 tables it generates a lot of code, and quite a large executable. (this isn’t a major issue).
I was curious as to the performance impact of using the above structure rather than just a standard Int32, and I was quite shocked.
Creating a List<> populating it, then checking for the existance of some of the keys..
When using List<idUser> it takes 3 times longer to populate it, and 5 times longer to check some of the added items.
This is quite a major hit, so I was looking at alternative ways of doing this.
Ideally, I’d like to be able to say…
public struct idUser : Int32 { }
public struct idCustomer : Int32 { }
But that obviously doesn’t work.
As I said above, it works fine as it is, but just has a performance hit. Does anyone have any ideas about how to incorporate the strict type-checking, whilst retaining the performance of the Int32 ?
Thanks.
I suspect I can explain the performance problem with finding values in a
List<T>– you aren’t implementingIEquatable<T>, so anyEqualscall has to box the value it’s comparing with. You should also implementIComparable<T>for comparisons.You may well find that just this change brings the performance to an acceptable level. It won’t help when populating the list, admittedly – but it’ll depend on what your required operations are.
Note that in most cases I’d expect the bulk of the performance hit to be transferring data from the database to start with; comparing the performance of populating a list just with in-memory data isn’t really a realistic test of the performance impact on your real application.