Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve throughput of Environment.GetEnvironmentVariables()#45057

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
jkotas merged 2 commits intodotnet:masterfromstephentoub:envvarsindexof
Nov 22, 2020

Conversation

@stephentoub
Copy link
Member

@stephentoubstephentoub commentedNov 21, 2020
edited
Loading

Use IndexOf{Any} to search for positions rather than open-coded loops, taking advantage of vectorization to improve throughput.

[Benchmark]publicIDictionaryGet()=>Environment.GetEnvironmentVariables();

With the environment variables in my command prompt:

MethodJobToolchainMeanErrorStdDevMedianRatioGen 0Gen 1Gen 2Allocated
GetJob-PPEVTD\master\corerun.exe35.17 us0.423 us0.396 us35.10 us1.005.12700.8545-31.43 KB
GetJob-PCZKGH\pr\corerun.exe14.61 us0.278 us0.726 us14.33 us0.415.12700.8545-31.43 KB

Contributes to#44598

ShreyasJejurkar and davidfowl reacted with thumbs up emoji
@stephentoubstephentoub added this to the6.0.0 milestoneNov 21, 2020
Copy link
Member

@jkotasjkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Nice!

Use IndexOf to search for positions rather than open-coded loops, taking advantage of vectorization to improve throughput.
@jkotasjkotas merged commitbf302de intodotnet:masterNov 22, 2020
@stephentoubstephentoub deleted the envvarsindexof branchNovember 22, 2020 23:32
@adamsitnik
Copy link
Member

@stephentoub do you have any plans on improving the Unix implementation as well? This could improve the developer loop experience on macOS

@am11
Copy link
Member

@adamsitnik,Environment.Variables.Windows.cs is compiled for both Windows and Unix under feature coreclr.

@adamsitnik
Copy link
Member

is compiled for both Windows and Unix under feature coreclr

@am11 it might be, but according to the data shared in#41871, getting a single env var on macOS is still 4 times slower compared to Windows (using same hardware). And the time is not spent in the method that@stephentoub has optimized in this particular PR (see#866 (comment) for details)

@stephentoub
Copy link
MemberAuthor

do you have any plans on improving the Unix implementation as well? This could improve the developer loop experience on macOS

As@am11 said, this is the implementation of GetEnvironmentVariables for both Windows and Unix. GetEnvironmentVariable (singular) doesn't use this, on any platform.

@adamsitnik
Copy link
Member

Are there any plans to improve the performance of Unix implementation of the GetEnvironmentVariable (singular) method?

@stephentoub
Copy link
MemberAuthor

stephentoub commentedNov 23, 2020
edited
Loading

The costs you highlight for GetEnvironmentVariable are because the runtime reads and stores the UTF8 env vars on first use, and then calls to GetEnvironmentVariable need to transcode the UTF16 key being searched for, which is done linearly. To make GetEnvironmentVariable faster in microbenchmarks, the runtime could instead pre-transcode them all and potentially store them in a hashtable. The problem, though, is trying to optimize for microbenchmarks on the throughput of GetEnvironmentVariable will likely lead you to do the "wrong" thing. No one should be calling GetEnvironmentVariable on a hot path; instead, while there are exceptions to this, it's generally used once for a given key, often during startup / first use of some code that checks it for configuration info and then never looks at it again; in my experience, there are also generally many more environment variables in the environment than the app actually cares about. We could spend way more time transcoding all env vars, find we made a benchmark.net test of GetEnvironmentVariable much faster, but actually slowed down the overall startup of an app with no measurable reward to show for it. If you'd like to prototype that and can demonstrate it does help with the overall startup of various app types (e.g. because it turns out we end up looking for lots of environment variables, maybe multiple times, or because we have to transcode them all anyway for an inevitable call to GetEnvironmentVariables, plural), then we could certainly consider that direction. It would make multiple calls to GetEnvironmentVariables faster as well.

(It's also possible there are some simpler, smaller optimizations possible. For example, if the vast majority of keys are ASCII, maybe there could be a fast-path that skips the transcoding initially and tries to do the comparison between the byte and char unless a non-ASCII value is hit.)

@kunalspathak
Copy link
Contributor

Perf lab results shows improvements -DrewScoggins/performance-2#3515

@ghostghost locked asresolvedand limited conversation to collaboratorsDec 24, 2020
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@jkotasjkotasjkotas approved these changes

Assignees

No one assigned

Labels

area-System.Runtimetenet-performancePerformance related issue

Projects

None yet

Milestone

6.0.0

Development

Successfully merging this pull request may close these issues.

5 participants

@stephentoub@adamsitnik@am11@kunalspathak@jkotas

[8]ページ先頭

©2009-2025 Movatter.jp