Skip to main content
Code derp
Source Link
JPtheK9
  • 199
  • 1
  • 2
  • 11

I looked at the source code of the dictionary and noticed the use of "buckets". I'm not exactly sure how a dictionary sorts objects into buckets but it's a simple task with integers.

The main problem, I've logiced, is converting the input into an index that's small as possible yet doesn't collide with other inputs' indexes to make the array the outputs are stored in as small as possible, which is where buckets come in.

If the input is 255, the array can't be that large to store the output in the index of 255. Instead, operations are done on the number to determine where its output is stored.

Here's my implementation. It's optimized for my particular use because the largest key is 255.

public class FastMap {
    //8 buckets because the maximum key is 255
    public int[][] Buckets = new int[][8];
    
    public void Add (int input, int output)
    {
        //Finding out which bucket output will be assigned in
        int bucketIndex = input % 32;
        //Finding out the index inside of the bucket the output will be assigned to
        int innerIndex = input / 32;
        //Dropping output into the necessary bucket
        int[] bucket = Buckets[bucketIndex];
        if (bucket == null) 
        {
            bucket = new int[8];
            Buckets[bucketIndex] = bucket;
        }
        bucket[innerIndex] = output;
    }
    public int GetValue (int input)
    {
        return (Buckets[input % 32][input / 32]);
    }
}

I'm going to be playing with this more but already, my implementation out-performs Dictionary<int,int> in both memory use and performance.

I looked at the source code of the dictionary and noticed the use of "buckets". I'm not exactly sure how a dictionary sorts objects into buckets but it's a simple task with integers.

The main problem, I've logiced, is converting the input into an index that's small as possible yet doesn't collide with other inputs' indexes to make the array the outputs are stored in as small as possible, which is where buckets come in.

If the input is 255, the array can't be that large to store the output in the index of 255. Instead, operations are done on the number to determine where its output is stored.

Here's my implementation. It's optimized for my particular use because the largest key is 255.

public class FastMap {
    //8 buckets because the maximum key is 255
    public int[][] Buckets = new int[][8];
    
    public void Add (int input, int output)
    {
        //Finding out which bucket output will be assigned in
        int bucketIndex = input % 32;
        //Finding out the index inside of the bucket the output will be assigned to
        int innerIndex = input / 32;
        //Dropping output into the necessary bucket
        int[] bucket = Buckets[bucketIndex];
        if (bucket == null) bucket = new int[8];
        bucket[innerIndex] = output;
    }
    public int GetValue (int input)
    {
        return (Buckets[input % 32][input / 32]);
    }
}

I'm going to be playing with this more but already, my implementation out-performs Dictionary<int,int> in both memory use and performance.

I looked at the source code of the dictionary and noticed the use of "buckets". I'm not exactly sure how a dictionary sorts objects into buckets but it's a simple task with integers.

The main problem, I've logiced, is converting the input into an index that's small as possible yet doesn't collide with other inputs' indexes to make the array the outputs are stored in as small as possible, which is where buckets come in.

If the input is 255, the array can't be that large to store the output in the index of 255. Instead, operations are done on the number to determine where its output is stored.

Here's my implementation. It's optimized for my particular use because the largest key is 255.

public class FastMap {
    //8 buckets because the maximum key is 255
    public int[][] Buckets = new int[][8];
    
    public void Add (int input, int output)
    {
        //Finding out which bucket output will be assigned in
        int bucketIndex = input % 32;
        //Finding out the index inside of the bucket the output will be assigned to
        int innerIndex = input / 32;
        //Dropping output into the necessary bucket
        int[] bucket = Buckets[bucketIndex];
        if (bucket == null) 
        {
            bucket = new int[8];
            Buckets[bucketIndex] = bucket;
        }
        bucket[innerIndex] = output;
    }
    public int GetValue (int input)
    {
        return (Buckets[input % 32][input / 32]);
    }
}

I'm going to be playing with this more but already, my implementation out-performs Dictionary<int,int> in both memory use and performance.

Source Link
JPtheK9
  • 199
  • 1
  • 2
  • 11

I looked at the source code of the dictionary and noticed the use of "buckets". I'm not exactly sure how a dictionary sorts objects into buckets but it's a simple task with integers.

The main problem, I've logiced, is converting the input into an index that's small as possible yet doesn't collide with other inputs' indexes to make the array the outputs are stored in as small as possible, which is where buckets come in.

If the input is 255, the array can't be that large to store the output in the index of 255. Instead, operations are done on the number to determine where its output is stored.

Here's my implementation. It's optimized for my particular use because the largest key is 255.

public class FastMap {
    //8 buckets because the maximum key is 255
    public int[][] Buckets = new int[][8];
    
    public void Add (int input, int output)
    {
        //Finding out which bucket output will be assigned in
        int bucketIndex = input % 32;
        //Finding out the index inside of the bucket the output will be assigned to
        int innerIndex = input / 32;
        //Dropping output into the necessary bucket
        int[] bucket = Buckets[bucketIndex];
        if (bucket == null) bucket = new int[8];
        bucket[innerIndex] = output;
    }
    public int GetValue (int input)
    {
        return (Buckets[input % 32][input / 32]);
    }
}

I'm going to be playing with this more but already, my implementation out-performs Dictionary<int,int> in both memory use and performance.