Skip to content

Commit

Permalink
gf: cache cache_with_orig decision (#48833)
Browse files Browse the repository at this point in the history
Memoizing this can save a lot of repeated effort for queries such as
`@which eltype(String)`. Otherwise we repeatedly try to check if
`eltype(::Type)` is a good way to cache that result, even though it
never gets better the more we check it.

(cherry picked from commit bdcd5e2)
  • Loading branch information
vtjnash authored and KristofferC committed Mar 3, 2023
1 parent 4710009 commit b07fb40
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,8 @@ static jl_method_instance_t *cache_method(
}
// TODO: maybe assert(jl_isa_compileable_sig(compilationsig, sparams, definition));
newmeth = jl_specializations_get_linfo(definition, (jl_value_t*)compilationsig, sparams);
if (newmeth->cache_with_orig)
cache_with_orig = 1;

jl_tupletype_t *cachett = tt;
jl_svec_t* guardsigs = jl_emptysvec;
Expand Down Expand Up @@ -1261,6 +1263,10 @@ static jl_method_instance_t *cache_method(
max_valid = max_valid2;
cachett = compilationsig;
}
else {
// do not revisit this decision
newmeth->cache_with_orig = 1;
}
}

// now scan `cachett` and ensure that `Type{T}` in the cache will be matched exactly by `typeof(T)`
Expand Down
6 changes: 4 additions & 2 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2531,7 +2531,7 @@ void jl_init_types(void) JL_GC_DISABLED
jl_method_instance_type =
jl_new_datatype(jl_symbol("MethodInstance"), core,
jl_any_type, jl_emptysvec,
jl_perm_symsvec(9,
jl_perm_symsvec(10,
"def",
"specTypes",
"sparam_vals",
Expand All @@ -2540,8 +2540,9 @@ void jl_init_types(void) JL_GC_DISABLED
"callbacks",
"cache",
"inInference",
"cache_with_orig",
"precompiled"),
jl_svec(9,
jl_svec(10,
jl_new_struct(jl_uniontype_type, jl_method_type, jl_module_type),
jl_any_type,
jl_simplevector_type,
Expand All @@ -2550,6 +2551,7 @@ void jl_init_types(void) JL_GC_DISABLED
jl_any_type,
jl_any_type,
jl_bool_type,
jl_bool_type,
jl_bool_type),
jl_emptysvec,
0, 1, 3);
Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ struct _jl_method_instance_t {
jl_array_t *callbacks; // list of callback functions to inform external caches about invalidations
_Atomic(struct _jl_code_instance_t*) cache;
uint8_t inInference; // flags to tell if inference is running on this object
uint8_t cache_with_orig; // !cache_with_specTypes
uint8_t precompiled; // true if this instance was generated by an explicit `precompile(...)` call
};

Expand Down
1 change: 1 addition & 0 deletions src/method.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ JL_DLLEXPORT jl_method_instance_t *jl_new_method_instance_uninit(void)
li->callbacks = NULL;
jl_atomic_store_relaxed(&li->cache, NULL);
li->inInference = 0;
li->inInference = 0;
li->precompiled = 0;
return li;
}
Expand Down

0 comments on commit b07fb40

Please sign in to comment.