Skip to content

one question about the code in dictScan  #1926

Open

Description

@hzkltt

when i learn the function dictScan in redis source code 2.78. i found below code: v = (((v | m0) + 1) & ~m0) | (v & m0); my question is: the result of the code seems same with code: v = ((v | m0) + 1) | (v & m0) which has no "&~m0". i run a test about for these two code. and found that the result is same.
my test code is as below.( i wrote the result of two case into two file and compare the file found that the two file is same.) if i have something wrong, please correct me(please provide the test case for the different result of above two code). many thanks.

#define MAXSIZE 1000000

void binaryResize( int m0,int m1, int v,FILE* fp,FILE* fp1)
{
    int temp = v;
    do 
    {
        fprintf(fp,"v=%d,",v);
        v = (((v | m0) + 1) & ~m0) | (v & m0);
    } while (v & (m0 ^ m1));

    v = temp;
    do 
    {
        fprintf(fp1,"v=%d,",v);
        v = (((v | m0) + 1)) | (v & m0);
    } while (v & (m0 ^ m1));

}

int main()
{
    int m0=7,m1=63;
    int v;
    FILE* fp,*fp1;
    char *path = "/mnt/hgfs/Shared/c++/tmplog";
    char *path1 = "/mnt/hgfs/Shared/c++/tmplog1";
    fp = fopen(path,"a");
    fp1 = fopen(path1,"a");
    for( v=1; v < MAXSIZE; v++)
    {
        binaryResize(m0,m1,v,fp,fp1);
    }
    v=-1;
    fprintf(fp1,"v=%d,",v);
    fclose(fp);
    fclose(fp1);
}

Activity

  1. mattsta commented on Aug 11, 2014

    @mattsta
    Contributor

    You are correct!

    Why is ~m0 unnecessary?

    • The size mask for each hash table is guaranteed to be 2^k - 1. ◀️ That invariant is necessary for the final result to remain valid when removing ~m0.
    • Because m0 is always one minus a power of two, m0 always has uniformly set lower bits.
    • Because m0 has uniformly set lower bits, adding 1 to the union of m0 and v zeros out all existing lower bit 1 positions in (v | m0).
    • Because the 1 positions get zero'd out, asking for the AND has no effect because you are ANDing: the the positions zero'd out by the + 1 in (v | m0) against the original 1 positions in m0 which turned into uniform 0 values when you did "NOT ALL MY ONES!"
    • 0 & 0 = 0. qed.

    Or, using a concrete example:

    v = 00110100 # (52)
    m0 = 01111111 # (63)
    
    ((00110100 | 01111111) + 1) & ~01111111 =
    ((01111111) + 1) & 10000000 =
    (10000000 & 10000000) =
    10000000

    It works without ~m0 as long as m0 takes the form of 2^k - 1, which is always does in our use cases.

  2. antirez commented on Aug 15, 2014

    @antirez
    Contributor

    Hello, this was noticed months ago by @soveran, the reason for taking the redundant code is that it helps to explain what is going on. However we could turn this issue into adding a comment there in order to specify that the final mask is redundant but is there just to make sure the reader understands in bolder terms what is going on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions