Skip to content

Commit 9bae8c4

Browse files
committed
make the TypeMap capable of splitting using the TypeName group of the argument
Previously, the TypeMap could only split on leaf types. Now it also has the ability to split on more abstract types. This helps gives types their own linear list to scan to find its constructors, instead of needing to search through all constructors.
1 parent 11b6433 commit 9bae8c4

File tree

8 files changed

+364
-64
lines changed

8 files changed

+364
-64
lines changed

base/reflection.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,18 @@ function visit(f, mc::Core.TypeMapLevel)
924924
isassigned(e, i) && visit(f, e[i])
925925
end
926926
end
927+
if mc.tname !== nothing
928+
e = mc.tname::Vector{Any}
929+
for i in 2:2:length(e)
930+
isassigned(e, i) && visit(f, e[i])
931+
end
932+
end
933+
if mc.name1 !== nothing
934+
e = mc.name1::Vector{Any}
935+
for i in 2:2:length(e)
936+
isassigned(e, i) && visit(f, e[i])
937+
end
938+
end
927939
mc.list !== nothing && visit(f, mc.list)
928940
mc.any !== nothing && visit(f, mc.any)
929941
nothing

src/datatype.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ JL_DLLEXPORT jl_methtable_t *jl_new_method_table(jl_sym_t *name, jl_module_t *mo
5555
mt->kwsorter = NULL;
5656
mt->backedges = NULL;
5757
JL_MUTEX_INIT(&mt->writelock);
58-
mt->offs = 1;
58+
mt->offs = 0;
5959
mt->frozen = 0;
6060
return mt;
6161
}
@@ -569,8 +569,8 @@ JL_DLLEXPORT jl_datatype_t *jl_new_datatype(
569569
// as an optimization
570570
tn->mt = jl_new_method_table(name, module);
571571
jl_gc_wb(tn, tn->mt);
572-
if (jl_svec_len(parameters) > 0)
573-
tn->mt->offs = 0;
572+
if (jl_svec_len(parameters) == 0 && !abstract)
573+
tn->mt->offs = 1;
574574
}
575575
else {
576576
// Everything else, gets to use the unified table

src/dump.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -771,22 +771,6 @@ static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_li
771771
}
772772
return;
773773
}
774-
if (t == jl_typemap_level_type) {
775-
// perform some compression on the typemap levels
776-
// (which will need to be rehashed during deserialization anyhow)
777-
jl_typemap_level_t *node = (jl_typemap_level_t*)v;
778-
assert( // make sure this type has the expected ordering and layout
779-
offsetof(jl_typemap_level_t, arg1) == 0 * sizeof(jl_value_t*) &&
780-
offsetof(jl_typemap_level_t, targ) == 1 * sizeof(jl_value_t*) &&
781-
offsetof(jl_typemap_level_t, linear) == 2 * sizeof(jl_value_t*) &&
782-
offsetof(jl_typemap_level_t, any) == 3 * sizeof(jl_value_t*) &&
783-
sizeof(jl_typemap_level_t) == 4 * sizeof(jl_value_t*));
784-
jl_serialize_value(s, node->arg1);
785-
jl_serialize_value(s, node->targ);
786-
jl_serialize_value(s, node->linear);
787-
jl_serialize_value(s, node->any);
788-
return;
789-
}
790774

791775
char *data = (char*)jl_data_ptr(v);
792776
size_t i, j, np = t->layout->npointers;

src/jltypes.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,6 @@ void jl_init_types(void) JL_GC_DISABLED
18731873
jl_any_type->super = jl_any_type;
18741874
jl_nonfunction_mt = jl_any_type->name->mt;
18751875
jl_any_type->name->mt = NULL;
1876-
jl_nonfunction_mt->offs = 0;
18771876

18781877
jl_type_type = (jl_unionall_t*)jl_new_abstracttype((jl_value_t*)jl_symbol("Type"), core, jl_any_type, jl_emptysvec);
18791878
jl_type_typename = ((jl_datatype_t*)jl_type_type)->name;
@@ -2091,17 +2090,21 @@ void jl_init_types(void) JL_GC_DISABLED
20912090

20922091
jl_typemap_level_type =
20932092
jl_new_datatype(jl_symbol("TypeMapLevel"), core, jl_any_type, jl_emptysvec,
2094-
jl_perm_symsvec(4,
2093+
jl_perm_symsvec(6,
20952094
"arg1",
20962095
"targ",
2096+
"name1",
2097+
"tname",
20972098
"list",
20982099
"any"),
2099-
jl_svec(4,
2100+
jl_svec(6,
2101+
jl_any_type,
2102+
jl_any_type,
21002103
jl_any_type,
21012104
jl_any_type,
21022105
jl_any_type,
21032106
jl_any_type),
2104-
0, 1, 4);
2107+
0, 1, 6);
21052108

21062109
jl_typemap_entry_type =
21072110
jl_new_datatype(jl_symbol("TypeMapEntry"), core, jl_any_type, jl_emptysvec,

src/julia.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,22 @@ typedef struct _jl_typemap_entry_t {
518518
int8_t va; // isVararg(sig)
519519
} jl_typemap_entry_t;
520520

521-
// one level in a TypeMap tree
522-
// indexed by key if it is a sublevel in an array
521+
// one level in a TypeMap tree (each level splits on a type at a given offset)
523522
typedef struct _jl_typemap_level_t {
524523
JL_DATA_TYPE
525-
jl_array_t *arg1;
526-
jl_array_t *targ;
527-
jl_typemap_entry_t *linear; // jl_typemap_t * (but no more levels)
528-
jl_typemap_t *any; // type at offs is Any
524+
// these vectors contains vectors of more levels in their intended visit order
525+
// with an index that gives the functionality of a sorted dict.
526+
// next split may be on Type{T} as LeafTypes then TypeName's parents up to Any
527+
// next split may be on LeafType
528+
// next split may be on TypeName
529+
jl_array_t *arg1; // contains LeafType
530+
jl_array_t *targ; // contains Type{LeafType}
531+
jl_array_t *name1; // contains non-abstract TypeName, for parents up to (excluding) Any
532+
jl_array_t *tname; // contains a dict of Type{TypeName}, for parents up to Any
533+
// next a linear list of things too complicated at this level for analysis (no more levels)
534+
jl_typemap_entry_t *linear;
535+
// finally, start a new level if the type at offs is Any
536+
jl_typemap_t *any;
529537
} jl_typemap_level_t;
530538

531539
// contains the TypeMap for one Type

src/julia_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ void jl_mach_gc_end(void);
10381038

10391039
typedef uint_t (*smallintset_hash)(size_t val, jl_svec_t *data);
10401040
typedef int (*smallintset_eq)(size_t val, const void *key, jl_svec_t *data, uint_t hv);
1041-
ssize_t jl_smallintset_lookup(jl_array_t *cache JL_PROPAGATES_ROOT, smallintset_eq eq, const void *key, jl_svec_t *data, uint_t hv);
1041+
ssize_t jl_smallintset_lookup(jl_array_t *cache, smallintset_eq eq, const void *key, jl_svec_t *data, uint_t hv);
10421042
void jl_smallintset_insert(jl_array_t **pcache, jl_value_t *parent, smallintset_hash hash, size_t val, jl_svec_t *data);
10431043

10441044
// -- typemap.c -- //

src/rtutils.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,10 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
664664
n += jl_static_show_x(out, li->uninferred, depth);
665665
}
666666
}
667+
else if (vt == jl_typename_type) {
668+
n += jl_static_show_x(out, jl_unwrap_unionall(((jl_typename_t*)v)->wrapper), depth);
669+
n += jl_printf(out, ".name");
670+
}
667671
else if (vt == jl_simplevector_type) {
668672
n += jl_show_svec(out, (jl_svec_t*)v, "svec", "(", ")");
669673
}

0 commit comments

Comments
 (0)