Cygwin: smallprint.cc: Convert tmpbuf to lockless

The old technique was from a time when we had to reduce stack pressure
by moving 64K buffers elsewhere.  It was implemented using a static
global buffer, guarded by a muto. However, that adds a lock which may
unnecessarily serialize threads.

Use Windows heap buffers per invocation instead.  HeapAlloc/HeapFree are
pretty fast, scale nicely in multithreaded scenarios and don't serialize
threads unnecessarily.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2022-08-22 12:01:15 +02:00
parent 1b3a0effd4
commit 07ec40170a
1 changed files with 9 additions and 15 deletions

View File

@ -56,32 +56,26 @@ static const char hex_str_lower[] = "0123456789abcdef";
class tmpbuf class tmpbuf
{ {
static WCHAR buf[NT_MAX_PATH]; PWCHAR buf;
static muto lock;
bool locked;
public: public:
operator WCHAR * () operator WCHAR * ()
{ {
if (!locked) if (!buf)
{ buf = (PWCHAR) HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY,
lock.init ("smallprint_buf")->acquire (); NT_MAX_PATH * sizeof (WCHAR));
locked = true;
}
return buf; return buf;
} }
operator char * () {return (char *) ((WCHAR *) *this);} operator char * () { return (char *) ((WCHAR *) *this); }
tmpbuf (): locked (false) {}; tmpbuf () : buf (NULL) {}
~tmpbuf () ~tmpbuf ()
{ {
if (locked) if (buf)
lock.release (); HeapFree (GetProcessHeap (), 0, buf);
} }
}; };
WCHAR tmpbuf::buf[NT_MAX_PATH];
NO_COPY muto tmpbuf::lock;
static char * static char *
__rn (char *dst, int base, int dosign, long long val, int len, int pad, unsigned long long mask) __rn (char *dst, int base, int dosign, long long val, int len, int pad, unsigned long long mask)
{ {