Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize rdbEncodeInteger using bit operations #196

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/rdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,38 @@ uint64_t rdbLoadLen(rio *rdb, int *isencoded) {
* representation is stored in the buffer pointer to by "enc" and the string
* length is returned. Otherwise 0 is returned. */
int rdbEncodeInteger(long long value, unsigned char *enc) {
if (value >= -(1<<7) && value <= (1<<7)-1) {
struct SignExtendBits{
long long bits8: 8;
long long bits16: 16;
long long bits32: 32;
} v;

v.bits8 = value;
if (v.bits8 == value) {
enc[0] = (RDB_ENCVAL<<6)|RDB_ENC_INT8;
enc[1] = value&0xFF;
enc[1] = v.bits8;
return 2;
} else if (value >= -(1<<15) && value <= (1<<15)-1) {
}

v.bits16 = value;
if (v.bits16 == value) {
enc[0] = (RDB_ENCVAL<<6)|RDB_ENC_INT16;
enc[1] = value&0xFF;
enc[2] = (value>>8)&0xFF;
return 3;
} else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) {
}

v.bits32 = value;
if (v.bits32 == value) {
enc[0] = (RDB_ENCVAL<<6)|RDB_ENC_INT32;
enc[1] = value&0xFF;
enc[2] = (value>>8)&0xFF;
enc[3] = (value>>16)&0xFF;
enc[4] = (value>>24)&0xFF;
return 5;
} else {
return 0;
}

return 0;
}

/* Loads an integer-encoded object with the specified encoding type "enctype".
Expand Down