From 558670bc6363e4b5eef44247d3ecb24fd5b99a47 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Mon, 4 Aug 2025 17:34:57 +0800 Subject: [PATCH] performance opt.: replace sprintf as necessary --- src/connection_pool.c | 2 +- src/connection_pool.h | 10 ++++++++-- src/fast_buffer.c | 4 ++-- src/hash.c | 11 +++++------ src/ini_file_reader.c | 3 +-- src/logger.c | 24 +++++++++++++++++------- src/process_ctrl.c | 2 +- src/server_id_func.c | 15 +++++++++------ src/shared_func.c | 22 ++++++++++++---------- 9 files changed, 56 insertions(+), 37 deletions(-) diff --git a/src/connection_pool.c b/src/connection_pool.c index e5b5abd..26b256f 100644 --- a/src/connection_pool.c +++ b/src/connection_pool.c @@ -511,7 +511,7 @@ static ConnectionInfo *get_conn(ConnectionPool *cp, { if (cm->head == NULL) { - if ((cp->max_count_per_entry > 0) && + if ((cp->max_count_per_entry > 0) && (cm->total_count >= cp->max_count_per_entry)) { format_ip_address(conn->ip_addr, formatted_ip); diff --git a/src/connection_pool.h b/src/connection_pool.h index b3f670c..716c5c3 100644 --- a/src/connection_pool.h +++ b/src/connection_pool.h @@ -462,8 +462,14 @@ int conn_pool_parse_server_info(const char *pServerStr, static inline void conn_pool_set_server_info(ConnectionInfo *pServerInfo, const char *ip_addr, const int port) { - snprintf(pServerInfo->ip_addr, sizeof(pServerInfo->ip_addr), - "%s", ip_addr); + int len; + + len = strlen(ip_addr); + if (len >= sizeof(pServerInfo->ip_addr)) { + len = sizeof(pServerInfo->ip_addr) - 1; + } + memcpy(pServerInfo->ip_addr, ip_addr, len); + *(pServerInfo->ip_addr + len) = '\0'; pServerInfo->port = port; pServerInfo->af = is_ipv6_addr(ip_addr) ? AF_INET6 : AF_INET; pServerInfo->sock = -1; diff --git a/src/fast_buffer.c b/src/fast_buffer.c index 1b8a755..c48b522 100644 --- a/src/fast_buffer.c +++ b/src/fast_buffer.c @@ -180,7 +180,7 @@ int fast_buffer_append_int(FastBuffer *buffer, const int n) return result; } - buffer->length += sprintf(buffer->data + buffer->length, "%d", n); + buffer->length += fc_itoa(n, buffer->data + buffer->length); return 0; } @@ -193,7 +193,7 @@ int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n) return result; } - buffer->length += sprintf(buffer->data + buffer->length, "%"PRId64, n); + buffer->length += fc_itoa(n, buffer->data + buffer->length); return 0; } diff --git a/src/hash.c b/src/hash.c index 367248d..cf6f80b 100644 --- a/src/hash.c +++ b/src/hash.c @@ -768,23 +768,22 @@ int64_t fc_hash_inc_value(const HashData *old_data, const int inc, { if (old_data->value_len < *new_value_len) { - memcpy(new_value, old_data->value, old_data->value_len); - new_value[old_data->value_len] = '\0'; - n = strtoll(new_value, NULL, 10); - n += inc; + memcpy(new_value, old_data->value, old_data->value_len); + new_value[old_data->value_len] = '\0'; + n = strtoll(new_value, NULL, 10); + n += inc; } else { n = inc; } - *new_value_len = sprintf(new_value, "%"PRId64, n); } else { n = inc; - *new_value_len = sprintf(new_value, "%"PRId64, n); } + *new_value_len = fc_itoa(n, new_value); return n; } diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 54176dc..ca90afa 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -2629,8 +2629,7 @@ static char *iniProccessFor(char *content, const int content_len, char *pRemain; int remainLen; - valueLen = sprintf(value, "%d", i); - + valueLen = fc_itoa(i, value); pRemain = pForBlock; remainLen = forBlockLen; while (remainLen >= tagLen) diff --git a/src/logger.c b/src/logger.c index 348fd06..4cbc6b1 100644 --- a/src/logger.c +++ b/src/logger.c @@ -590,7 +590,9 @@ static int log_get_matched_files(LogContext *pContext, the_time = get_current_time() - days_before * 86400; localtime_r(&the_time, &tm); memset(filename_prefix, 0, sizeof(filename_prefix)); - len = sprintf(filename_prefix, "%s.", log_filename); + len = strlen(log_filename); + memcpy(filename_prefix, log_filename, len); + *(filename_prefix + len++) = '.'; strftime(filename_prefix + len, sizeof(filename_prefix) - len, rotate_time_format_prefix, &tm); prefix_filename_len = strlen(filename_prefix); @@ -703,7 +705,9 @@ int log_delete_old_files(void *args) the_time -= 86400; localtime_r(&the_time, &tm); memset(old_filename, 0, sizeof(old_filename)); - len = sprintf(old_filename, "%s.", pContext->log_filename); + len = strlen(pContext->log_filename); + memcpy(old_filename, pContext->log_filename, len); + *(old_filename + len++) = '.'; strftime(old_filename + len, sizeof(old_filename) - len, pContext->rotate_time_format, &tm); if ((result=log_delete_old_file(pContext, old_filename)) != 0) @@ -843,7 +847,9 @@ int log_rotate(LogContext *pContext) localtime_r(¤t_time, &tm); memset(old_filename, 0, sizeof(old_filename)); - len = sprintf(old_filename, "%s.", pContext->log_filename); + len = strlen(pContext->log_filename); + memcpy(old_filename, pContext->log_filename, len); + *(old_filename + len++) = '.'; strftime(old_filename + len, sizeof(old_filename) - len, pContext->rotate_time_format, &tm); if (access(old_filename, F_OK) == 0) @@ -1045,10 +1051,14 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \ } if (caption != NULL) - { - buff_len = sprintf(pContext->pcurrent_buff, "%s - ", caption); - pContext->pcurrent_buff += buff_len; - } + { + buff_len = strlen(caption); + memcpy(pContext->pcurrent_buff, caption, buff_len); + pContext->pcurrent_buff += buff_len; + *pContext->pcurrent_buff++ = ' '; + *pContext->pcurrent_buff++ = '-'; + *pContext->pcurrent_buff++ = ' '; + } memcpy(pContext->pcurrent_buff, text, text_len); pContext->pcurrent_buff += text_len; *pContext->pcurrent_buff++ = '\n'; diff --git a/src/process_ctrl.c b/src/process_ctrl.c index 92b6971..74a4848 100644 --- a/src/process_ctrl.c +++ b/src/process_ctrl.c @@ -50,7 +50,7 @@ int write_to_pid_file(const char *pidFilename) char buff[32]; int len; - len = sprintf(buff, "%d", (int)getpid()); + len = fc_itoa(getpid(), buff); return writeToFile(pidFilename, buff, len); } diff --git a/src/server_id_func.c b/src/server_id_func.c index 16ea874..3a9c9be 100644 --- a/src/server_id_func.c +++ b/src/server_id_func.c @@ -294,15 +294,18 @@ FCServerInfo *fc_server_get_by_ip_port_ex(FCServerConfig *ctx, static inline void fc_server_set_group_ptr_name(FCServerGroupInfo *ginfo, const char *group_name) { + int len; + ginfo->group_name.str = ginfo->name_buff; - ginfo->group_name.len = snprintf(ginfo->name_buff, - sizeof(ginfo->name_buff) - 1, "%s", group_name); - if (ginfo->group_name.len == 0) { - return; + len = strlen(group_name); + if (len >= sizeof(ginfo->name_buff)) { + len = sizeof(ginfo->name_buff) - 1; } - fc_trim(ginfo->group_name.str); - ginfo->group_name.len = strlen(ginfo->group_name.str); + memcpy(ginfo->name_buff, group_name, len); + *(ginfo->name_buff + len) = '\0'; + fc_trim(ginfo->name_buff); + ginfo->group_name.len = strlen(ginfo->name_buff); } static inline void fc_server_set_ip_prefix(FCServerGroupInfo *ginfo, diff --git a/src/shared_func.c b/src/shared_func.c index d663023..50a2329 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -528,18 +528,20 @@ void daemon_init(bool bCloseFiles) char *bin2hex(const char *s, const int len, char *szHexBuff) { + const char *hex_chars = "0123456789abcdef"; unsigned char *p; unsigned char *pEnd; - int nLen; - - nLen = 0; + char *dest; + + dest = szHexBuff; pEnd = (unsigned char *)s + len; for (p=(unsigned char *)s; p> 4) & 0x0F]; + *dest++ = hex_chars[*p & 0x0F]; + } + + *dest = '\0'; return szHexBuff; } @@ -3231,7 +3233,7 @@ static void add_thousands_separator(char *str, const int len) const char *int2str(const int n, char *buff, const bool thousands_separator) { int len; - len = sprintf(buff, "%d", n); + len = fc_itoa(n, buff); if (thousands_separator) { add_thousands_separator(buff, len); @@ -3242,7 +3244,7 @@ const char *int2str(const int n, char *buff, const bool thousands_separator) const char *long2str(const int64_t n, char *buff, const bool thousands_separator) { int len; - len = sprintf(buff, "%"PRId64, n); + len = fc_itoa(n, buff); if (thousands_separator) { add_thousands_separator(buff, len);