Skip to content

Commit b07fb40

Browse files
vtjnashKristofferC
authored andcommitted
gf: cache cache_with_orig decision (#48833)
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)
1 parent 4710009 commit b07fb40

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

src/gf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,8 @@ static jl_method_instance_t *cache_method(
11951195
}
11961196
// TODO: maybe assert(jl_isa_compileable_sig(compilationsig, sparams, definition));
11971197
newmeth = jl_specializations_get_linfo(definition, (jl_value_t*)compilationsig, sparams);
1198+
if (newmeth->cache_with_orig)
1199+
cache_with_orig = 1;
11981200

11991201
jl_tupletype_t *cachett = tt;
12001202
jl_svec_t* guardsigs = jl_emptysvec;
@@ -1261,6 +1263,10 @@ static jl_method_instance_t *cache_method(
12611263
max_valid = max_valid2;
12621264
cachett = compilationsig;
12631265
}
1266+
else {
1267+
// do not revisit this decision
1268+
newmeth->cache_with_orig = 1;
1269+
}
12641270
}
12651271

12661272
// now scan `cachett` and ensure that `Type{T}` in the cache will be matched exactly by `typeof(T)`

src/jltypes.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,7 +2531,7 @@ void jl_init_types(void) JL_GC_DISABLED
25312531
jl_method_instance_type =
25322532
jl_new_datatype(jl_symbol("MethodInstance"), core,
25332533
jl_any_type, jl_emptysvec,
2534-
jl_perm_symsvec(9,
2534+
jl_perm_symsvec(10,
25352535
"def",
25362536
"specTypes",
25372537
"sparam_vals",
@@ -2540,8 +2540,9 @@ void jl_init_types(void) JL_GC_DISABLED
25402540
"callbacks",
25412541
"cache",
25422542
"inInference",
2543+
"cache_with_orig",
25432544
"precompiled"),
2544-
jl_svec(9,
2545+
jl_svec(10,
25452546
jl_new_struct(jl_uniontype_type, jl_method_type, jl_module_type),
25462547
jl_any_type,
25472548
jl_simplevector_type,
@@ -2550,6 +2551,7 @@ void jl_init_types(void) JL_GC_DISABLED
25502551
jl_any_type,
25512552
jl_any_type,
25522553
jl_bool_type,
2554+
jl_bool_type,
25532555
jl_bool_type),
25542556
jl_emptysvec,
25552557
0, 1, 3);

src/julia.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ struct _jl_method_instance_t {
369369
jl_array_t *callbacks; // list of callback functions to inform external caches about invalidations
370370
_Atomic(struct _jl_code_instance_t*) cache;
371371
uint8_t inInference; // flags to tell if inference is running on this object
372+
uint8_t cache_with_orig; // !cache_with_specTypes
372373
uint8_t precompiled; // true if this instance was generated by an explicit `precompile(...)` call
373374
};
374375

src/method.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ JL_DLLEXPORT jl_method_instance_t *jl_new_method_instance_uninit(void)
447447
li->callbacks = NULL;
448448
jl_atomic_store_relaxed(&li->cache, NULL);
449449
li->inInference = 0;
450+
li->inInference = 0;
450451
li->precompiled = 0;
451452
return li;
452453
}

0 commit comments

Comments
 (0)