Conversation
…idator.Validate to eliminate per-call allocations on .NET Framework
nkolev92
reviewed
Apr 8, 2026
nkolev92
previously approved these changes
Apr 8, 2026
kartheekp-ms
reviewed
Apr 9, 2026
kartheekp-ms
approved these changes
Apr 9, 2026
Nigusu-Allehu
approved these changes
Apr 14, 2026
jeffkl
approved these changes
Apr 14, 2026
This was referenced Apr 17, 2026
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI-Generated Pull Request 🤖
This pull request was generated by the VS Perf Rel AI Agent. Please review this AI-generated PR with extra care! For more information, visit our wiki. Please share feedback with TIP Insights
Issue:
PackageIdValidator.Validate()callsEnvironment.GetEnvironmentVariable("NUGET_DISABLE_PACKAGEID_VALIDATION")on every invocation.On .NET Framework (the Visual Studio host process), each call triggers two allocation sources: (1) a
Char[]buffer from the P/Invoke return path viaCOMStringBuffer::ReplaceBufferInternal, and (2) a CAS (Code Access Security) permission demand viaEnvironmentPermission.AddPathList, which internally allocates anEnvironmentStringExpressionSet,String[],Int32[], andObject[].Together this produces ~5 short-lived objects per call.
This method is called from 8 production call sites across 7 files. In the
ProcessRegistrationPagehot path, it is invoked 3× per package version via:PackageMetadataResourceV3.ProcessRegistrationPage→GetReadmeUrl/GetReportAbuseUrl/GetUri→PackageIdValidator.Validate→EnvironmentVariableWrapper.GetEnvironmentVariable→Environment.GetEnvironmentVariable.The hot path call tree from PerfWatson telemetry confirms that 100% of sampled allocations in this issue trace back to this single
GetEnvironmentVariablecall:Issue type: Reduce repeated identical allocations from per-call
Environment.GetEnvironmentVariableon a hot pathProposed fix: Cache the environment variable result in a
private static readonly Lazy<bool>field that reads fromEnvironmentVariableWrapper.Instanceonce per process. The production path (env == null) reads the cached value; the test path (env != null) reads directly from the providedIEnvironmentVariableReaderto preserve testability.This follows the existing convention in the codebase:
EnhancedHttpRetryHelperuses the identicalLazy<bool>+IEnvironmentVariableReaderpattern (lines 66–72) for cachingNUGET_RETRY_HTTP_429and other environment variables.Char[](string buffer)EnvironmentStringExpressionSet(CAS)String[](CAS split)Int32[](CAS split)Object[](CAS ArrayList)Lazy<bool>, once per process)Impact: Eliminates 100% of sampled allocations in this issue's call tree. For a query returning 50 versions, this removes ~750 short-lived objects per query. The CAS-related allocations (~64% of sampled weight) are .NET Framework-specific and affect the Visual Studio host process directly.
Safety: No behavior change — same validation logic, same exception type, same conditions.
Lazy<bool>is thread-safe by default (ExecutionAndPublication). The env var is a diagnostic escape hatch with zero in-process writers (SetEnvironmentVariableis never called for this variable anywhere in the codebase).SecurityExceptionfrom the underlyingGetEnvironmentVariableis still handled —EnvironmentVariableWrappercatches it and returnsnull, which evaluates tofalse(validation enabled), identical to the pre-fix behavior. Existing tests pass a mockIEnvironmentVariableReaderand bypass the cache entirely.Best practices wiki
See related failure in PRISM
ADO work item