Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From: Christian Ullrich <chris(at)chrullrich(dot)net>
To: Noah Misch <noah(at)leadboat(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Magnus Hagander <magnus(at)hagander(dot)net>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, "michael(dot)paquier(at)gmail(dot)com" <michael(dot)paquier(at)gmail(dot)com>
Subject: Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013
Date: 2016-11-16 20:45:20
Message-ID: DB6PR0602MB2981431251C29DBAB293A6CAD4BE0@DB6PR0602MB2981.eurprd06.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-hackers

* Noah Misch wrote:

> On Tue, Apr 26, 2016 at 07:39:29PM +0200, Christian Ullrich
> wrote:

> > * Christian Ullrich wrote:

> Patch 1 looks good, except that it should be three patches:
>
> - cosmetic parts: change whitespace and s/0/NULL/
> - remove CloseHandle() call
> - probe for debug CRT modules, not just release CRT modules

Attached as 0001, 0002, 0003, in that order.

0004 is what used to be 0002, it disables caching of "DLL not
loaded" results.

> I recommend also moving the SetEnvironmentVariable() call before
> the putenv calls. That way, if a CRT loads while pgwin32_putenv()
> is executing, the newly-loaded CRT will always have the latest
> value.

Agreed, attached as 0005.

0006 was previously known as 0004, removing all caching.

> > Even with patch 0004, there is still a race condition between
> > the main thread and a theoretical additional thread created by
> > some other component that unloads some CRT between
> > GetProcAddress() and the _putenv() call, but that is hardly
> > fixable.
>
> I think you can fix it by abandoning GetModuleHandle() in favor
> of GetModuleHandleEx() + GetProcessAddress() + FreeLibrary().

Attached as 0007.

> > There is another possible fix, ugly as sin, if we really need
> > to keep the whole environment update machinery *and* cannot do
> > the full loop each time. Patch 0003 pins each CRT when we see
> > it for the first time.

This is now 0008.

Patch topology: master --- 1 .. 5 --- 6 --- 7
\
`- 8

> I prefer the simplicity of abandoning the cache (patch 4), if it
> performs decently. Would you compare the performance of patch 1,
> patches 1+2+3, and patches 1+2+4? This should measure the right
> thing (after substituting @libdir@ for your environment):

Tested with release builds; Core i7-6700K (quad/HT; 4000 MHz).
I did three runs each, and they were always within 0.5 % of each
other's run time.

- patch 1 (now 1-3): 24 μs/iteration (24 s for 1e6)
- patch 1+2+3 (now 1-5 + 8): 29 μs/iteration
- patch 1+2+4 (now 1-7): 30 μs/iteration

I also did a debug build with 1+2+4 that came in at 84 μs/iteration.

--
Christian

... now how do I get all the dangling debris out of the repo ...

Attachment Content-Type Size
0008-getmodulehandleex-pin.patch application/octet-stream 3.2 KB
0001-whitespace.patch application/octet-stream 2.1 KB
0002-closehandle.patch application/octet-stream 809 bytes
0003-debug-crt.patch application/octet-stream 1.9 KB
0004 no-caching-notfound.patch application/octet-stream 1.6 KB
0005-reorder-update.patch application/octet-stream 2.2 KB
0006-no-caching-at-all.patch application/octet-stream 3.9 KB
0007-getmodulehandleex-correctness.patch application/octet-stream 1.3 KB

In response to

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2016-11-16 20:59:28 Re: Re: [COMMITTERS] pgsql: Build HTML documentation using XSLT stylesheets by default
Previous Message Alvaro Herrera 2016-11-16 20:38:51 Re: [COMMITTERS] pgsql: Build HTML documentation using XSLT stylesheets by default

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2016-11-16 20:54:50 Re: Unlogged tables cleanup
Previous Message Alvaro Herrera 2016-11-16 20:38:51 Re: [COMMITTERS] pgsql: Build HTML documentation using XSLT stylesheets by default