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

Memoize cwstring when used for env lookup / modification on Windows #51371

Merged
merged 9 commits into from
Nov 27, 2023
10 changes: 9 additions & 1 deletion base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ if Sys.iswindows()
_hasenv(s::Vector{UInt16}) = _getenvlen(s) != 0 || Libc.GetLastError() != ERROR_ENVVAR_NOT_FOUND
_hasenv(s::AbstractString) = _hasenv(cwstring(s))

const env_dict = IdDict{String, Vector{Cwchar_t}}()
const env_lock = ReentrantLock()

function access_env(onError::Function, str::AbstractString)
var = cwstring(str)
var = get(env_dict, str, nothing)
if isnothing(var)
var = @lock env_lock begin
env_dict[str] = cwstring(str)
MasonProtter marked this conversation as resolved.
Show resolved Hide resolved
end
end
len = _getenvlen(var)
if len == 0
return Libc.GetLastError() != ERROR_ENVVAR_NOT_FOUND ? "" : onError(str)
Expand Down