Skip to content

Commit 43c784b

Browse files
vtjnashKristofferC
authored andcommitted
gf: make dispatch heuristic representation explicit (#58167)
Makes this query more accurate for the rare nonfunction_mt dispatch, but otherwise not expected to be a visible change. (needed for future followup work, so splitting this out into a small change since it can be done independently) (cherry picked from commit fb31b3c)
1 parent ea25e2a commit 43c784b

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

src/datatype.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ JL_DLLEXPORT jl_typename_t *jl_new_typename_in(jl_sym_t *name, jl_module_t *modu
8080
tn->partial = NULL;
8181
tn->atomicfields = NULL;
8282
tn->constfields = NULL;
83+
jl_atomic_store_relaxed(&tn->cache_entry_count, 0);
8384
tn->max_methods = 0;
8485
tn->constprop_heustic = 0;
8586
return tn;

src/gf.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,15 @@ jl_method_instance_t *cache_method(
16181618
}
16191619
else {
16201620
jl_typemap_insert(cache, parent, newentry, offs);
1621+
if (mt) {
1622+
jl_datatype_t *dt = jl_nth_argument_datatype((jl_value_t*)tt, 1);
1623+
if (dt) {
1624+
jl_typename_t *tn = dt->name;
1625+
int cache_entry_count = jl_atomic_load_relaxed(&tn->cache_entry_count);
1626+
if (cache_entry_count < 31)
1627+
jl_atomic_store_relaxed(&tn->cache_entry_count, cache_entry_count + 1);
1628+
}
1629+
}
16211630
}
16221631

16231632
JL_GC_POP();
@@ -3616,9 +3625,9 @@ STATIC_INLINE jl_method_instance_t *jl_lookup_generic_(jl_value_t *F, jl_value_t
36163625
mt = jl_gf_mtable(F);
36173626
jl_genericmemory_t *leafcache = jl_atomic_load_relaxed(&mt->leafcache);
36183627
entry = NULL;
3619-
if (leafcache != (jl_genericmemory_t*)jl_an_empty_memory_any &&
3620-
jl_typetagis(jl_atomic_load_relaxed(&mt->cache), jl_typemap_level_type)) {
3621-
// hashing args is expensive, but looking at mt->cache is probably even more expensive
3628+
int cache_entry_count = jl_atomic_load_relaxed(&((jl_datatype_t*)FT)->name->cache_entry_count);
3629+
if (leafcache != (jl_genericmemory_t*)jl_an_empty_memory_any && (cache_entry_count == 0 || cache_entry_count >= 8)) {
3630+
// hashing args is expensive, but so do that only if looking at mc->cache is probably even more expensive
36223631
tt = lookup_arg_type_tuple(F, args, nargs);
36233632
if (tt != NULL)
36243633
entry = lookup_leafcache(leafcache, (jl_value_t*)tt, world);

src/jltypes.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,26 +3007,27 @@ void jl_init_types(void) JL_GC_DISABLED
30073007
jl_typename_type->name->mt = jl_nonfunction_mt;
30083008
jl_typename_type->super = jl_any_type;
30093009
jl_typename_type->parameters = jl_emptysvec;
3010-
jl_typename_type->name->n_uninitialized = 16 - 2;
3011-
jl_typename_type->name->names = jl_perm_symsvec(16, "name", "module",
3010+
jl_typename_type->name->n_uninitialized = 17 - 2;
3011+
jl_typename_type->name->names = jl_perm_symsvec(17, "name", "module",
30123012
"names", "atomicfields", "constfields",
30133013
"wrapper", "Typeofwrapper", "cache", "linearcache",
30143014
"mt", "partial",
30153015
"hash", "n_uninitialized",
30163016
"flags", // "abstract", "mutable", "mayinlinealloc",
3017-
"max_methods", "constprop_heuristic");
3018-
const static uint32_t typename_constfields[1] = { 0x00003a27 }; // (1<<0)|(1<<1)|(1<<2)|(1<<5)|(1<<9)|(1<<11)|(1<<12)|(1<<13) ; TODO: put back (1<<3)|(1<<4) in this list
3019-
const static uint32_t typename_atomicfields[1] = { 0x00000180 }; // (1<<7)|(1<<8)
3017+
"cache_entry_count", "max_methods", "constprop_heuristic");
3018+
const static uint32_t typename_constfields[1] = { 0b00011101000100111 }; // TODO: put back atomicfields and constfields in this list
3019+
const static uint32_t typename_atomicfields[1] = { 0b00100000110000000 };
30203020
jl_typename_type->name->constfields = typename_constfields;
30213021
jl_typename_type->name->atomicfields = typename_atomicfields;
30223022
jl_precompute_memoized_dt(jl_typename_type, 1);
3023-
jl_typename_type->types = jl_svec(16, jl_symbol_type, jl_any_type /*jl_module_type*/,
3023+
jl_typename_type->types = jl_svec(17, jl_symbol_type, jl_any_type /*jl_module_type*/,
30243024
jl_simplevector_type, jl_any_type/*jl_voidpointer_type*/, jl_any_type/*jl_voidpointer_type*/,
30253025
jl_type_type, jl_type_type, jl_simplevector_type, jl_simplevector_type,
30263026
jl_methtable_type, jl_any_type,
30273027
jl_any_type /*jl_long_type*/, jl_any_type /*jl_int32_type*/,
30283028
jl_any_type /*jl_uint8_type*/,
30293029
jl_any_type /*jl_uint8_type*/,
3030+
jl_any_type /*jl_uint8_type*/,
30303031
jl_any_type /*jl_uint8_type*/);
30313032

30323033
jl_methtable_type->name = jl_new_typename_in(jl_symbol("MethodTable"), core, 0, 1);
@@ -3856,6 +3857,7 @@ void jl_init_types(void) JL_GC_DISABLED
38563857
jl_svecset(jl_typename_type->types, 13, jl_uint8_type);
38573858
jl_svecset(jl_typename_type->types, 14, jl_uint8_type);
38583859
jl_svecset(jl_typename_type->types, 15, jl_uint8_type);
3860+
jl_svecset(jl_typename_type->types, 16, jl_uint8_type);
38593861
jl_svecset(jl_methtable_type->types, 4, jl_long_type);
38603862
jl_svecset(jl_methtable_type->types, 5, jl_module_type);
38613863
jl_svecset(jl_methtable_type->types, 6, jl_array_any_type);

src/julia.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ typedef struct {
536536
uint8_t mutabl:1;
537537
uint8_t mayinlinealloc:1;
538538
uint8_t _reserved:5;
539+
_Atomic(uint8_t) cache_entry_count; // (approximate counter of TypeMapEntry for heuristics)
539540
uint8_t max_methods; // override for inference's max_methods setting (0 = no additional limit or relaxation)
540541
uint8_t constprop_heustic; // override for inference's constprop heuristic
541542
} jl_typename_t;

test/core.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ for (T, c) in (
3939
(Core.MethodTable, [:defs, :leafcache, :cache, :max_args]),
4040
(Core.TypeMapEntry, [:next, :min_world, :max_world]),
4141
(Core.TypeMapLevel, [:arg1, :targ, :name1, :tname, :list, :any]),
42-
(Core.TypeName, [:cache, :linearcache]),
42+
(Core.TypeName, [:cache, :linearcache, :cache_entry_count]),
4343
(DataType, [:types, :layout]),
4444
(Core.Memory, []),
4545
(Core.GenericMemoryRef, []),

0 commit comments

Comments
 (0)