Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/644349981382549504
deleted 19 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Possible optimizations for this Tuple<int, int> replacement?

I use the following structure in a web service where performance is critical. It It is used for making joins between domain data objects, the key is made of two integers. The idea is to fit the two 32 bits integer in a long (64 bits).

Before using that structure, I used Tuple<int, int> which was really slower.

Here is my question : isIs there any additional optimization to be made  ? (eg e.g.: addtional override, unneeded cast etc...)

public struct CombinedKey : IEquatable<CombinedKey>
{
   private readonly long value;

   public CombinedKey(int item1, int item2)
   {
        unchecked
        {
            value = (long)item1 << 32 | (uint)item2;
        }
    }

    public override int GetHashCode()
    {
        return (int)value ^ (int)(value >> 32);
    }

    public bool Equals(CombinedKey other)
    {
        return this.value == other.value;
    }

    public int Item1
    {
        get
        {
            return (int)(this.value >> 32);
        }
    }

    public int Item2
    {
        get
        {
            return (int)(this.value & 0xffffffff);
        }
    }
}

}

Possible optimizations for this Tuple<int, int> replacement?

I use the following structure in a web service where performance is critical. It is used for making joins between domain data objects, the key is made of two integers. The idea is to fit the two 32 bits integer in a long (64 bits).

Before using that structure, I used Tuple<int, int> which was really slower.

Here is my question : is there any additional optimization to be made  ? (eg : addtional override, unneeded cast etc...)

public struct CombinedKey : IEquatable<CombinedKey>
{
   private readonly long value;

   public CombinedKey(int item1, int item2)
   {
        unchecked
        {
            value = (long)item1 << 32 | (uint)item2;
        }
    }

    public override int GetHashCode()
    {
        return (int)value ^ (int)(value >> 32);
    }

    public bool Equals(CombinedKey other)
    {
        return this.value == other.value;
    }

    public int Item1
    {
        get
        {
            return (int)(this.value >> 32);
        }
    }

    public int Item2
    {
        get
        {
            return (int)(this.value & 0xffffffff);
        }
    }

}

Tuple<int, int> replacement

I use the following structure in a web service where performance is critical. It is used for making joins between domain data objects, the key is made of two integers. The idea is to fit the two 32 bits integer in a long (64 bits).

Before using that structure, I used Tuple<int, int> which was really slower.

Is there any additional optimization to be made? (e.g.: addtional override, unneeded cast etc...)

public struct CombinedKey : IEquatable<CombinedKey>
{
   private readonly long value;

   public CombinedKey(int item1, int item2)
   {
        unchecked
        {
            value = (long)item1 << 32 | (uint)item2;
        }
    }

    public override int GetHashCode()
    {
        return (int)value ^ (int)(value >> 32);
    }

    public bool Equals(CombinedKey other)
    {
        return this.value == other.value;
    }

    public int Item1
    {
        get
        {
            return (int)(this.value >> 32);
        }
    }

    public int Item2
    {
        get
        {
            return (int)(this.value & 0xffffffff);
        }
    }
}
Source Link
tigrou
  • 421
  • 3
  • 11

Possible optimizations for this Tuple<int, int> replacement?

I use the following structure in a web service where performance is critical. It is used for making joins between domain data objects, the key is made of two integers. The idea is to fit the two 32 bits integer in a long (64 bits).

Before using that structure, I used Tuple<int, int> which was really slower.

Here is my question : is there any additional optimization to be made ? (eg : addtional override, unneeded cast etc...)

public struct CombinedKey : IEquatable<CombinedKey>
{
   private readonly long value;

   public CombinedKey(int item1, int item2)
   {
        unchecked
        {
            value = (long)item1 << 32 | (uint)item2;
        }
    }

    public override int GetHashCode()
    {
        return (int)value ^ (int)(value >> 32);
    }

    public bool Equals(CombinedKey other)
    {
        return this.value == other.value;
    }

    public int Item1
    {
        get
        {
            return (int)(this.value >> 32);
        }
    }

    public int Item2
    {
        get
        {
            return (int)(this.value & 0xffffffff);
        }
    }

}