From d5dbe3d030226d32e31e765fc86471888120631b Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Fri, 3 Oct 2025 21:03:31 +0800 Subject: [PATCH] free_queue support parameter: need_shrink and set task->shrinked --- HISTORY | 3 ++- src/fast_task_queue.c | 24 ++++++++++++++++-------- src/fast_task_queue.h | 30 +++++++++++++++++------------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/HISTORY b/HISTORY index a723a5d..59444cf 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ -Version 1.81 2025-09-27 +Version 1.81 2025-10-03 * support Linux io_uring + * free_queue support parameter: need_shrink and set task->shrinked Version 1.80 2025-09-10 * getIpaddrByNameEx: IPv4 has priority over IPv6 diff --git a/src/fast_task_queue.c b/src/fast_task_queue.c index 101ad59..719f7e6 100644 --- a/src/fast_task_queue.c +++ b/src/fast_task_queue.c @@ -59,11 +59,11 @@ static int task_alloc_init(struct fast_task_info *task, } int free_queue_init_ex2(struct fast_task_queue *queue, const char *name, - const bool double_buffers, const int max_connections, - const int alloc_task_once, const int min_buff_size, - const int max_buff_size, const int padding_size, - const int arg_size, TaskInitCallback init_callback, - void *init_arg) + const bool double_buffers, const bool need_shrink, + const int max_connections, const int alloc_task_once, + const int min_buff_size, const int max_buff_size, + const int padding_size, const int arg_size, + TaskInitCallback init_callback, void *init_arg) { #define MAX_DATA_SIZE (256 * 1024 * 1024) int alloc_once; @@ -123,6 +123,7 @@ int free_queue_init_ex2(struct fast_task_queue *queue, const char *name, } queue->double_buffers = double_buffers; + queue->need_shrink = need_shrink; queue->min_buff_size = aligned_min_size; queue->max_buff_size = aligned_max_size; queue->padding_size = aligned_padding_size; @@ -183,16 +184,23 @@ void free_queue_push(struct fast_task_info *task) task->send.ptr->length = 0; task->send.ptr->offset = 0; task->req_count = 0; - if (task->send.ptr->size > task->free_queue->min_buff_size) {//need thrink - _realloc_buffer(task->send.ptr, task->free_queue->min_buff_size, false); + if (task->free_queue->need_shrink && task->send. + ptr->size > task->free_queue->min_buff_size) + { //need thrink + _realloc_buffer(task->send.ptr, task->free_queue-> + min_buff_size, false); + task->shrinked = true; } if (task->free_queue->double_buffers) { task->recv.ptr->length = 0; task->recv.ptr->offset = 0; - if (task->recv.ptr->size > task->free_queue->min_buff_size) { + if (task->free_queue->need_shrink && task->recv. + ptr->size > task->free_queue->min_buff_size) + { _realloc_buffer(task->recv.ptr, task->free_queue-> min_buff_size, false); + task->shrinked = true; } } diff --git a/src/fast_task_queue.h b/src/fast_task_queue.h index f9e58c2..5f5f6ff 100644 --- a/src/fast_task_queue.h +++ b/src/fast_task_queue.h @@ -140,6 +140,7 @@ struct fast_task_info volatile uint8_t notify; } nio_stages; //stages for network IO volatile int8_t canceled; //if task canceled + volatile int8_t shrinked; //if task shrinked, since V1.0.81 volatile int reffer_count; int pending_send_count; int64_t req_count; //request count @@ -169,6 +170,7 @@ struct fast_task_queue int block_size; bool malloc_whole_block; bool double_buffers; //if send buffer and recv buffer are independent + bool need_shrink; struct fast_mblock_man allocator; TaskInitCallback init_callback; void *init_arg; @@ -180,22 +182,22 @@ extern "C" { #endif int free_queue_init_ex2(struct fast_task_queue *queue, const char *name, - const bool double_buffers, const int max_connections, - const int alloc_task_once, const int min_buff_size, - const int max_buff_size, const int padding_size, - const int arg_size, TaskInitCallback init_callback, - void *init_arg); + const bool double_buffers, const bool need_shrink, + const int max_connections, const int alloc_task_once, + const int min_buff_size, const int max_buff_size, + const int padding_size, const int arg_size, + TaskInitCallback init_callback, void *init_arg); static inline int free_queue_init_ex(struct fast_task_queue *queue, const char *name, const bool double_buffers, - const int max_connections, const int alloc_task_once, - const int min_buff_size, const int max_buff_size, - const int arg_size) + const bool need_shrink, const int max_connections, + const int alloc_task_once, const int min_buff_size, + const int max_buff_size, const int arg_size) { const int padding_size = 0; - return free_queue_init_ex2(queue, name, double_buffers, max_connections, - alloc_task_once, min_buff_size, max_buff_size, padding_size, - arg_size, NULL, NULL); + return free_queue_init_ex2(queue, name, double_buffers, need_shrink, + max_connections, alloc_task_once, min_buff_size, max_buff_size, + padding_size, arg_size, NULL, NULL); } static inline int free_queue_init(struct fast_task_queue *queue, @@ -204,9 +206,11 @@ static inline int free_queue_init(struct fast_task_queue *queue, { const char *name = ""; const bool double_buffers = false; + const bool need_shrink = true; const int arg_size = 0; - return free_queue_init_ex(queue, name, double_buffers, max_connections, - alloc_task_once, min_buff_size, max_buff_size, arg_size); + return free_queue_init_ex(queue, name, double_buffers, + need_shrink, max_connections, alloc_task_once, + min_buff_size, max_buff_size, arg_size); } static inline void free_queue_set_release_callback(