Skip to content

Commit 18e19cd

Browse files
committed
some inlining cost model updates
- treat arrayset like arrayref - add small cost for sizeof, nfields, isa, subtype
1 parent 4d062a5 commit 18e19cd

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

base/compiler/optimize.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,20 @@ function statement_cost(ex::Expr, line::Int, src::CodeInfo, sptypes::Vector{Any}
313313
# The efficiency of operations like a[i] and s.b
314314
# depend strongly on whether the result can be
315315
# inferred, so check the type of ex
316-
if f === Main.Core.getfield || f === Main.Core.tuple
316+
if f === Core.getfield || f === Core.tuple
317317
# we might like to penalize non-inferrability, but
318318
# tuple iteration/destructuring makes that impossible
319319
# return plus_saturate(argcost, isknowntype(extyp) ? 1 : params.inline_nonleaf_penalty)
320320
return 0
321-
elseif (f === Main.Core.arrayref || f === Main.Core.const_arrayref) && length(ex.args) >= 3
321+
elseif (f === Core.arrayref || f === Core.const_arrayref || f === Core.arrayset) && length(ex.args) >= 3
322322
atyp = argextype(ex.args[3], src, sptypes, slottypes)
323323
return isknowntype(atyp) ? 4 : params.inline_nonleaf_penalty
324+
elseif f === typeassert && isconstType(widenconst(argextype(ex.args[3], src, sptypes, slottypes)))
325+
return 1
324326
end
325327
fidx = find_tfunc(f)
326328
if fidx === nothing
327-
# unknown/unhandled builtin or anonymous function
329+
# unknown/unhandled builtin
328330
# Use the generic cost of a direct function call
329331
return 20
330332
end

base/compiler/tfuncs.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ function sizeof_tfunc(@nospecialize(x),)
342342
isprimitivetype(x) && return _const_sizeof(x)
343343
return Int
344344
end
345-
add_tfunc(Core.sizeof, 1, 1, sizeof_tfunc, 0)
345+
add_tfunc(Core.sizeof, 1, 1, sizeof_tfunc, 1)
346346
function nfields_tfunc(@nospecialize(x))
347347
isa(x, Const) && return Const(nfields(x.val))
348348
isa(x, Conditional) && return Const(0)
@@ -354,7 +354,7 @@ function nfields_tfunc(@nospecialize(x))
354354
end
355355
return Int
356356
end
357-
add_tfunc(nfields, 1, 1, nfields_tfunc, 0)
357+
add_tfunc(nfields, 1, 1, nfields_tfunc, 1)
358358
add_tfunc(Core._expr, 1, INT_INF, (@nospecialize args...)->Expr, 100)
359359
function typevar_tfunc(@nospecialize(n), @nospecialize(lb_arg), @nospecialize(ub_arg))
360360
lb = Union{}
@@ -482,7 +482,7 @@ function typeof_tfunc(@nospecialize(t))
482482
end
483483
return DataType # typeof(anything)::DataType
484484
end
485-
add_tfunc(typeof, 1, 1, typeof_tfunc, 0)
485+
add_tfunc(typeof, 1, 1, typeof_tfunc, 1)
486486

487487
function typeassert_tfunc(@nospecialize(v), @nospecialize(t))
488488
t = instanceof_tfunc(t)[1]
@@ -537,7 +537,7 @@ function isa_tfunc(@nospecialize(v), @nospecialize(tt))
537537
# TODO: handle non-leaftype(t) by testing against lower and upper bounds
538538
return Bool
539539
end
540-
add_tfunc(isa, 2, 2, isa_tfunc, 0)
540+
add_tfunc(isa, 2, 2, isa_tfunc, 1)
541541

542542
function subtype_tfunc(@nospecialize(a), @nospecialize(b))
543543
a, isexact_a = instanceof_tfunc(a)
@@ -555,7 +555,7 @@ function subtype_tfunc(@nospecialize(a), @nospecialize(b))
555555
end
556556
return Bool
557557
end
558-
add_tfunc(<:, 2, 2, subtype_tfunc, 0)
558+
add_tfunc(<:, 2, 2, subtype_tfunc, 1)
559559

560560
is_dt_const_field(fld::Int) = (
561561
fld == DATATYPE_NAME_FIELDINDEX ||

base/iddict.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function get(d::IdDict{K,V}, @nospecialize(key), @nospecialize(default)) where {
8686
val === default ? default : val::V
8787
end
8888
function getindex(d::IdDict{K,V}, @nospecialize(key)) where {K, V}
89-
val = get(d, key, secret_table_token)
89+
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
9090
val === secret_table_token && throw(KeyError(key))
9191
return val::V
9292
end
@@ -137,15 +137,15 @@ copy(d::IdDict) = typeof(d)(d)
137137
get!(d::IdDict{K,V}, @nospecialize(key), @nospecialize(default)) where {K, V} = (d[key] = get(d, key, default))::V
138138

139139
function get(default::Callable, d::IdDict{K,V}, @nospecialize(key)) where {K, V}
140-
val = get(d, key, secret_table_token)
140+
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
141141
if val === secret_table_token
142142
val = default()
143143
end
144144
return val
145145
end
146146

147147
function get!(default::Callable, d::IdDict{K,V}, @nospecialize(key)) where {K, V}
148-
val = get(d, key, secret_table_token)
148+
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
149149
if val === secret_table_token
150150
val = default()
151151
setindex!(d, val, key)

0 commit comments

Comments
 (0)