Skip to content

Commit 5848e99

Browse files
timholyKristofferC
authored andcommitted
Reduce invalidations when loading JuliaData packages (#47889)
(cherry picked from commit e84634e)
1 parent 3e1373e commit 5848e99

File tree

7 files changed

+26
-20
lines changed

7 files changed

+26
-20
lines changed

base/Base.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ include("idset.jl")
168168
include("iterators.jl")
169169
using .Iterators: zip, enumerate, only
170170
using .Iterators: Flatten, Filter, product # for generators
171+
using .Iterators: Stateful # compat (was formerly used in reinterpretarray.jl)
171172

172173
include("namedtuple.jl")
173174

base/array.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,7 +2730,8 @@ keepat!(a::Vector, m::AbstractVector{Bool}) = _keepat!(a, m)
27302730
# set-like operators for vectors
27312731
# These are moderately efficient, preserve order, and remove dupes.
27322732

2733-
_unique_filter!(pred, update!, state) = function (x)
2733+
_unique_filter!(pred::P, update!::U, state) where {P,U} = function (x)
2734+
# P, U force specialization
27342735
if pred(x, state)
27352736
update!(state, x)
27362737
true
@@ -2756,7 +2757,7 @@ union!(v::AbstractVector{T}, itrs...) where {T} =
27562757
symdiff!(v::AbstractVector{T}, itrs...) where {T} =
27572758
_grow!(_shrink_filter!(symdiff!(Set{T}(), v, itrs...)), v, itrs)
27582759

2759-
function _shrink!(shrinker!, v::AbstractVector, itrs)
2760+
function _shrink!(shrinker!::F, v::AbstractVector, itrs) where F
27602761
seen = Set{eltype(v)}()
27612762
filter!(_grow_filter!(seen), v)
27622763
shrinker!(seen, itrs...)
@@ -2768,7 +2769,7 @@ setdiff!( v::AbstractVector, itrs...) = _shrink!(setdiff!, v, itrs)
27682769

27692770
vectorfilter(T::Type, f, v) = T[x for x in v if f(x)]
27702771

2771-
function _shrink(shrinker!, itr, itrs)
2772+
function _shrink(shrinker!::F, itr, itrs) where F
27722773
T = promote_eltype(itr, itrs...)
27732774
keep = shrinker!(Set{T}(itr), itrs...)
27742775
vectorfilter(T, _shrink_filter!(keep), itr)

base/loading.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,8 @@ function explicit_manifest_uuid_path(project_file::String, pkg::PkgId)::Union{No
809809
end
810810
end
811811
# Extensions
812-
for (name, entries::Vector{Any}) in d
812+
for (name, entries) in d
813+
entries = entries::Vector{Any}
813814
for entry in entries
814815
uuid = get(entry, "uuid", nothing)::Union{Nothing, String}
815816
extensions = get(entry, "extensions", nothing)::Union{Nothing, Dict{String, Any}}

base/logging.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,14 @@ function logmsg_code(_module, file, line, level, message, exs...)
378378
id = $(log_data._id)
379379
# Second chance at an early bail-out (before computing the message),
380380
# based on arbitrary logger-specific logic.
381-
if _invoked_shouldlog(logger, level, _module, group, id)
381+
if invokelatest(shouldlog, logger, level, _module, group, id)
382382
file = $(log_data._file)
383383
if file isa String
384384
file = Base.fixup_stdlib_path(file)
385385
end
386386
line = $(log_data._line)
387387
local msg, kwargs
388-
$(logrecord) && handle_message(
388+
$(logrecord) && invokelatest(handle_message,
389389
logger, level, msg, _module, group, id, file, line;
390390
kwargs...)
391391
end

base/reinterpretarray.jl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -722,25 +722,23 @@ function CyclePadding(T::DataType)
722722
CyclePadding(pad, as)
723723
end
724724

725-
using .Iterators: Stateful
726725
@assume_effects :total function array_subpadding(S, T)
727-
checked_size = 0
728726
lcm_size = lcm(sizeof(S), sizeof(T))
729-
s, t = Stateful{<:Any, Any}(CyclePadding(S)),
730-
Stateful{<:Any, Any}(CyclePadding(T))
727+
s, t = CyclePadding(S), CyclePadding(T)
731728
isempty(t) && return true
732729
isempty(s) && return false
730+
checked_size = 0
731+
ps, sstate = iterate(s) # use of Stateful harms inference and makes this vulnerable to invalidation
732+
pad, tstate = iterate(t)
733733
while checked_size < lcm_size
734-
# Take padding in T
735-
pad = popfirst!(t)
736-
# See if there's corresponding padding in S
737734
while true
738-
ps = peek(s)
735+
# See if there's corresponding padding in S
739736
ps.offset > pad.offset && return false
740737
intersect(ps, pad) == pad && break
741-
popfirst!(s)
738+
ps, sstate = iterate(s, sstate)
742739
end
743740
checked_size = pad.offset + pad.size
741+
pad, tstate = iterate(t, tstate)
744742
end
745743
return true
746744
end

base/show.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,8 +1878,12 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In
18781878
# .
18791879
print(io, '.')
18801880
# item
1881-
parens = !(field isa Symbol) || (field::Symbol in quoted_syms)
1882-
quoted = parens || isoperator(field)
1881+
if isa(field, Symbol)
1882+
parens = field in quoted_syms
1883+
quoted = parens || isoperator(field)
1884+
else
1885+
parens = quoted = true
1886+
end
18831887
quoted && print(io, ':')
18841888
parens && print(io, '(')
18851889
show_unquoted(io, field, indent, 0, quote_level)
@@ -2003,10 +2007,11 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In
20032007

20042008
# binary operator (i.e. "x + y")
20052009
elseif func_prec > 0 # is a binary operator
2010+
func = func::Symbol # operator_precedence returns func_prec == 0 for non-Symbol
20062011
na = length(func_args)
2007-
if (na == 2 || (na > 2 && isa(func, Symbol) && func in (:+, :++, :*)) || (na == 3 && func === :(:))) &&
2012+
if (na == 2 || (na > 2 && func in (:+, :++, :*)) || (na == 3 && func === :(:))) &&
20082013
all(a -> !isa(a, Expr) || a.head !== :..., func_args)
2009-
sep = func === :(:) ? "$func" : " " * convert(String, string(func))::String * " " # if func::Any, avoid string interpolation (invalidation)
2014+
sep = func === :(:) ? "$func" : " $func "
20102015

20112016
if func_prec <= prec
20122017
show_enclosed_list(io, '(', func_args, sep, ')', indent, func_prec, quote_level, true)

base/strings/util.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ julia> hex2bytes(a)
830830
"""
831831
function hex2bytes end
832832

833-
hex2bytes(s) = hex2bytes!(Vector{UInt8}(undef, length(s) >> 1), s)
833+
hex2bytes(s) = hex2bytes!(Vector{UInt8}(undef, length(s)::Int >> 1), s)
834834

835835
# special case - valid bytes are checked in the generic implementation
836836
function hex2bytes!(dest::AbstractArray{UInt8}, s::String)

0 commit comments

Comments
 (0)