Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
1a132bbc
Commit
1a132bbc
authored
May 28, 2010
by
Pieter Noordhuis
Browse files
use qsort and bsearch to lookup commands in O(log(N)) instead of O(N)
parent
3caf15e6
Changes
1
Hide whitespace changes
Inline
Side-by-side
redis.c
View file @
1a132bbc
...
@@ -752,7 +752,9 @@ static void unwatchCommand(redisClient *c);
...
@@ -752,7 +752,9 @@ static void unwatchCommand(redisClient *c);
/* Global vars */
/* Global vars */
static
struct
redisServer
server
;
/* server global state */
static
struct
redisServer
server
;
/* server global state */
static
struct
redisCommand
cmdTable
[]
=
{
static
struct
redisCommand
*
commandTable
;
static
unsigned
int
commandTableSize
;
static
struct
redisCommand
readonlyCommandTable
[]
=
{
{
"get"
,
getCommand
,
2
,
REDIS_CMD_INLINE
,
NULL
,
1
,
1
,
1
},
{
"get"
,
getCommand
,
2
,
REDIS_CMD_INLINE
,
NULL
,
1
,
1
,
1
},
{
"set"
,
setCommand
,
3
,
REDIS_CMD_BULK
|
REDIS_CMD_DENYOOM
,
NULL
,
0
,
0
,
0
},
{
"set"
,
setCommand
,
3
,
REDIS_CMD_BULK
|
REDIS_CMD_DENYOOM
,
NULL
,
0
,
0
,
0
},
{
"setnx"
,
setnxCommand
,
3
,
REDIS_CMD_BULK
|
REDIS_CMD_DENYOOM
,
NULL
,
0
,
0
,
0
},
{
"setnx"
,
setnxCommand
,
3
,
REDIS_CMD_BULK
|
REDIS_CMD_DENYOOM
,
NULL
,
0
,
0
,
0
},
...
@@ -2247,13 +2249,33 @@ static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int
...
@@ -2247,13 +2249,33 @@ static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int
}
}
}
}
static
int
qsortRedisCommands
(
const
void
*
r1
,
const
void
*
r2
)
{
return
strcasecmp
(
((
struct
redisCommand
*
)
r1
)
->
name
,
((
struct
redisCommand
*
)
r2
)
->
name
);
}
static
void
sortCommandTable
()
{
int
i
=
0
,
size
=
0
;
/* Determine and store the size of the command table */
while
(
readonlyCommandTable
[
i
++
].
name
!=
NULL
)
size
++
;
commandTableSize
=
size
;
/* Copy and sort the read-only version of the command table */
commandTable
=
(
struct
redisCommand
*
)
malloc
(
sizeof
(
readonlyCommandTable
));
memcpy
(
commandTable
,
readonlyCommandTable
,
sizeof
(
readonlyCommandTable
));
qsort
(
commandTable
,
size
,
sizeof
(
struct
redisCommand
),
qsortRedisCommands
);
}
static
struct
redisCommand
*
lookupCommand
(
char
*
name
)
{
static
struct
redisCommand
*
lookupCommand
(
char
*
name
)
{
int
j
=
0
;
struct
redisCommand
tmp
=
{
name
,
NULL
,
0
,
0
,
NULL
,
0
,
0
,
0
};
while
(
cmdTable
[
j
].
name
!=
NULL
)
{
return
bsearch
(
if
(
!
strcasecmp
(
name
,
cmdTable
[
j
].
name
))
return
&
cmdTable
[
j
];
&
tmp
,
j
++
;
commandTable
,
}
commandTableSize
,
return
NULL
;
sizeof
(
struct
redisCommand
),
qsortRedisCommands
);
}
}
/* resetClient prepare the client to process the next command */
/* resetClient prepare the client to process the next command */
...
@@ -10935,6 +10957,7 @@ int main(int argc, char **argv) {
...
@@ -10935,6 +10957,7 @@ int main(int argc, char **argv) {
time_t
start
;
time_t
start
;
initServerConfig
();
initServerConfig
();
sortCommandTable
();
if
(
argc
==
2
)
{
if
(
argc
==
2
)
{
if
(
strcmp
(
argv
[
1
],
"-v"
)
==
0
||
if
(
strcmp
(
argv
[
1
],
"-v"
)
==
0
||
strcmp
(
argv
[
1
],
"--version"
)
==
0
)
version
();
strcmp
(
argv
[
1
],
"--version"
)
==
0
)
version
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment