Skip to content
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

break inference in Windows env caching to prevent a large set of invalidations from popular packages #52760

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if Sys.iswindows()
const env_dict = IdDict{String, Vector{Cwchar_t}}()
const env_lock = ReentrantLock()

function memoized_env_lookup(str::AbstractString)
function _memoized_env_lookup(str::AbstractString)
# Windows environment variables have a different format from Linux / MacOS, and previously
# incurred allocations because we had to convert a String to a Vector{Cwchar_t} each time
# an environment variable was looked up. This function memoizes that lookup process, storing
Expand All @@ -20,6 +20,12 @@ if Sys.iswindows()
var
end

# The `@nospecialize`d `setindex!` in `iddict` called in `_memoized_env_lookup` unfortunately
# sets up a situation where adding a method `convert(::Type{Array}, ::SomeType)` invalidates
# the memoization method causing a cascade of upstream methods to be invalidated. Therefore,
# break the inference chain here with an `inferencebarrier`.
memoized_env_lookup(str::AbstractString) = inferencebarrier(_memoized_env_lookup(str))::Vector{Cwchar_t}

_getenvlen(var::Vector{UInt16}) = ccall(:GetEnvironmentVariableW,stdcall,UInt32,(Ptr{UInt16},Ptr{UInt16},UInt32),var,C_NULL,0)
_hasenv(s::Vector{UInt16}) = _getenvlen(s) != 0 || Libc.GetLastError() != ERROR_ENVVAR_NOT_FOUND
_hasenv(s::AbstractString) = _hasenv(memoized_env_lookup(s))
Expand Down