use fixed stack buffer for UV_THREADPOOL_SIZE

This commit is contained in:
konakona418 2025-09-14 07:20:52 +08:00
parent b0561e3e6e
commit 7233af5c6e

View File

@ -192,44 +192,26 @@ void uv__threadpool_cleanup(void) {
static void init_threads(void) {
const char env_name[] = "UV_THREADPOOL_SIZE";
uv_thread_options_t config;
unsigned int i;
size_t bufsize;
/* a size of 5 should work quite fine, as there won't be many threads
* and the MAX_THREADPOOL_SIZE is 1024
*/
char fastvarbuf[5];
char* varbuf;
char fastvarbuf[16];
const char* val;
int err;
uv_sem_t sem;
nthreads = ARRAY_SIZE(default_threads);
bufsize = ARRAY_SIZE(fastvarbuf);
varbuf = NULL;
int err = uv_os_getenv(env_name, fastvarbuf, &bufsize);
err = uv_os_getenv("UV_THREADPOOL_SIZE", fastvarbuf, &bufsize);
if (err == 0)
val = fastvarbuf;
else if (err == UV_ENOBUFS) {
/* this should not happen, but just in case */
varbuf = uv__malloc(bufsize);
val = varbuf;
err = uv_os_getenv(env_name, varbuf, &bufsize);
if (err != 0) {
/* failed again */
val = NULL;
uv__free(varbuf);
}
} else {
/* UV_ENOENT and other err */
val = NULL;
}
else
val = NULL; /* UV_ENOBUFS, UV_ENOENT and other err */
if (val != NULL)
nthreads = atoi(val);
@ -238,11 +220,6 @@ static void init_threads(void) {
if (nthreads > MAX_THREADPOOL_SIZE)
nthreads = MAX_THREADPOOL_SIZE;
if (varbuf != NULL) {
uv__free(varbuf);
varbuf = NULL;
}
threads = default_threads;
if (nthreads > ARRAY_SIZE(default_threads)) {
threads = uv__malloc(nthreads * sizeof(threads[0]));