Skip to content

Commit 4c77ef2

Browse files
committed
optimized zrem
1 parent 914f9f0 commit 4c77ef2

2 files changed

Lines changed: 65 additions & 45 deletions

File tree

code/serverbrowsing/tasks/GetServers.cpp

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -256,24 +256,29 @@ namespace MM {
256256
}
257257
}
258258
}
259-
260-
if(!req->m_for_game.gamename.empty()) {
259+
260+
if(!req->m_for_game.gamename.empty() && !keys_to_delete.empty()) {
261+
size_t num_keys = keys_to_delete.size();
262+
263+
size_t num_args = 2 + num_keys;
264+
const char **args = (const char **)malloc(num_args * sizeof(const char *));
261265
std::vector<std::string>::iterator del_it = keys_to_delete.begin();
266+
args[0] = "ZREM";
267+
args[1] = req->m_for_game.gamename.c_str();
268+
269+
int idx = 2;
262270
while(del_it != keys_to_delete.end()) {
263-
std::string server_key = *del_it;
264-
std::string gamename = req->m_for_game.gamename; //incase of reference issue
265-
const char *args[] = {"ZREM" , gamename.c_str(), server_key.c_str()};
266-
redisAppendCommandArgv(thread_data->mp_redis_connection, 3, args, NULL);
271+
args[idx] = (*del_it).c_str();
272+
idx++;
267273
del_it++;
268274
}
269-
270-
for(size_t i=0;i<keys_to_delete.size();i++) {
271-
void *reply;
272-
int r = redisGetReply(thread_data->mp_redis_connection, (void **)&reply);
273-
if (r == REDIS_OK) {
274-
freeReplyObject(reply);
275-
}
275+
276+
redisReply* reply = (redisReply*)redisCommandArgv(thread_data->mp_redis_connection, num_args, args, NULL);
277+
278+
if (reply != NULL) {
279+
freeReplyObject(reply);
276280
}
281+
free((void *)args);
277282
}
278283
}
279284

@@ -525,21 +530,29 @@ namespace MM {
525530
it++;
526531
}
527532

528-
std::vector<std::string>::iterator del_it = keys_to_delete.begin();
529-
while(del_it != keys_to_delete.end()) { //delete cache misses
530-
std::string server_key = *del_it;
531-
std::string gamename = req->m_for_game.gamename; //incase of reference issue
532-
const char *args[] = {"ZREM" , gamename.c_str(), server_key.c_str()};
533-
redisAppendCommandArgv(thread_data->mp_redis_connection, 3, args, NULL);
534-
del_it++;
535-
}
536-
for(size_t i=0;i<keys_to_delete.size();i++) {
537-
void *reply;
538-
int r = redisGetReply(thread_data->mp_redis_connection, (void **)&reply);
539-
if (r == REDIS_OK) {
540-
freeReplyObject(reply);
541-
}
542-
}
533+
if(!req->m_for_game.gamename.empty() && !keys_to_delete.empty()) {
534+
size_t num_keys = keys_to_delete.size();
535+
536+
size_t num_args = 2 + num_keys;
537+
const char **args = (const char **)malloc(num_args * sizeof(const char *));
538+
std::vector<std::string>::iterator del_it = keys_to_delete.begin();
539+
args[0] = "ZREM";
540+
args[1] = req->m_for_game.gamename.c_str();
541+
542+
int idx = 2;
543+
while(del_it != keys_to_delete.end()) {
544+
args[idx] = (*del_it).c_str();
545+
idx++;
546+
del_it++;
547+
}
548+
549+
redisReply* reply = (redisReply*)redisCommandArgv(thread_data->mp_redis_connection, num_args, args, NULL);
550+
551+
if (reply != NULL) {
552+
freeReplyObject(reply);
553+
}
554+
free((void *)args);
555+
}
543556
}
544557

545558
void LoadCustomServerInfo_AllKeys(MM::Server* server, redisReply* custom_keys_response) {

code/utmaster/tasks/PerformListServers.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,30 @@ namespace MM {
196196

197197
//delete deferred cache misses
198198
std::vector<std::string>::iterator del_it = keys_to_delete.begin();
199-
int cmd_count = 0;
200-
while(del_it != keys_to_delete.end()) {
201-
std::string key = *del_it;
202-
std::string gamename = request.peer->GetGameData().gamename; //incase of reference issue
203-
const char *args[] = {"ZREM" , gamename.c_str(), key.c_str()};
204-
redisAppendCommandArgv(thread_data->mp_redis_connection, 3, args, NULL);
205-
cmd_count++;
206-
del_it++;
207-
}
208-
209-
for(int i=0;i<cmd_count;i++) {
210-
void *reply;
211-
int r = redisGetReply(thread_data->mp_redis_connection, (void**)&reply);
212-
if(r == REDIS_OK) {
213-
freeReplyObject(reply);
214-
}
215-
}
199+
if(!keys_to_delete.empty()) {
200+
std::string gamename = request.peer->GetGameData().gamename;
201+
size_t num_keys = keys_to_delete.size();
202+
203+
size_t num_args = 2 + num_keys;
204+
const char **args = (const char **)malloc(num_args * sizeof(const char *));
205+
std::vector<std::string>::iterator del_it = keys_to_delete.begin();
206+
args[0] = "ZREM";
207+
args[1] = gamename.c_str();
208+
209+
int idx = 2;
210+
while(del_it != keys_to_delete.end()) {
211+
args[idx] = (*del_it).c_str();
212+
idx++;
213+
del_it++;
214+
}
215+
216+
redisReply* reply = (redisReply*)redisCommandArgv(thread_data->mp_redis_connection, num_args, args, NULL);
217+
218+
if (reply != NULL) {
219+
freeReplyObject(reply);
220+
}
221+
free((void *)args);
222+
}
216223

217224
return records;
218225
}

0 commit comments

Comments
 (0)