-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Investigate which other properties of System.Environment
can be cached.
#57442
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
I would guess they do change when you do impersonation (eg. |
Nice catch @filipnavara, do you know whether they can change on Unix? |
The caller can modify the array. The clone is done to make sure that the next caller gets the unmodified content. Removing the clone would be a breaking change. |
Indeed, but in |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsI noticed that the following
Other properties like
|
I noticed that the following
System.Environment
properties (on Windows at least) are not cached and instead perform syscalls and allocations to return a value that does not change over the course of a program's lifetime:CommandLine
- partially cached, since it does not always call into native code, but it still every time creates a string and calls the publicGetCommandLineArgs
which needlessly clones an internal array (that clone could also be eliminated when this property gets initialized).CurrentDirectory
- this cannot be entirely cached since it might change by directly calling the Windows APIs but we could check whether the current directory has changed since the last time we got this property and avoid a string allocation if it hasn't, returning a cached string object.MachineName
- calls a Windows API function and allocates a new string every time. Documentation says that "This name is established at system startup, when the system reads it from the registry."SystemDirectory
- calls a Windows API function and allocates a new string every time. Doubt it can change without reinstalling Windows.SystemPageSize
- calls a Windows API function every time (@jkotas called it "slow" in Enforce scatter/gather file I/O Windows API requirements et. al. #57424 (comment)). Doubt it can change, couldn't find anything online (searching "windows change page size" returns articles about changing the size of the page file, an unrelated thing").UserDomainName
/UserInteractive
/UserName
- I'm not sure about whether they can change in the middle of a program's execution, but at least we could cache identical string objects like I proposed withCurrentDirectory
.Version
- a cross-platform API, it performs reflection and allocates a new string with the same content every time.Other properties like
OSVersion
andProcessId
are already cached. We could cache more unchangable properties as well to improve performance. I might be wrong about some of them, feel free to modify the list accordingly.The text was updated successfully, but these errors were encountered: