From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 9817F385AC2C; Wed, 24 Aug 2022 09:20:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9817F385AC2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1661332841; bh=juVMHOcYzX4MvXYBeCUlEZ6HZnM6J3LVSbdm10VmeT4=; h=From:To:Subject:Date:From; b=fbKCgQz3iJJOF+C3g8Kqlm/Yhb7yC0zp9dKCVMpLLT7joUHN9VEspWTYJPN2CFW6V Tr6qyVz1xcUh2Z6MnDONKlhafGzd5APqys/66XcfBGNkdVUOQSJGqICPOZNihW7XPP w6RedefwKEnekfj8qDBrhDtluN4PxL68+Bo2Mzj0= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: tls_pathbuf: Use Windows heap X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/master X-Git-Oldrev: 64a11fded15b92b56b91d65fd5b2851245f69299 X-Git-Newrev: 63b503916d4258cec39df09c545498e463d9a088 Message-Id: <20220824092041.9817F385AC2C@sourceware.org> Date: Wed, 24 Aug 2022 09:20:41 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D63b503916d4= 258cec39df09c545498e463d9a088 commit 63b503916d4258cec39df09c545498e463d9a088 Author: Corinna Vinschen Date: Tue Aug 23 10:59:18 2022 +0200 Cygwin: tls_pathbuf: Use Windows heap =20 Rather than using malloc/free for the buffers, we're now using HeapAlloc/HeapFree on a HEAP_NO_SERIALIZE heap created for this thread. =20 Advantages: - Less contention. Our malloc/free doesn't scale well in multithreaded scenarios - Even faster heap allocation by using a non serialized heap. - Internal, local, temporary data not cluttering the user heap. - Internal, local, temporary data not copied over to child process at fork(). =20 Disadvantage: - A forked process has to start allocating temporary buffers from scratch. However, this should be alleviated by the fact that buffer allocation usually reaches its peak very early in process runtime, so the longer the proceess runs, the less buffers have to allocated, and, only few processes don't exec after fork anyway. =20 Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/cygtls.cc | 2 ++ winsup/cygwin/local_includes/cygtls.h | 3 +++ winsup/cygwin/tls_pbuf.cc | 25 +++++++++++++++++++------ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/winsup/cygwin/cygtls.cc b/winsup/cygwin/cygtls.cc index cf3a7daba..afaee8e97 100644 --- a/winsup/cygwin/cygtls.cc +++ b/winsup/cygwin/cygtls.cc @@ -64,6 +64,7 @@ _cygtls::init_thread (void *x, DWORD (*func) (void *, voi= d *)) initialized =3D CYGTLS_INITIALIZED; errno_addr =3D &(local_clib._errno); locals.cw_timer =3D NULL; + locals.pathbufs.clear (); =20 if ((void *) func =3D=3D (void *) cygthread::stub || (void *) func =3D=3D (void *) cygthread::simplestub) @@ -84,6 +85,7 @@ _cygtls::fixup_after_fork () signal_arrived =3D NULL; locals.select.sockevt =3D NULL; locals.cw_timer =3D NULL; + locals.pathbufs.clear (); wq.thread_ev =3D NULL; } =20 diff --git a/winsup/cygwin/local_includes/cygtls.h b/winsup/cygwin/local_in= cludes/cygtls.h index b265978f0..018a59946 100644 --- a/winsup/cygwin/local_includes/cygtls.h +++ b/winsup/cygwin/local_includes/cygtls.h @@ -54,10 +54,13 @@ class tls_pathbuf }; uint64_t _counters; }; + HANDLE tls_heap; char *c_buf[TP_NUM_C_BUFS]; WCHAR *w_buf[TP_NUM_W_BUFS]; =20 public: + void clear () { memset (this, 0, sizeof *this); } + void create (); void destroy (); friend class tmp_pathbuf; friend class san; diff --git a/winsup/cygwin/tls_pbuf.cc b/winsup/cygwin/tls_pbuf.cc index f5f318e98..7c8149856 100644 --- a/winsup/cygwin/tls_pbuf.cc +++ b/winsup/cygwin/tls_pbuf.cc @@ -10,22 +10,32 @@ details. */ =20 #define tls_pbuf _my_tls.locals.pathbufs =20 +void +tls_pathbuf::create () +{ + tls_heap =3D HeapCreate (HEAP_NO_SERIALIZE | HEAP_GENERATE_EXCEPTIONS, + NT_MAX_PATH * sizeof (WCHAR) * 10, /* 640K */ + NT_MAX_PATH * TP_NUM_C_BUFS /* 4.6M */ + + NT_MAX_PATH * TP_NUM_W_BUFS * sizeof (WCHAR)); +} + void tls_pathbuf::destroy () { - for (uint32_t i =3D 0; i < TP_NUM_C_BUFS && c_buf[i]; ++i) - free (c_buf[i]); - for (uint32_t i =3D 0; i < TP_NUM_W_BUFS && w_buf[i]; ++i) - free (w_buf[i]); + if (tls_heap) + HeapDestroy (tls_heap); } =20 char * tmp_pathbuf::c_get () { + if (!tls_pbuf.tls_heap) + tls_pbuf.create (); if (tls_pbuf.c_cnt >=3D TP_NUM_C_BUFS) api_fatal ("Internal error: TP_NUM_C_BUFS too small: %u", TP_NUM_C_BUF= S); if (!tls_pbuf.c_buf[tls_pbuf.c_cnt] - && !(tls_pbuf.c_buf[tls_pbuf.c_cnt] =3D (char *) malloc (NT_MAX_PATH= ))) + && !(tls_pbuf.c_buf[tls_pbuf.c_cnt] + =3D (char *) HeapAlloc (tls_pbuf.tls_heap, 0, NT_MAX_PATH))) api_fatal ("Internal error: Out of memory for new path buf."); return tls_pbuf.c_buf[tls_pbuf.c_cnt++]; } @@ -33,11 +43,14 @@ tmp_pathbuf::c_get () PWCHAR tmp_pathbuf::w_get () { + if (!tls_pbuf.tls_heap) + tls_pbuf.create (); if (tls_pbuf.w_cnt >=3D TP_NUM_W_BUFS) api_fatal ("Internal error: TP_NUM_W_BUFS too small: %u.", TP_NUM_W_BU= FS); if (!tls_pbuf.w_buf[tls_pbuf.w_cnt] && !(tls_pbuf.w_buf[tls_pbuf.w_cnt] - =3D (PWCHAR) malloc (NT_MAX_PATH * sizeof (WCHAR)))) + =3D (PWCHAR) HeapAlloc (tls_pbuf.tls_heap, 0, + NT_MAX_PATH * sizeof (WCHAR)))) api_fatal ("Internal error: Out of memory for new wide path buf."); return tls_pbuf.w_buf[tls_pbuf.w_cnt++]; }