Skip to content

Commit

Permalink
optimize invokelatest call performance
Browse files Browse the repository at this point in the history
People previously were making dumb work-arounds like try/catch around
this to get a bit faster here, since the compiler couldn't optimize it
before. Applications typically shouldn't use this function in
performance sensitive places, as it hints that their design is flawed,
but might as well make it faster anyways.

and optimize invokelatest kwcall too, while we are at it
  • Loading branch information
vtjnash committed Dec 11, 2020
1 parent 99402b4 commit 3870a65
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 27 deletions.
13 changes: 6 additions & 7 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,11 @@ call obsolete versions of a function `f`.
`f` directly, and the type of the result cannot be inferred by the compiler.)
"""
function invokelatest(@nospecialize(f), @nospecialize args...; kwargs...)
kwargs = Base.merge(NamedTuple(), kwargs)
if isempty(kwargs)
return Core._apply_latest(f, args)
return Core._call_latest(f, args...)
end
# We use a closure (`inner`) to handle kwargs.
inner() = f(args...; kwargs...)
Core._apply_latest(inner)
return Core._call_latest(Core.kwfunc(f), kwargs, f, args...)
end

"""
Expand Down Expand Up @@ -738,11 +737,11 @@ of [`invokelatest`](@ref).
world age refers to system state unrelated to the main Julia session.
"""
function invoke_in_world(world::UInt, @nospecialize(f), @nospecialize args...; kwargs...)
kwargs = Base.merge(NamedTuple(), kwargs)
if isempty(kwargs)
return Core._apply_in_world(world, f, args)
return Core._call_in_world(world, f, args...)
end
inner() = f(args...; kwargs...)
Core._apply_in_world(world, inner)
return Core._call_in_world(world, Core.kwfunc(f), kwargs, f, args...)
end

# TODO: possibly make this an intrinsic
Expand Down
4 changes: 2 additions & 2 deletions src/builtin_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ DECLARE_BUILTIN(throw); DECLARE_BUILTIN(is);
DECLARE_BUILTIN(typeof); DECLARE_BUILTIN(sizeof);
DECLARE_BUILTIN(issubtype); DECLARE_BUILTIN(isa);
DECLARE_BUILTIN(_apply); DECLARE_BUILTIN(_apply_pure);
DECLARE_BUILTIN(_apply_latest); DECLARE_BUILTIN(_apply_iterate);
DECLARE_BUILTIN(_apply_in_world);
DECLARE_BUILTIN(_call_latest); DECLARE_BUILTIN(_apply_iterate);
DECLARE_BUILTIN(_call_in_world);
DECLARE_BUILTIN(isdefined); DECLARE_BUILTIN(nfields);
DECLARE_BUILTIN(tuple); DECLARE_BUILTIN(svec);
DECLARE_BUILTIN(getfield); DECLARE_BUILTIN(setfield);
Expand Down
19 changes: 9 additions & 10 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,32 +712,31 @@ JL_CALLABLE(jl_f__apply_pure)
return ret;
}

// this is like `_apply`, but always runs in the newest world
JL_CALLABLE(jl_f__apply_latest)
// this is like a regular call, but always runs in the newest world
JL_CALLABLE(jl_f__call_latest)
{
jl_ptls_t ptls = jl_get_ptls_states();
size_t last_age = ptls->world_age;
if (!ptls->in_pure_callback)
ptls->world_age = jl_world_counter;
jl_value_t *ret = jl_f__apply(NULL, args, nargs);
jl_value_t *ret = jl_apply(args, nargs);
ptls->world_age = last_age;
return ret;
}

// Like `_apply`, but runs in the specified world.
// Like call_in_world, but runs in the specified world.
// If world > jl_world_counter, run in the latest world.
JL_CALLABLE(jl_f__apply_in_world)
JL_CALLABLE(jl_f__call_in_world)
{
JL_NARGSV(_apply_in_world, 2);
jl_ptls_t ptls = jl_get_ptls_states();
size_t last_age = ptls->world_age;
JL_TYPECHK(_apply_in_world, ulong, args[0]);
size_t world = jl_unbox_ulong(args[0]);
world = world <= jl_world_counter ? world : jl_world_counter;
if (!ptls->in_pure_callback) {
if (!ptls->in_pure_callback)
ptls->world_age = world;
}
jl_value_t *ret = do_apply(NULL, args+1, nargs-1, NULL);
jl_value_t *ret = jl_apply(&args[1], nargs - 1);
ptls->world_age = last_age;
return ret;
}
Expand Down Expand Up @@ -1555,8 +1554,8 @@ void jl_init_primitives(void) JL_GC_DISABLED
jl_builtin__expr = add_builtin_func("_expr", jl_f__expr);
jl_builtin_svec = add_builtin_func("svec", jl_f_svec);
add_builtin_func("_apply_pure", jl_f__apply_pure);
add_builtin_func("_apply_latest", jl_f__apply_latest);
add_builtin_func("_apply_in_world", jl_f__apply_in_world);
add_builtin_func("_call_latest", jl_f__call_latest);
add_builtin_func("_call_in_world", jl_f__call_in_world);
add_builtin_func("_typevar", jl_f__typevar);
add_builtin_func("_structtype", jl_f__structtype);
add_builtin_func("_abstracttype", jl_f__abstracttype);
Expand Down
4 changes: 2 additions & 2 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,8 @@ static const std::map<jl_fptr_args_t, JuliaFunction*> builtin_func_map = {
{ &jl_f__apply, new JuliaFunction{"jl_f__apply", get_func_sig, get_func_attrs} },
{ &jl_f__apply_iterate, new JuliaFunction{"jl_f__apply_iterate", get_func_sig, get_func_attrs} },
{ &jl_f__apply_pure, new JuliaFunction{"jl_f__apply_pure", get_func_sig, get_func_attrs} },
{ &jl_f__apply_latest, new JuliaFunction{"jl_f__apply_latest", get_func_sig, get_func_attrs} },
{ &jl_f__apply_in_world, new JuliaFunction{"jl_f__apply_in_world", get_func_sig, get_func_attrs} },
{ &jl_f__call_latest, new JuliaFunction{"jl_f__call_latest", get_func_sig, get_func_attrs} },
{ &jl_f__call_in_world, new JuliaFunction{"jl_f__call_in_world", get_func_sig, get_func_attrs} },
{ &jl_f_throw, new JuliaFunction{"jl_f_throw", get_func_sig, get_func_attrs} },
{ &jl_f_tuple, jltuple_func },
{ &jl_f_svec, new JuliaFunction{"jl_f_svec", get_func_sig, get_func_attrs} },
Expand Down
6 changes: 3 additions & 3 deletions src/jl_exported_funcs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@
XX(jl_expand_with_loc_warn) \
XX(jl_extern_c) \
XX(jl_f__abstracttype) \
XX(jl_f_applicable) \
XX(jl_f__apply) \
XX(jl_f__apply_in_world) \
XX(jl_f__apply_iterate) \
XX(jl_f__apply_latest) \
XX(jl_f__apply_pure) \
XX(jl_f__call_in_world) \
XX(jl_f__call_latest) \
XX(jl_f_applicable) \
XX(jl_f_apply_type) \
XX(jl_f_arrayref) \
XX(jl_f_arrayset) \
Expand Down
4 changes: 2 additions & 2 deletions src/jlfrontend.scm
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@
(= (call include ,x)
(block
,@loc
(call (core _apply_latest) (top include) (call (core svec) ,name ,x))))
(call (core _call_latest) (top include) ,name ,x)))
(= (call include (:: ,mex (top Function)) ,x)
(block
,@loc
(call (core _apply_latest) (top include) (call (core svec) ,mex ,name ,x))))))
(call (core _call_latest) (top include) ,mex ,name ,x)))))
'none 0))

; run whole frontend on a string. useful for testing.
Expand Down
2 changes: 1 addition & 1 deletion src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void *native_functions;
static const jl_fptr_args_t id_to_fptrs[] = {
&jl_f_throw, &jl_f_is, &jl_f_typeof, &jl_f_issubtype, &jl_f_isa,
&jl_f_typeassert, &jl_f__apply, &jl_f__apply_iterate, &jl_f__apply_pure,
&jl_f__apply_latest, &jl_f__apply_in_world, &jl_f_isdefined,
&jl_f__call_latest, &jl_f__call_in_world, &jl_f_isdefined,
&jl_f_tuple, &jl_f_svec, &jl_f_intrinsic_call, &jl_f_invoke_kwsorter,
&jl_f_getfield, &jl_f_setfield, &jl_f_fieldtype, &jl_f_nfields,
&jl_f_arrayref, &jl_f_const_arrayref, &jl_f_arrayset, &jl_f_arraysize, &jl_f_apply_type,
Expand Down

0 comments on commit 3870a65

Please sign in to comment.