performance opt.: replace sprintf as necessary

use_iouring
YuQing 2025-08-04 17:34:57 +08:00
parent cf16c41054
commit 558670bc63
9 changed files with 56 additions and 37 deletions

View File

@ -462,8 +462,14 @@ int conn_pool_parse_server_info(const char *pServerStr,
static inline void conn_pool_set_server_info(ConnectionInfo *pServerInfo, static inline void conn_pool_set_server_info(ConnectionInfo *pServerInfo,
const char *ip_addr, const int port) const char *ip_addr, const int port)
{ {
snprintf(pServerInfo->ip_addr, sizeof(pServerInfo->ip_addr), int len;
"%s", ip_addr);
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->port = port;
pServerInfo->af = is_ipv6_addr(ip_addr) ? AF_INET6 : AF_INET; pServerInfo->af = is_ipv6_addr(ip_addr) ? AF_INET6 : AF_INET;
pServerInfo->sock = -1; pServerInfo->sock = -1;

View File

@ -180,7 +180,7 @@ int fast_buffer_append_int(FastBuffer *buffer, const int n)
return result; return result;
} }
buffer->length += sprintf(buffer->data + buffer->length, "%d", n); buffer->length += fc_itoa(n, buffer->data + buffer->length);
return 0; return 0;
} }
@ -193,7 +193,7 @@ int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n)
return result; return result;
} }
buffer->length += sprintf(buffer->data + buffer->length, "%"PRId64, n); buffer->length += fc_itoa(n, buffer->data + buffer->length);
return 0; return 0;
} }

View File

@ -777,14 +777,13 @@ int64_t fc_hash_inc_value(const HashData *old_data, const int inc,
{ {
n = inc; n = inc;
} }
*new_value_len = sprintf(new_value, "%"PRId64, n);
} }
else else
{ {
n = inc; n = inc;
*new_value_len = sprintf(new_value, "%"PRId64, n);
} }
*new_value_len = fc_itoa(n, new_value);
return n; return n;
} }

View File

@ -2629,8 +2629,7 @@ static char *iniProccessFor(char *content, const int content_len,
char *pRemain; char *pRemain;
int remainLen; int remainLen;
valueLen = sprintf(value, "%d", i); valueLen = fc_itoa(i, value);
pRemain = pForBlock; pRemain = pForBlock;
remainLen = forBlockLen; remainLen = forBlockLen;
while (remainLen >= tagLen) while (remainLen >= tagLen)

View File

@ -590,7 +590,9 @@ static int log_get_matched_files(LogContext *pContext,
the_time = get_current_time() - days_before * 86400; the_time = get_current_time() - days_before * 86400;
localtime_r(&the_time, &tm); localtime_r(&the_time, &tm);
memset(filename_prefix, 0, sizeof(filename_prefix)); 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, strftime(filename_prefix + len, sizeof(filename_prefix) - len,
rotate_time_format_prefix, &tm); rotate_time_format_prefix, &tm);
prefix_filename_len = strlen(filename_prefix); prefix_filename_len = strlen(filename_prefix);
@ -703,7 +705,9 @@ int log_delete_old_files(void *args)
the_time -= 86400; the_time -= 86400;
localtime_r(&the_time, &tm); localtime_r(&the_time, &tm);
memset(old_filename, 0, sizeof(old_filename)); 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, strftime(old_filename + len, sizeof(old_filename) - len,
pContext->rotate_time_format, &tm); pContext->rotate_time_format, &tm);
if ((result=log_delete_old_file(pContext, old_filename)) != 0) if ((result=log_delete_old_file(pContext, old_filename)) != 0)
@ -843,7 +847,9 @@ int log_rotate(LogContext *pContext)
localtime_r(&current_time, &tm); localtime_r(&current_time, &tm);
memset(old_filename, 0, sizeof(old_filename)); 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, strftime(old_filename + len, sizeof(old_filename) - len,
pContext->rotate_time_format, &tm); pContext->rotate_time_format, &tm);
if (access(old_filename, F_OK) == 0) if (access(old_filename, F_OK) == 0)
@ -1046,8 +1052,12 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \
if (caption != NULL) if (caption != NULL)
{ {
buff_len = sprintf(pContext->pcurrent_buff, "%s - ", caption); buff_len = strlen(caption);
memcpy(pContext->pcurrent_buff, caption, buff_len);
pContext->pcurrent_buff += buff_len; pContext->pcurrent_buff += buff_len;
*pContext->pcurrent_buff++ = ' ';
*pContext->pcurrent_buff++ = '-';
*pContext->pcurrent_buff++ = ' ';
} }
memcpy(pContext->pcurrent_buff, text, text_len); memcpy(pContext->pcurrent_buff, text, text_len);
pContext->pcurrent_buff += text_len; pContext->pcurrent_buff += text_len;

View File

@ -50,7 +50,7 @@ int write_to_pid_file(const char *pidFilename)
char buff[32]; char buff[32];
int len; int len;
len = sprintf(buff, "%d", (int)getpid()); len = fc_itoa(getpid(), buff);
return writeToFile(pidFilename, buff, len); return writeToFile(pidFilename, buff, len);
} }

View File

@ -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, static inline void fc_server_set_group_ptr_name(FCServerGroupInfo *ginfo,
const char *group_name) const char *group_name)
{ {
int len;
ginfo->group_name.str = ginfo->name_buff; ginfo->group_name.str = ginfo->name_buff;
ginfo->group_name.len = snprintf(ginfo->name_buff, len = strlen(group_name);
sizeof(ginfo->name_buff) - 1, "%s", group_name); if (len >= sizeof(ginfo->name_buff)) {
if (ginfo->group_name.len == 0) { len = sizeof(ginfo->name_buff) - 1;
return;
} }
fc_trim(ginfo->group_name.str); memcpy(ginfo->name_buff, group_name, len);
ginfo->group_name.len = strlen(ginfo->group_name.str); *(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, static inline void fc_server_set_ip_prefix(FCServerGroupInfo *ginfo,

View File

@ -528,18 +528,20 @@ void daemon_init(bool bCloseFiles)
char *bin2hex(const char *s, const int len, char *szHexBuff) char *bin2hex(const char *s, const int len, char *szHexBuff)
{ {
const char *hex_chars = "0123456789abcdef";
unsigned char *p; unsigned char *p;
unsigned char *pEnd; unsigned char *pEnd;
int nLen; char *dest;
nLen = 0; dest = szHexBuff;
pEnd = (unsigned char *)s + len; pEnd = (unsigned char *)s + len;
for (p=(unsigned char *)s; p<pEnd; p++) for (p=(unsigned char *)s; p<pEnd; p++)
{ {
nLen += sprintf(szHexBuff + nLen, "%02x", *p); *dest++ = hex_chars[(*p >> 4) & 0x0F];
*dest++ = hex_chars[*p & 0x0F];
} }
szHexBuff[nLen] = '\0'; *dest = '\0';
return szHexBuff; 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) const char *int2str(const int n, char *buff, const bool thousands_separator)
{ {
int len; int len;
len = sprintf(buff, "%d", n); len = fc_itoa(n, buff);
if (thousands_separator) if (thousands_separator)
{ {
add_thousands_separator(buff, len); 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) const char *long2str(const int64_t n, char *buff, const bool thousands_separator)
{ {
int len; int len;
len = sprintf(buff, "%"PRId64, n); len = fc_itoa(n, buff);
if (thousands_separator) if (thousands_separator)
{ {
add_thousands_separator(buff, len); add_thousands_separator(buff, len);