-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce Environment.GetEnvironmentVariables calls on start-up #49856
Conversation
Tagging subscribers to this area: @maryamariyan, @safern Issue DetailsResolves #44733
|
918eb68
to
9b1ca72
Compare
9b1ca72
to
fa39359
Compare
...tensions.Configuration.EnvironmentVariables/src/EnvironmentVariablesConfigurationProvider.cs
Outdated
Show resolved
Hide resolved
private static IDictionary CreateEnvironmentVariablesCache() | ||
{ | ||
IDictionary environmentVariables; | ||
s_environmentVariablesCache = environmentVariables = Environment.GetEnvironmentVariables(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will misbehave if someone adds/sets an environment variable before Gen2 runs (which can be anytime). I think a better fix would be to cache it inside Environment class for Windows too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a better fix would be to cache it inside Environment class for Windows too.
If it was in Environment
would it need to do a defensive copy on return since its returning an IDictionary
that is writable; but unlinked from environment vars? That would mean if it was called only once it would create 2 IDictionary
s which would be a regression; could alternatively make it a live IDictionary
so changes are reflected in Environment
, but that might be breaking?
This will misbehave if someone adds/sets an environment variable before Gen2 runs
Might be bigger issues there?
-
The
ConfigurationProvider
has aSet
method; howeverEnvironmentVariablesConfigurationProvider
doesn't callEnvironment.SetEnvironmentVariable
if that set method is called but just updates that instance's copy. -
The
ConfigurationProvider
also has anIChangeToken
which says to update and reload the configuration however ifEnvironment.SetEnvironmentVariable
is called it doesn't reload and fire that event (unlike with file config), nor is there currently anEnvironment
event that it can listen to trigger that reload of configuration.
842c6f4
to
3172aa5
Compare
Does this actually improve startup performance that is the primary metric that we care about? This is replacing one cost (redundant short term allocation that will get collected quickly) with a different cost (dragging the allocation all the way to Gen2 and only allowing it to be collected then). |
I assume its also the reducing the multiple copies of the keys and value environment runtime/src/libraries/System.Private.CoreLib/src/System/Environment.Variables.Windows.cs Lines 93 to 108 in c373952
Mono/Linux takes a different approach; which may be what @marek-safar #49856 (comment) was referring to and caches everything in a runtime/src/mono/System.Private.CoreLib/src/System/Environment.Unix.Mono.cs Lines 88 to 94 in c373952
|
CoreCLR on Linux uses similar approach, except that the cache is in the Win32 PAL. It should be possible to switch CoreCLR to a similar plan on Linux, but not on Windows. |
Why wouldn't it work on Windows? (For process level environment vars) |
Code out there depends on environment changes done via |
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue DetailsCaching it privately here is safer than caching in Then throw the cache away at Gen2 as shouldn't hold onto it forever as its only looked at at start-up; but a Before After And the allocated As well as the Gen2Callback Resolves #44733
|
So wouldn't be able to cache in
Locally (for me) one call to |
Oh the 15kb might be 3 calls; so is only a saving of 10kB (and 10Kb less parsing) |
@benaadams @jkotas what you think is the next step here with this PR? |
I think that this PR can be closed. It does not save that much if anything, and it comes with other issues. There may be an opportunity to move the caching from Win32 PAL to C# on non-Windows, but that should be a separate PR. |
@benaadams Thank you anyway for giving it a shot! Keep these coming :-) |
Caching it privately here is safer than caching in
Environment
as theIDictionary
is writable and at this level we know its not being modified.Then throw the cache away at Gen2 as shouldn't hold onto it forever as its only looked at at start-up; but a
WeakReference<T>
would have the risk of discarding too early as there are likely many allocations at startup.Before
After
And the allocated
IDictionary
(HashTable
) is collectedAs well as the Gen2Callback
Resolves #44733
Contributes to #44598